How to access Delayed Job instance inside Active Job - Rails 4.2 - ruby-on-rails

I'm using ActiveJob with delayed_job (4.0.6) in the background and I want to find a scheduled job to deleted it.
For instance, if I have
class MyClass
def my_method
perform_stuff
MyJob.set(wait: 1.month.from_now).perform_later(current_user)
end
end
Then, if I edit MyClass instance and call my_method again, I want to cancel that job and schedule a new one.
As suggested in this post http://www.sitepoint.com/delayed-jobs-best-practices, I added two columns to the Delayed Job Table:
table.integer :delayed_reference_id
table.string :delayed_reference_type
add_index :delayed_jobs, [:delayed_reference_id], :name => 'delayed_jobs_delayed_reference_id'
add_index :delayed_jobs, [:delayed_reference_type], :name => 'delayed_jobs_delayed_reference_type'
So this way I may find a delayed Job and destroy it. But I wanted to do that inside a ActiveJob class, to maintain the pattern of jobs in my project.
I wanted to do something like:
class MyJob < ActiveJob::Base
after_enqueue do |job|
user = self.arguments.first
job.delayed_reference_id = user.id,
job.delayed_reference_type = "User"
end
def perform(user)
delete_previous_job_if_exists(user_id)
end
def delete_previous_job_if_exists(user_id)
Delayed::Job.find_by(delayed_reference_id: 1, delayed_reference_type: 'User').delete
end
end
But that doesn't work.
Anyone had this kind of issue?

Two changes:
1. updated the after_enqueue callback so that you can update the
delayed_jobs table directly
2. fixed a typo where delayed_reference_id was hard coded as 1
This should work:
class MyJob < ActiveJob::Base
after_enqueue do |job|
user = self.arguments.first
delayed_job = Delayed::Job.find(job.provider_job_id)
delayed_job.update(delayed_reference_id:user.id,delayed_reference_type:'User')
end
def perform(user)
delete_previous_job_if_exists(user.id)
end
def delete_previous_job_if_exists(user_id)
Delayed::Job.find_by(delayed_reference_id: user_id, delayed_reference_type: 'User').delete
end
end

If you want to access the Delayed::Job from your worker, in an initializer, you can monkey patch the JobWrapper class.
module ActiveJob
module QueueAdapters
class DelayedJobAdapter
class JobWrapper
def perform(job)
end
end
end
end
end

Related

Ruby/Rails worker to create or update record if exists - avoid DRY

I've got worker to store data from incoming webhooks. Now I want to use this worker to update existing data if this data already exists.
class StoreActivityWorker
include Sidekiq::Worker
def perform(webhook)
Activity.create!(
cms_activity_id: webhook.dig('entity', 'id').to_i,
is_separate_activity: webhook.dig('entity', 'attributes', 'is_separate_activity'),
content_full: retrieve_full_content(webhook),
content_basic: retrieve_basic_content(webhook),
)
end
end
Because the webhook does not show which records have been updated the UpdateActivityWorker will be the same (update all fields):
class UpdateActivityWorker
include Sidekiq::Worker
def perform(webhook)
Activity.where(cms_activity_id: webhook[:entity][:id]).update(
cms_activity_id: webhook.dig('entity', 'id').to_i,
is_separate_activity: webhook.dig('entity', 'attributes', 'is_separate_activity'),
content_full: retrieve_full_content(webhook),
content_basic: retrieve_basic_content(webhook),
)
end
end
As you see it's not inline with DRY. Is there any way to avoid repeats that code?
Take a look at find_or_initialize_by method:
class StoreActivityWorker
include Sidekiq::Worker
def perform(webhook)
entity = webhook.dig 'entity'
activity = Activity.find_or_initialize_by(cms_activity_id: entity['id'])
activity.update!(
is_separate_activity: entity.dig('attributes', 'is_separate_activity'),
content_full: retrieve_full_content(webhook),
content_basic: retrieve_basic_content(webhook),
)
end
end

