Delayed_jobs save completed tasks

Is there a parameter that I can pass delayed_job that will prevent the removal of completed jobs from the delayed_jobs table?

sort of like destroy_failed_jobs , but for completed jobs

any ideas?

+8
ruby-on-rails delayed-job
source share
3 answers

This is not true. From README: https://github.com/tobi/delayed_job

By default, it will delete failed jobs (and it will always delete successful jobs). If you want to save failed jobs, set Delayed :: Job.destroy_failed_jobs = false. Failed jobs will be flagged with a non-zero value of fail_at.

You will probably need to connect to his destruction method so that he copies the task to another separate table that stores a list of completed tasks for you or simply records which tasks were completed in a file if the log is all you need.

What you don't want is to leave jobs in the delayed_jobs table for several reasons. Firstly, because delayed_jobs uses this table as a TODO list - you want these to be just the things that still need to be done. Secondly, if you hacked it to save all tasks in one table, then the delayed_jobs table only grows, which will slow down the processing of delayed_jobs over time, since a query to search for tasks that do not yet have to filter out those that have .

+4
source share

Extension of answer @ jefflunt.

I added a migration to create a table containing completed tasks

 class CreateCompletedJobs < ActiveRecord::Migration def change create_table :completed_jobs do |t| t.integer "priority", :default => 0 t.integer "attempts", :default => 0 t.text "handler", :limit => 2147483647 t.datetime "run_at" t.datetime "completed_at" t.string "queue" t.timestamps end end end 

Then the module

 class CompletedJob < ActiveRecord::Base end 

Finally, a hook has been added to the task I want to save

  def success job save_completed_job job end private def save_completed_job job CompletedJob.create({ priority: job.priority, attempts: job.attempts, handler: job.handler, run_at: job.run_at, completed_at: DateTime.now, queue: job.queue }) end 

Since I have more than one task, I put the success method in the module and included it in all the tasks that I would like to save. (Note: some of them should not be stored)

+6
source share

You can use something like acts_as_paranoid .

It basically works as follows:

  • #destroy is overwritten so records are not deleted from the database, but deleted_at is set instead
  • the default scope is set, so .find (and all its variants) do not return items from the database where the deleted_at label is set

Here is an introduction: http://technicalpickles.com/posts/embracing-your-paranoia-with-acts_as_paranoid/

Beware: act_as_paranoid is very old and possibly better solutions, so check out https://www.ruby-toolbox.com/categories/Active_Record_Soft_Delete

0
source share

All Articles