Rails for Beginners Part 34: Background Job for Posting Tweets Discussion
Hi Chris,
if I post Tweet with publish_at at 8am and reschedule it to 9am wouldn't have the first job Tweet instance with 8am?
Shouldn't I send to job only an id and find actual Tweet inside the job with latest publish_at to check?
When you re-schedule a Tweet, the existing Tweet is being updated. You do not create another Tweet in your DB when you re-schedule. So there will be only one Tweet in your database. Since there will only be one Tweet you can not check depending on publish_at
.
What happens to the job if I schedule a tweet and then delete it? Is the job associated with it also deleted? Or the job still runs but as there is no body, no tweet is published?
What is the best way to handle a job after destroying a tweet ? Should there be an after delete hook called?
Can someone please shed light on this, thanks
That's an excellent question! I had never thought about it and that made me wonder as well. So I've tried that with the app I'd created by watching this tutorial.
Basically if you schedule a Tweet Job and then delete the Tweet then you basically get an error saying
2021-03-21T18:26:02.805921+00:00 app[worker.1]: pid=4 tid=f60 WARN: ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find Tweet with 'id'=4
That means the job is still being executed but since the Tweet is deleted you get an error.
Below you can find the details about the job when I check the Sidekiq::RetrySet
.
=> #<Sidekiq::SortedEntry:0x0000564c2ef9d410 @args=nil, @value="{\"retry\":true,\"queue\":\"default\",\"class\":\"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper\",\"wrapped\":\"TweetJob\",\"args\":[{\"job_class\":\"TweetJob\",\"job_id\":\"57111c77-cc0f-42d1-b64d-e9834b6ad3dc\",\"provider_job_id\":null,\"queue_name\":\"default\",\"priority\":null,\"arguments\":[{\"_aj_globalid\":\"gid://scheduled-tweets/Tweet/4\"}],\"executions\":0,\"exception_executions\":{},\"locale\":\"en\",\"timezone\":\"UTC\",\"enqueued_at\":\"2021-03-21T18:25:04Z\"}],\"jid\":\"e2c13d0784ef40334478e95c\",\"created_at\":1616351104.086762,\"enqueued_at\":1616351294.922475,\"error_message\":\"Error while trying to deserialize arguments: Couldn't find Tweet with 'id'=4\",\"error_class\":\"ActiveJob::DeserializationError\",\"failed_at\":1616351162.804378,\"retry_count\":3,\"retried_at\":1616351294.929358}", @item={"retry"=>true, "queue"=>"default", "class"=>"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper", "wrapped"=>"TweetJob", "args"=>[{"job_class"=>"TweetJob", "job_id"=>"57111c77-cc0f-42d1-b64d-e9834b6ad3dc", "provider_job_id"=>nil, "queue_name"=>"default", "priority"=>nil, "arguments"=>[{"_aj_globalid"=>"gid://scheduled-tweets/Tweet/4"}], "executions"=>0, "exception_executions"=>{}, "locale"=>"en", "timezone"=>"UTC", "enqueued_at"=>"2021-03-21T18:25:04Z"}], "jid"=>"e2c13d0784ef40334478e95c", "created_at"=>1616351104.086762, "enqueued_at"=>1616351294.922475, "error_message"=>"Error while trying to deserialize arguments: Couldn't find Tweet with 'id'=4", "error_class"=>"ActiveJob::DeserializationError", "failed_at"=>1616351162.804378, "retry_count"=>3, "retried_at"=>1616351294.929358}, @queue="default", @score=1616351458.9293838, @parent=#<Sidekiq::RetrySet:0x0000564c2ef7a4d8 @name="retry", @_size=1>>
My tweets don't publish on their scheduled date & time. Can anyone please help me with this issue? Everything else seems fine - this is the only issue i'm facing.