Created
December 7, 2010 20:09
-
-
Save robhurring/732327 to your computer and use it in GitHub Desktop.
Delayed Job with Sinatra -- without sinatra specific gems, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# make this mirror your config/environment files | |
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'dj-sinatra')) | |
require 'delayed/command' | |
Delayed::Command.new(ARGV).daemonize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Caressing DelayedJob to work with Sinatra. | |
# I'm not sure if there is a gem for this or not, but i'm not a big fan | |
# of installing gems if i don't have to -- so heres what i found to be the | |
# bare foundation necessary to get delayed jobs running with sinatra | |
# | |
# 1. ./bin/delayed_job -- taken from the DJ gem's generator template but | |
# modified to point to this "environment" file rather than lib/environment | |
# 2. log/delayed_job.log -- DJ needs this | |
# 3. tmp/pids -- DJ needs this | |
# 4. Rails.logger -- DJ needs this | |
# 5. RAILS_ROOT -- DJ needs this constant defined | |
# 6. Delayed::Worker.backend = :active_record (or Delayed::Worker.guess_backend to guess) | |
# 7. migration file -- can take this from github or the gem's generator templates | |
# | |
# Debugging / Starting: | |
# | |
# 1. ./bin/delayed_job run | |
# 2. irb -r ./dj-sinatra.rb | |
# 3. tail -f log/delayed_job.log | |
# | |
# Console: | |
# | |
# Delayed::Job.enqueue SuccessfulJob.new # => should be clean in the logs/db table | |
# Delayed::Job.enqueue FailureJob.new # => should show failure message, etc. | |
# | |
# Author:: Rob Hurring | |
# Date:: 2012-12-7 | |
# | |
# I know AR/AS/DJ are out of date, but the project that needed this was running on a | |
# non-rails-3 server. either way, thats not the point :) | |
require 'rubygems' | |
gem 'sinatra' | |
gem 'activesupport', '~> 2.3.8' | |
gem 'activerecord', '~> 2.3.8' | |
gem 'delayed_job', '= 2.0.3' | |
require 'sinatra' | |
require 'logger' | |
require 'active_support' | |
require 'active_record' | |
require 'delayed_job' | |
# Global app logger | |
Log = Logger.new(File.expand_path('../log/app.log', __FILE__)) | |
# DelayedJob wants us to be on rails, so it looks for stuff | |
# in the rails namespace -- so we emulate it a bit | |
module Rails | |
class << self | |
attr_accessor :logger | |
end | |
end | |
Rails.logger = Log | |
ActiveRecord::Base.logger = Log | |
# this is used by DJ to guess where tmp/pids is located (default) | |
RAILS_ROOT = File.expand_path('..', __FILE__) | |
# Configure DelayedJob | |
Delayed::Worker.backend = :active_record | |
Delayed::Worker.destroy_failed_jobs = true | |
Delayed::Worker.sleep_delay = 5 | |
Delayed::Worker.max_attempts = 5 | |
Delayed::Worker.max_run_time = 5.minutes | |
# for brevity i'm not including the migration here | |
# you can figure that out from the README on github | |
# this is an existing project w/ a delayed_jobs file | |
ActiveRecord::Base.establish_connection({ | |
:adapter => 'mysql2', | |
:host => 'localhost', | |
:username => 'root', | |
:password => '', | |
:database => 'your_database_with_a_delayed_jobs_table', | |
}) | |
# Jobs to test | |
class SuccessfulJob | |
def perform | |
true | |
end | |
end | |
class FailureJob | |
def perform | |
raise "Failed!" | |
end | |
end | |
########### START OF RAD SINATRA APP ########### | |
get '/' do | |
"blah blah blah blah" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task :environment do | |
require './dj-sinatra' | |
end | |
namespace :jobs do | |
desc "Clear the delayed_job queue." | |
task :clear => :environment do | |
Delayed::Job.delete_all | |
end | |
desc "Start a delayed_job worker." | |
task :work => :environment do | |
Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY']).start | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# run from djsinatra folder | |
mkdir -p tmp/pids log bin | |
chmod -R a+w tmp/pids log | |
touch log/{app,delayed_job}.log | |
# place delayed_job.rb script in ./bin/delayed_job |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With
ActiveRecord >= 6.0
the above is no longer enough. Easiest solution is to apply this fix.