Skip to content

Commit

Permalink
Enhances logging with structured data
Browse files Browse the repository at this point in the history
Updates log statements to include structured emission of
job-related information such as ID, status, and expiry.
Improves log consistency across dead letter, retry, and timeout
handlers, aiding in monitoring and debugging efforts.

Refines job handling within worker by converting job instances
to strings.
  • Loading branch information
eliasjpr committed Nov 27, 2024
1 parent f5e96f5 commit 73261de
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/joobq/dead_letter_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module JoobQ

def self.add(job)
store.mark_as_dead(job, expires)
Log.info { "Job #{job.id} has been added to the dead letter queue" }
Log.info &.emit("Job added to dead letter queue",
job_id: job.jid, status: job.status, expires: job.expires, retries: job.retries, queue: job.queue)
end
end
end
3 changes: 2 additions & 1 deletion src/joobq/middlewares/retry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ module JoobQ
delay = (2 ** (job.retries)) * 1000 # Delay in ms
# Logic to add the job back to the queue after a delay
queue.store.schedule(job, delay, delay_set: RedisStore::FAILED_SET)
Log.info { "Job #{job.id} has been scheduled for retry in #{delay}ms" }
Log.info &.emit("Job has been scheduled for retry", retry_in: delay,
job_id: job.jid, status: job.status, expires: job.expires, retries: job.retries, queue: job.queue)
end
end
end
2 changes: 1 addition & 1 deletion src/joobq/middlewares/throttle.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module JoobQ
elapsed = now - last_job_time
sleep_time = min_interval - elapsed

Log.debug { "Throttling #{queue.name} queue. Last job was #{elapsed}ms ago. Sleeping for #{sleep_time}ms" }
Log.debug &.emit("Throttling #{queue.name} queue. Last job was #{elapsed}ms ago. Sleeping for #{sleep_time}ms")

if sleep_time > 0
sleep (sleep_time / 1000.0).seconds
Expand Down
4 changes: 3 additions & 1 deletion src/joobq/middlewares/timeout.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ module JoobQ
def call(job : Job, queue : BaseQueue, next_middleware : ->) : Nil
if job.expired?
job.expired!
Log.info { "Job #{job.id} has expired" }

DeadLetterManager.add(job)
Log.info &.emit("Job has expired, added to dead letter queue",
job_id: job.jid, status: job.status, expires: job.expires, retries: job.retries, queue: job.queue)
else
next_middleware.call
end
Expand Down
2 changes: 1 addition & 1 deletion src/joobq/worker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module JoobQ
break
else
if job = @queue.next_job
handle_job job
handle_job job.to_s
end
end
end
Expand Down

0 comments on commit 73261de

Please sign in to comment.