TestJob.perform(argument) # perform sync
Resque.enqueue(TestJob, argument)
Resque.enqueue_in(5.days, TestJob, argument)
Resque.enqueue_at(5.days.from_now, TestJob, argument)
Resque::Failure.all
# config/application.rb | |
module AppName | |
class Application < Rails::Application | |
config.active_job.queue_adapter = :resque | |
end | |
end |
require 'capistrano-resque' |
# config/deploy.rb | |
set :resque_log_file, 'log/resque.log' | |
set :workers, {main: 2} | |
namespace :deploy do | |
after :finishing, 'resque:restart' | |
after :finishing, 'resque:scheduler:restart' | |
end |
gem 'foreman' # to use command 'foreman start' and run all servers, using Procfile | |
gem 'resque' | |
gem 'resque-scheduler' | |
group :development do | |
gem 'capistrano-resque' | |
end |
web: rails s -b 0.0.0.0 -p 3000 | |
worker: COUNT=1 QUEUE=* rake environment resque:work | |
scheduler: rake environment resque:scheduler |
# lib/tasks/resque.rake | |
require 'resque/tasks' | |
require 'resque/scheduler/tasks' | |
task 'resque:setup' => :environment do | |
ENV['QUEUE'] ||= '*' | |
# for redistogo on heroku http://stackoverflow.com/questions/2611747/rails-resque-workers-fail-with-pgerror-server-closed-the-connection-unexpectedl | |
# Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection } | |
end |
# config/initializers/resque.rb | |
require 'resque' | |
require 'resque/server' | |
require 'resque-scheduler' | |
Resque.redis = 'localhost:6379' #TODO: move to config | |
Resque.redis.namespace = "resque:app-name-#{Rails.env.to_s}" | |
Resque.schedule = YAML.load_file(Rails.root.join('config', 'resque_scheduler.yml')) |
# config/resque_scheduler.yml | |
# Cron explained | |
# | |
# ┌────────────────── minute (0-59) | |
# │ ┌──────────────── hour (0-23) | |
# │ │ ┌────────────── day (month) (1-31) | |
# │ │ │ ┌──────────── month (1-12) | |
# │ │ │ │ ┌────────── day (week) (1-45) | |
# * * * * * | |
# | |
# values: | |
# "*" - every (minute/hour/day/month) | |
# "," - seperator for multiple values (1,2 = every time minute/hour/day/month is 1 & 2) | |
# "-" - range (1-3 = every time minute/hour/day/month is from 1 to 3) | |
# "/" - step (*/5 - every half minute/hour/day/month) | |
test_job_every_1min_using_cron: | |
cron: "* * * * *" | |
class: TestJob | |
queue: scraper | |
args: [1] | |
description: "It's a simple job." | |
test_job_every_5sec: | |
every: "5s" | |
queue: scraper | |
class: TestJob | |
args: [5] | |
description: "It's a simple job." | |
test_job_every_30sec_with_named_variables: | |
every: "30s" | |
queue: scraper | |
class: TestJob | |
args: | |
- foo: 1 | |
bar: 2 | |
description: It's a simple job. | |
named_args: true |
# config/routes.rb | |
Rails.application.routes.draw do | |
mount Resque::Server.new, at: "/jobs" | |
end |
# app/jobs/test_job.rb | |
class TestJob | |
@queue = :scraper | |
def perform(id) | |
sleep(2) | |
end | |
end |