Awesome Web Blog

Delayed Job With Recurring Option

| Comments

delayed job recurring

Requirement

I have an app called clone app and the user will update some information to the table call user_informations. Our requirement is to copy this data to another table user_information_backups. It will be a background job. But we dont want to run this when the user updated the user_informations, instead what we are planning is to do the backup in each 5 mins. when the backup is completed, mark the cloned one master as flagged to restrict one information to be taken more than one.

app link

1. create an app user_clone

used the database as mysql in dev, since I have sqlyog to installed for proper UI View and understanding.

database is mysql , we can understand from the gemfile.

my database.yml file is attached below

Need to create the database

now the database is shown in the sqlyog tool

create the model UserInformation

update the user_informations migration

migrated the file

created the model UserInformationBackup

display the table in sqlyog

Now, we need to generate the controller

Do the crud operations.

controller code is attached here below,

routes file

index page

new Page

update the migration to fetch only the unselected ones

is_selected to user_informations , default value: false

add a custom action in controller and route

1
  get 'users/display_backup', to: "users#display_backup"

action in controller

1
2
3
  def display_backup
    @users = UserInformation.all.where(is_selected: false)
  end

create the view part for the action

Now, the Application part completed, moving to our delayed_job and its works.

Delayed Job

install delayed_job

1
rails generate delayed_job:active_record

1
rails db:migrate

set the below config in application.rb

1
  config.active_job.queue_adapter = :delayed_job

Now the backup process code explained

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  def self.intiate_backup
    user_details = UserInformation.fetch_details
    if user_details.present?
      instance_data = []
      user_details.each do |user_data|
        user_backup_instance = UserInformationBackup.new
        user_backup_instance.first_name = user_data.first_name
        user_backup_instance.last_name = user_data.last_name
        user_backup_instance.secret_message =user_data.secret_message
        instance_data << user_backup_instance
      end
      UserInformationBackup.import instance_data
      user_details.update_all(is_selected: true)
      end
    end
  end

now test this process via Delayed job, we can use the rails console to check this

now the entry got added, we can this delayed job using the command

1
  bundle exec rails jobs:work

one issue we have encountered is we have used model.import , which is coming from

1
gem 'activerecord-import'

it is not added here, need to add the gem and do the bundle install

1
  gem 'activerecord-import', '~> 0.23.0'

and do the same same as via console and restart the delayed job

hooray , our process is done, now it will be cloned to the user_information_backups table

user_informations table

user_information_backups table

now 2 images got the same, we can’t see any difference. in order to check the difference we will create a new entry to the user_informations table via UI

I have added jayakrishnan as a new entry, since this data is not cloned by the user_information_backups, it will not be there.

Now we run the DJ via console and see the magic,

Now it is the time for our cookery show, we will do the delayed job recurring.

add below gem to the gemfile.

1
  gem 'delayed_job_recurring', '~> 0.3.7'

and do the bundle install

create a folder in app/interactors.

create a file in that, we can use any file name for example, created a file name clone_process.rb in

1
  app/interactors
1
2
3
4
5
6
7
8
  class CloneProcess
    include Delayed::RecurringJob
    run_every 5.minutes

    def perform
      UserInformationBackup.intiate_backup
    end
  end

Now, this class needs to be called for one time, for reuse, we can use the rakes; path: lib/tasks/recurring.rake

1
2
3
4
5
namespace :recurring do
  task init: :environment do
    CloneProcess.schedule!
  end
end

In order to run this recurring job for one time, need to call the rake task using

1
   bundle exec rails recurring:init

Now

An entry is being made up in the DJ and it will be updated by the delayed_job_recurring gem

in order to test this we will create a new data via UI and wait for the DJ to pickup

but this data is not here in the clone table

after 5 minutes, DJ will pick up this.

in UI,

visit the offical doc for further details

official doc

Hooray, my work is done here, feel free to comment if any issue

Comments