Question

I'd like to schedule a delayed job that contains an active record query (or conditions) that will be evaluated when the job is actually run.

Ex. I have a custom Job that is generating and sending notifications to users. I want to send parameters/query/conditions to the job that would filter the users that notifications get sent to.

I could eval a string but that seems so ugly.

The project is using Rails 2.3.5 so I can't go the Arel route.

I see stuff about Ambition but nothing since 2008 so I'm not sure the status of the project.

Suggestions?

Was it helpful?

Solution

You could send the conditions etc as a hash to the job when you create it and then use it just like you normally do with ActiveRecord. Perhaps something like this:

class NotificationJob < Struct.new(:message, :query)

  def perform
    @users = User.all(query)
    ...
  end

end

And then you create the Job like this:

query = {:conditions => ["users.company_id = ?", @company.id]}
Delayed::Job.enqueue(NotificationJob.new("There is no cake",query), 0, Time.now)

If this is not working for you, then perhaps you can supply your current code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top