ActiveJob not enqueuing job

I've implemented Ahoy for tracking events inside my application and I want to move the processing into a background job.
My setup works fine when I call perform_now (the job gets processed), but changing to perform_later doesn't work. From the logs, it seems the job doesn't even get enqueued.
config/initializers/ahoy.rb
class Ahoy::Store < Ahoy::BaseStore
def track_visit(data)
AhoyTrackVisitJob.perform_later(#options, data)
end
def track_event(data)
AhoyTrackEventJob.perform_later(#options, data)
end
end
app/jobs/ahoy_track_event_job.rb
class AhoyTrackEventJob < ApplicationJob
queue_as :default
def perform(options, data)
Ahoy::DatabaseStore.new(options).track_event(data)
end
end
I've tried with both Sidekiq and SuckerPunch:
development.rb
config.active_job.queue_adapter = :sidekiq

Rails 4.2 get delayed job id from active job

Any idea how to get the Delayed::Job id from the ActiveJob enqueuing? When I enqueue a job I get back an instance of ActiveJob::Base with a #job_id, but that job id seems to be internal to ActiveJob. My best guess so far is just to walk down the most recently created jobs:
active_job_id = GenerateReportJob.perform_later(self.id).job_id
delayed_job = Delayed::Job.order(id: :desc).limit(5).detect do |job|
YAML.load(job.handler).job_data['job_id'] == active_job_id
end
but that seems all kinds of hacky. Kind of surprised ActiveJob isn't returning the ID from Delayed::Job, especially since that is what is explicitly returned when the job gets enqueued.
== EDIT
Looks like I'm not the only one (https://github.com/rails/rails/issues/18821)
In case anyone finds this in the future: Rails just accepted a patch to allow you to get this id from provider_job_id in Rails 5. You can get it to work with a patch like
ActiveJob::QueueAdapters::DelayedJobAdapter.singleton_class.prepend(Module.new do
def enqueue(job)
provider_job = super
job.provider_job_id = provider_job.id
provider_job
end
def enqueue_at(job, timestamp)
provider_job = super
job.provider_job_id = provider_job.id
provider_job
end
end)
Inspired by the answer of Beguene and some reverse engineering of the Rails 5 ActiveJob code, I have made it work with Rails 4.2 by
1) adding following code in lib/active_job/queue_adapters/delayed_job_adapter.rb or config/initializers/delayed_job.rb (both locations have worked):
# file: lib/active_job/queue_adapters/delayed_job_adapter.rb
module ActiveJob
module Core
# ID optionally provided by adapter
attr_accessor :provider_job_id
end
module QueueAdapters
class DelayedJobAdapter
class << self
def enqueue(job) #:nodoc:
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name)
job.provider_job_id = delayed_job.id
delayed_job
end
def enqueue_at(job, timestamp) #:nodoc:
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, run_at: Time.at(timestamp))
job.provider_job_id = delayed_job.id
delayed_job
end
end
class JobWrapper #:nodoc:
attr_accessor :job_data
def initialize(job_data)
#job_data = job_data
end
def perform
Base.execute(job_data)
end
end
end
end
end
The attr_accessor :provider_job_id statement is needed in Rails 4.2, since it is used in the enqueue method and is not yet defined in 4.2.
Then we can make use of it like follows:
2) define our own ActiveJob class:
# file: app/jobs/my_job.rb
class MyJob < ActiveJob::Base
queue_as :default
def perform(object, performmethod = method(:method))
# Do something later
returnvalue = object.send(performmethod)
returnvalue
end
end
end
3) Now we can create a new job anywhere in the code:
job = MyJob.perform_later(Myobject, "mymethod")
This will put the method Myobject.mymethod into the queue.
4) The code in 1) helps us to find the Delayed Job that is associated with our job:
delayed_job = Delayed::Job.find(job.provider_job_id)
5) finally, we can do, whatever we need to do with the delayed_job, e.g. delete it:
delayed_job.delete
Note: in Rails 5, step 1) will not be needed anymore, since the exact same code is integral part of Rails 5.
I made it work in Rails 4.2 using the new patch from Rails 5 like this:
create the file lib/active_job/queue_adapters/delayed_job_adapter.rb
module ActiveJob
module QueueAdapters
class DelayedJobAdapter
class << self
def enqueue(job) #:nodoc:
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name)
job.provider_job_id = delayed_job.id
delayed_job
end
def enqueue_at(job, timestamp) #:nodoc:
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, run_at: Time.at(timestamp))
job.provider_job_id = delayed_job.id
delayed_job
end
end
class JobWrapper #:nodoc:
attr_accessor :job_data
def initialize(job_data)
#job_data = job_data
end
def perform
Base.execute(job_data)
end
end
end
end
end
Instead of removing the job from the queue if it is cancelled you could model a cancellation of the job itself.
Then, when you come to run the GenerateReportJob you can first check for a cancellation of the report. If there is one then you can destroy the cancellation record and drop out of the report generation. If there is no cancellation then you can carry on as normal.

