Run active job method in background or not depends on scenario - ruby-on-rails

I am using active job to run my job in background.But i want to run one job in background in one scenario and run that job not in background in other scenario.Let me know how i can do that?
For example:
def update_name(user_id, name)
user = User.find_by_id(user_id)
user.update_attribute(:name, name)
end
handle_asynchronously :update_name, if is_delayed
In this example i am run update_name method in background if is_delayed is true.If is_delayed is false then update_name will not run in background.So is there anyway in active job to do this?

You can use perform_now or perform_later accordingly.
class UpdateUserNameJob < ActiveJob::Base
queue_as :default
def perform(user_id, name)
user = User.find_by_id(user_id)
user.update_attribute(:name, name)
end
end
Delay job execution based on is_delay value
if is_delayed
UpdateUserNameJob.perform_later(user_id, name)
else
UpdateUserNameJob.perform_now(user_id, name)
end

Related

Sidekiq Job - How can I launch a job and each time change the parameter used?

I would like to launch a job which is going to calculate the points of each user of my web-App.
Here is the problem, I would like to launch it automatically with sidekiq-scheduler.
But I have trouble to understand how I can launch my job with an argument which is going to change. I mean I have to calculate the amount of points for each user, so the argument is going to change and take different user_id.
Here is my code :
class PointsjoueurJob < ApplicationJob
queue_as :default
def perform(user_id)
#user = User.find(user_id)
#playerseason = PlayerSeason.where(user_id: #user.id)
#forecasts = Forecast.where(player_season_id: #playerseason)
points = []
#forecasts.each do |forecast|
if forecast.points_win.present? || forecast.points_lose.present?
if forecast.points_win.present?
points << forecast.points_win
else forecast.points_lose.present?
points << forecast.points_lose
end
#playerseason.update(number_of_points: points.sum)
else
end
end
end
Right now if I want to launch it, I have to go to my console then type :
PointsjoueurJob.perform_now(1)
But I want to schedule this with sidekiq-scheduler. The goal is to trigger the work everyday at 01h00 (cron: '0 1 * * *')but I don't know how to set-up the argument in order for the job to iterate trough all the users.
Thank you by advance.
Assuming that you want to recalculate all users' totals, you can create a separate 'wrapper' job, which is scheduled, that in turn enqueues the individual recalculation jobs:
class RecalcPointsJob < ApplicationJob
queue_as :default
def perform
User.find_each do |u|
PointsjoueurJob.perform_later(u.id)
end
end
end
If you are after a subset of users instead, substitute User.where() or User.find_by().
You can generate a Task and use whenever, then setup it.
on task you can write this:
rails g task test cron
namespace :test do
task :cron do
User.find_each do |u|
PointsjoueurJob.perform_async(u.id)
end
end
end
then in config/schedule.rb after install whenever
every '0 1 * * *' do
rake "test:cron"
end
then
whenever --update-crontab

List of scheduled Active Job in rails

How i can see the list scheduled or pending active job in rails 5.2.
I have Scheduled a job after 10min to send a email but user resend it again and again so i need to see that is job scheduled is executed or not.
My Job look like this
class GenerateTemplatePreviewJob < ApplicationJob
queue_as :default
def perform(*args)
# Do something later
end
end
I Have Called The job from Model
after_save ->{
GenerateTemplatePreviewJob.set(wait_until:
10.second.after).perform_later(self.template.id.to_s)
}

Rails control Job execution

I have a job created with rails g job cleanUp.
Is where any option to check if Job is running? Something like this CleanUpJob.isRunning?
If where is no way to make it without additional gems, which will be the simplest? delayed_job?
Second thing to control Job is progress, any thoughts how to implement CleanUpJob.progress or progress_job should be my choice?
briefly:
I need to create a job with two methods (isRunning?, progress).
I don't really want additional tables if possible.
You can use Class: Sidekiq::ScheduledSet for this purpose.
Documentation here
This Class is used in Sidekiq web interface.
Example:
Save job id (jid) when set job. Then you can call it for queried instance
def is_running?
require 'sidekiq/api'
ss = Sidekiq::ScheduledSet.new
jobs = ss.select {|ret| ret.jid == self.jid}
jobs.any?
end
Or, you can set DB flag inside Job with around_perform hook.
class SomeJob < ActiveJob::Base
queue_as :some_job
around_perform do |job, block|
start_import_process_log job.arguments[0], job.arguments[1] || {}
block.call
finish_import_process_log
end
# ...
private
def start_import_process_log import_process, options={}
#some actions
end
def finish_import_process_log
end
end
In this example associated log record is created.
Or you can use before_perform/ after_perform.
In my practice I'm using creting log records on long tasks.
When I need to find and kill job as example - I'm using Sidekiq::ScheduledSet.

How to perform a background job now?

I want to execute this on background
Product.all.map { |product| product.save }
When I save the product will call a callback to create a new record in a table with the costs of products
I create a job for this, but if I execute perform_now it is not executed on background and perform_later executes long after.
I want to execute this right now but in background. I'm not sure if I can just execute this in a thread too.
I am using Delayed Job, here is the job
class UpdateProductCostsJob < ApplicationJob
queue_as :default
def perform
Product.all.map { |product| product.save }
end
end
And I want to execute every time this model is saved
class CostComposition < ApplicationRecord
before_save :update_product_costs
private
def update_product_costs
UpdateProductCostsJob.perform_now
end
end
Assuming that you are using rails 5, you can create a high priority queue and create the job under that queue. If you have only one queue, you can add the job to the queue as well as specify the time when you want to process that job.
UpdateProductCostJob.set(wait_until: Time.now + 5.minutes).perform_later
refer http://edgeguides.rubyonrails.org/active_job_basics.html#enqueue-the-job

Delayed_Job: accessing job metadata and/or avoiding duplicate jobs

i'm trying to get the run_at datetime in a custom job class. my code looks like this:
class MyCustomJob < Struct.new(:my_object)
def perform
if self.run_at == my_object.start_time
# process the job
end
end
end
i also tried a Delayed::Job.find(self) but had no luck.
thanks in advance
If you define a before method on your custom job, the worker will pass you the delayed job instance before calling perform:
class MyCustomTask
def before(job)
#job = job
end
def perform
# has access to #job object.
# You may need to call #job.reload to see in-flight changes to object in the database.
end
end
You should handle this when you create the job: priority = 0
run_time = my_object.start_time
Delayed::Job.enqueue(MyCustomJob.new(my_object), priority, run_time)
https://github.com/tobi/delayed_job/wiki
If your jobs aren't running at the expected time, you may be scheduling them for UTC:
http://www.gregbenedict.com/2009/08/19/is-delayed-job-run_at-datetime-giving-you-fits/
To check the queue for an existing job - you could do the following:
class MyCustomJob < Struct.new(:object_id)
def self.exists?(object_id)
Delayed::Job.where(['handler = ? and failed_at is null',handler(object_id)]).count(:all) > 0
end
def self.handler(object_id)
"--- !ruby/struct:MyCustomJob \nobject_id: #{object_id}\n"
end
def perform
my_object = MyObject.find(object_id)
my_object.do_stuff
end
end
Then just check for MyCustomJob.exists?(my_object.id) before queueing.
It's a little bit of a hack - edit the handler method as needed. I would modify the delayed_jobs table to have a class/object_id to make for cleaner code and more efficient table scans if your jobs table is large or if you do this with other types of jobs.
This question also looks relevant:
How to cancel scheduled job with delayed_job in Rails?

Resources