Sidekiq jobs disappering - ruby-on-rails

I have a worker class that finds out who to send notifications and then creates notifications at which point push will send out the pushes.
But i have a problem with Sidekiq, that is when i do SomeWorker.perform_in(5.seconds,someID, otherID) it does show up in scheduled on the sidekiq dashboard. but after 5 seconds the job disappears. the count for processed does not update nor it shows up in failed. Infact it just won't show up in any counter. Although sometimes the job is processed and i see the counter and logs and the push. but most of the times it just disappears to be found nowhere. Where is the problem?

You have an old Sidekiq process consuming the job. Make sure all old processes are dead.

Related

Sidekiq scheduled jobs gets automatically deleted (Sidekiq + Rails)

I want the worker to run on a specific date. I am able to schedule jobs in sidekiq. And sidekiq UI also shows scheduled jobs perfectly. But due to unknown reason my data on sidekiq (processed count,scheduled jobs etc.) gets deleted and everything is reset to 0 in sidekiq UI. Can someone please help me understand this issue.
I suspect you are calling flush on Redis, clearing all your data.

Rails 4 ActionMailer deliver_later method not working if given extended wait period

I have a Rails application that is hosted on Heroku that has a feature to send delayed emails.
So the deliver_later! method for ActionMailer is able to send emails asynchronously at a scheduled time.
Basically I have a mailer called NotificationMailer, and when I do
NotificationMailer.send_email(user).deliver_later!(wait_until: 5.minutes.from_now)
or
NotificationMailer.send_email(user).deliver_later!(wait_until: 30.minutes.from_now)
I am able to receive the email 5 minutes from the time it was sent, and 30 minutes from the time it was sent, however when I get more ambitious and do
NotificationMailer.send_email(user).deliver_later!(wait_until: (Time.now.tomorrow.noon - Time.now).seconds.from_now)
Which means I am supposed to receive the next day at noon, but I am unable to receive that email. I looked a lot into this, and I read that Heroku's dynos sleep whenever there's an extended period of inactivity, and then all ActiveJobs in the queue also sleeps. I thought this might be a cause for the problem. I'm not sure how exactly to fix this and would appreciate any suggestions!
You can take a look at this link. But basically after the dyno goes to sleep you loose all the jobs queue the dyno got before sleeping. So you need to save them in a redis store.

Kill multiple Sidekiq jobs from the same worker

I would like to know how to kill many Sidekiq jobs from the same worker at once.
I deployed a bug on a production environment and there are queued jobs that are bugging out. I can simply fix the bug and deploy again, but the jobs are time-sensitive (they send out SMS alert to people).
When the bug is gone, the jobs will be executed and many people will get outdated SMS alerts. So I would like to kill all the jobs from that worker before deploying my fix.
Any suggestions? The buggy jobs are enqueued with many other jobs and I can't just remove all jobs from one queue.
Ideally you should enqueue those messages to a different queue so you can clear that queue on its own. There's no other efficient way to remove a set of jobs.

Locked delayed_job row lingers in the database after capistrano deploy

Whenever I deploy with capistrano or run cap production delayed_job:restart, I end up with the currently-running delayed_job row remaining locked.
The delayed_job process is successfully stopped, a new delayed_job process is started, and a new row is locked by the new process. The problem is that the last process' row is still sitting there & marked as locked. So I have to go into the database manually, delete the row, and then manually add that job back into the queue for the new delayed_job process to get to.
Is there a way for the database cleanup & re-queue of the previous job to happen automatically?
I have the same problem. This happens whenever a job is forcibly killed. Part of the problem is that worker processes are managed by the daemons gem rather than delayed_job itself. I'm currently investigating ways to fix this, such as:
Setting a longer timeout before daemons forcibly terminates (nothing about this in docs for delayed_joob or daemons)
Clearing locks before starting delayed_job workers
I'll post back here whan and if I come up with a solution.
Adjust your Daemon wait time or raise an exception on SIGINT.
#John Carney is correct. In short, all delayed_job workers get sent something like a SIGINT (nice interrupt) on a redeploy. delayed_job workers, by default, will complete their current job (if they are working on one) and then gracefully terminate.
However, if the job that they are working on is a longer-running job, there's an amount of time the Daemon manager waits before it gets annoyed and sends a more serious interrupt signal, like a SIGTERM or SIGKILL. This wait time and what gets sent really depends on your setup and configuration.
When that happens, the delayed_job worker gets killed immediately without being able to finish the job it is working on or even cleanup after itself and mark the job as no longer locked.
This ends up in a "stranded" job that is marked as "locked" but locked to a process/worker that no longer exists. Not good.
That's the crux of the issue and what is happening. To get around this, you have two main options, depending on your what your jobs look like (we use both):
1. Raise an exception when an interrupt is received.
You can do this by setting the raise_signal_exceptions configuration to either :term or true:
Delayed::Worker.raise_signal_exceptions = :term
This configuration options accepts :term, true or false (default). You can read more on the original commit here.
I would try first with :term and see if that solves your issue. If not, you may need to set it to true.
Setting to :term or true will gracefully raise an exception and unlock the job for another delayed_job worker to pickup the job and start working on it.
Setting it to true means that your delayed_job workers won't even attempt to finish the current job that they are working on. They will just immediately raise an exception, unlock the job and terminate themselves.
2. Adjust how your workers are interrupted/terminated/killed on a redeploy.
This really depends on your redeploy, etc. In our case, we are using Cloud66 to handle deploys so we just had to configure this with them. But this is what ours looks like:
stop_sequence: int, 172800, term, 90, kill # Allows long-running delayed jobs to finish before being killed (i.e. on redeploy). Sends SIGINT, waits 48 hours, sends SIGTERM, waits 90 seconds, sends SIGKILL.
On a redeploy, this tells the Daemon manager to follow these steps will each delayed_job worker:
Send a SIGINT.
Wait 172800 seconds (2 days) - we have very long-running jobs.
Send a SIGTERM, if the worker is still alive.
Wait 90 seconds.
Send a SIGKILL, if the worker is still alive.
Anyway, that should help get you on the right track to configuring this properly for yourself.
We use both methods by setting a lengthy timeout as well as raising an exception when a SIGTERM is received. This ensures that if there is a job that runs past the 2 day limit, it will at least raise an exception and unlock the job, allowing us to investigate instead of just leaving a stranded job that is locked to a process that no longer exists.

Perfomance issue with rake task: Would adding worker dyno resolve the issue?

In our application we are using rake task for sending mails to around 11 000 users. Each email sending is executed as a delayed job as given following.
#Users.each do |a|
a.delay.send_email(body,text)
end
It was working perfect two weeks back and suddenly slowed down. Means it was about to send all that emails in single day but currently it takes time.
We have tried to follow this performance issue but couldn't find anything so far.
1. We investigated the code, tried with single delayed job. Commented out the part taking from db etc. But it is doing in the same time
2. Tried the email sending part commented out. But time taken was same to execute the delayed job.
Later on noticed about the heroku worker process dyno. We have purchased 1 Worker and 2 Webs currently. Is that the reason it is getting delayed. If so how it was previously working? Adding more workers would increase the performance?

Resources