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.
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
|
|
action in controller
1 2 3 |
|
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
|
|
1
|
|
set the below config in application.rb
1
|
|
Now the backup process code explained
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
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
|
|
one issue we have encountered is we have used model.import , which is coming from
1
|
|
it is not added here, need to add the gem and do the bundle install
1
|
|
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
|
|
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
|
|
1 2 3 4 5 6 7 8 |
|
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 |
|
In order to run this recurring job for one time, need to call the rake task using
1
|
|
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
Hooray, my work is done here, feel free to comment if any issue