Share This Article
If you want to use a job, you can use Resque in Rails. You should require the “resque” gem in your Gemfile.
gem "resque"
Then you can create a new job by subclassing the Resque::Job class.
require "resque"
class MyJob < Resque::Job
def self.perform
p "Doing some work..."
end
end
Enqueue a Job
Once you’ve defined your job, you can enqueue it using the Resque.enqueue
method.
Resque.enqueue(MyJob)
You can also pass arguments to your job if you need to.
Resque.enqueue(MyJob, "arg1", "arg2")
These arguments will be available inside the perform
method as method arguments.
require "resque"
class MyJob < Resque::Job
def self.perform(arg1, arg2)
p "Doing some work with #{arg1} & #{arg2}..."
end
end
Resque.enqueue(MyJob, "arg1", "arg2")
Run a Job Now
When you enqueue a Resque job, it will be added to a queue and executed as soon as a worker is available.
But sometimes, you might want to run a job immediately instead of waiting for a worker.
This can be done using the Resque
class and the #perform
method.
require "resque"
class MyJob < Resque::Job
def self.perform
p "Doing some work..."
end
end
MyJob.perform
Execute a Job in a Separate Thread
You can execute it in a separate thread if you don’t want to wait for the job to finish.
This can be done using the Thread
class.
require "resque"
class MyJob < Resque::Job
def self.perform
p "Doing some work..."
end
end
Thread.new { MyJob.perform }
Execute a Job at a Specific Time
If you want to execute a job at a specific time, you can use the Resque.enqueue_at
method.
Resque.enqueue_at(Time.now + 10.seconds, MyJob)
Scheduling Jobs with Cron
You can use crontab to schedule your jobs.
You need to add a new line in your crontab file to do this.
30 21 * * * rake resque:schedule
This will run the rake resque:schedule
command every day at 9:30 PM.
Scheduling Jobs with the Whenever Gem
The Whenever gem makes it easy to schedule jobs using cron.
It also has a friendly DSL for setting up your appointments.
First, you need to add the whenever gem to your Gemfile.
gem "whenever"
Then you need to create the config/schedule.rb
file.
require "resque/scheduler"
set :environment, :development
env :PATH, ENV['PATH']
job_type :job, "cd :path && :environment_variable=:environment bundle exec rake :task --silent :output"
every 1.day, :at => '2:30 am' do
job "resque:schedule"
end
Finally, you need to run the whenever --update-crontab
command to generate the crontab file and schedule your jobs.
Passing Variables to Your Job
If you want to pass variables to your job, you need to overwrite the perform
method.
require "resque"
class MyJob < Resque::Job
def self.perform(name, age)
p "Hi #{name}, you are #{age}"
end
end
Resque.enqueue(MyJob, "John", 21)
Running a Job in the Background
Resque also has a perform_in
and perform_at
method, which can run a job in the background.
require "resque"
class MyJob < Resque::Job
def self.perform
p "Doing some work..."
end
end
Resque.enqueue_in(10.seconds, MyJob)
Resque.enqueue_at(Time.now + 10.seconds, MyJob)
With perform_in
the job will be executed in 10 seconds, and with perform _at
the job will be completed at the specified time.
You can also pass arguments to these methods.
require "resque"
class MyJob < Resque::Job
def self.perform(name, age)
p "Hi #{name}, you are #{age}"
end
end
Resque.enqueue_in(10.seconds, MyJob, "John", 30)
Resque.enqueue_at(Time.now + 10.seconds, MyJob, "John", 30)
Running a Job on a Specific Queue
If you want to run a job on a specific queue, you can use the Resque.enqueue_to
method.
require "resque"
class MyJob < Resque::Job
def self.perform
p "Doing some work..."
end
end
Resque.enqueue_to("some_queue", MyJob)
You can also pass arguments to this method.
require "resque"
class MyJob < Resque::Job
def self.perform(name, age)
p "Hi #{name}, you are #{age}"
end
end
Resque.enqueue_to("some_queue", MyJob, "John", 30)
Conclusion
In this article, you’ve learned how to enqueue, schedule, and run jobs using the Resque gem.