rails / solid_queue

Database-backed Active Job backend
MIT License
1.65k stars 91 forks source link

Cron tasks can give weird errors if enqueue fails #251

Open minamete opened 2 weeks ago

minamete commented 2 weeks ago

I was attempting to have a recurring job in cron, and I kept on getting errors with no trace back in the SolidQueue service:

SolidQueue-0.3.1 Skipped recurring task – already dispatched (671.0ms)  task: :index_records, at: 2024-06-26 19:54:00 UTC
SolidQueue-0.3.1 Error in thread (0.0ms)  error: "NoMethodError undefined method `provider_job_id' for false:FalseClass"

After a bit of investigation, it turns out that if perform_later returns false for a cron job, this will be the error displayed. Similarly, if I make the job not use SolidQueue, it will display an error that job_id is undefined for false:FalseClass. I believe, but I'm not sure that it can happen if a recurring job is attempted to be enqueued while it's already running, or if multiple instances of SolidQueue are trying to enqueue the same recurring job at the same time.

Would it be a good idea to give a more descriptive error in the case that this happens?

rosa commented 2 weeks ago

Huh, yes, this seems wrong. I'll look into it, it shouldn't crash in that way.

minamete commented 1 week ago

I believe here are some of the culprits:

lib/solid_queue/dispatcher/recurring_task.rb line 41: for a cron job that is not run using SolidQueue that returns false

lib/solid_queue/dispatcher/recurring_task.rb line 68 into app/models/solid_queue/recurring_execution.rb line 11: for a cron job that is run using SolidQueue that returns false

rosa commented 1 week ago

@minamete, indeed! I realised the problem: I need to provide a block to perform_later to ensure I get the instance of the job class enqueued, my bad.

I'll work on a fix shortly.

rosa commented 1 week ago

In the end I went for something simpler in #252, I think that should do it.