-
-
Save robhurring/732327 to your computer and use it in GitHub Desktop.
| #!/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 |
@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?
@inakidelamadrid did u get this working? I am facing the same problem
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
With ActiveRecord >= 6.0 the above is no longer enough. Easiest solution is to apply this fix.
Thanks for this. Just a heads up, I didn't need the following:
Running activerecord 3.2.12, delayed_job 3.0.3, and sinatra 1.3.4.