rails - Delayed jobs without perform method

After make a big class with many method, I want all of these method be called in a delayed jobs.
But the practice of the Delayed::job, is that you have to create a class with a perform method, like that :
class Me < Struct.new(:something)
def perform
puts "GO"
end
end
and call it like :
Delayed::Job.enqueue Me.new(1)
But the problem is that my class as already many method like this type
class NameHandler
def self.init
ap "TODO : Delayed jobs "
end
def self.action_one
...
end
def self.action_two
...
end
etc.
end
and I want to call it like :
Delayed::Job.enqueue NameHandler.action_one params...
Theres is an best practice for that ? Or I have to follow the classic Delayed::job way and lose many times ?
In the README it has a number of ways:
Me.new.delay.action_one
or
class NameHandler
handle_asynchronously :action_one
def action_one
end
def self.action_one
new.action_one
end
end
NameHandler.action_one

Rails - Delayed job completion

I have a web application based on ruby on rails, which uses this delayed job gem.
I have a function that triggers a delayed job which in turn triggers several other delayed jobs. Is there a way, nest case being an event that can indicate that all the jobs related to a parent have completed? Or do I just have to work on whatever data is available when I try to retrieve docuemnts ?
Example:
def create_vegetable
#..... creates some vegetable
end
def create_vegetable_asynchronously id
Farm.delay(priority: 30, owner: User.where("authentication.auth_token" => token).first, class_name: "Farm", job_name:create "create_vegetable_asynchronously").create_vegetable(id)
end
def create_farm_asynchronously data
data.each do |vegetable|
create_vegetable_asynchronously vegetable.id
end
end
handle_asynchronously :create_farm_asynchoronously
maybe a little overkill, but you can explicitly organize your jobs hierarchically and employ before hook to pass on the parent job id to subtasks.
something like:
class Job < Delayed::Job
belongs_to :parent, class: 'Job' #also add db column for parent_id
end
class CreateVegetable
def initialize(id)
#id = id
end
def perform
Farm.create_vegetable(id)
end
end
class CreateFarm
def initialize(vegetable_ids,owner_id)
#vegetable_ids = vegetable_ids
#owner_id = owner_id
end
def before(job)
#job_id = job.id
end
def perform
#vegetable_ids.each { |id| Job.enqueue CreateVegetable.new(id), priority: 30, owner_id = #owner_id, :parent_id = #job_id }
end
end
Then when you launch create farm, somehow remember the job id.
def create_farm_asynchronously data
owner_id = User.where("authentications.auth_token" => token).first.id
#parent = Job.enqueue CreateFarm.new(data.map(&:id), owner_id)
end
then you can check sub-jobs by:
Job.where(parent_id: #parent.id)

Resources