Skip to content

Instantly share code, notes, and snippets.

@robhurring
Created December 7, 2010 20:09
Show Gist options
  • Save robhurring/732327 to your computer and use it in GitHub Desktop.
Save robhurring/732327 to your computer and use it in GitHub Desktop.
Delayed Job with Sinatra -- without sinatra specific gems, etc.
#!/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
# 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
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
#!/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
@hainesr
Copy link

hainesr commented Dec 4, 2012

Thanks for this, it really helped.

This pretty much works for the current versions of DelayedJob, etc. The only problem I had is that the active record backend now seems to require the DATABASE_URL environment variable set (which I assume Rails takes care of?). It resets the database connection when running the delayed_job script and relies on DATABASE_URL to re-establish it. If you're just using the rake task you're not affected by this.

In any case it's easily fixed with the rails-database-url gem: https://rubygems.org/gems/rails-database-url

Versions of things I was using (from my Gemfile.lock):

activerecord (3.2.9)
activemodel (3.2.9)
activesupport (3.2.9)
daemons (1.1.9)
delayed_job (3.0.4)
delayed_job_active_record (0.3.3)
rails-database-url (1.0.0)
sinatra (1.3.3)
sinatra-activerecord (1.1.2)

@alexlod
Copy link

alexlod commented Mar 8, 2013

Thanks for this. Just a heads up, I didn't need the following:

# 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

Running activerecord 3.2.12, delayed_job 3.0.3, and sinatra 1.3.4.

@inakidelamadrid
Copy link

@robhurring what if you are running the app with bundler? when I try to bundle exec

bundle exec ruby bin/delayed_job  run
delayed_job: process with pid 25196 started.
ActiveRecord::AdapterNotSpecified

How can I get this working?

@anupama-agarwal
Copy link

@inakidelamadrid did u get this working? I am facing the same problem

@diedthreetimes
Copy link

diedthreetimes commented Dec 19, 2021

FWIW, in 2021 without a hardcoded db configuration file rails-database-url is not sufficient.

What I needed (along with copying the generated migrationn and worker script) was to add :

ENV["RAILS_ENV"] ||= ENV['SINATRA_ENV']

to either my environment file (dj-sinatra.rb), or the delayed job worker script (delayed_job.rb).

This is largely due to the changes in active_record (currently using 5.2). Virtually no other changes were needed, when using the sinatra-activerecord gem. The only other optional change is adding rake tasks by using this one block in the projects Rakefile

require 'delayed/tasks.rb'

task :environment do
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment