Skip to content

Commit

Permalink
Added Rails 3 compatible generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Darrin Wortlehock authored and bkeepers committed May 2, 2010
1 parent c96da5f commit 03f2a69
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/generators/delayed_job/delayed_job_generator.rb
@@ -0,0 +1,33 @@
require 'rails/generators'
require 'rails/generators/migration'

class DelayedJobGenerator < Rails::Generators::Base

include Rails::Generators::Migration

def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

# Implement the required interface for Rails::Generators::Migration.
#
def self.next_migration_number(dirname) #:nodoc:
next_migration_number = current_migration_number(dirname) + 1
if ActiveRecord::Base.timestamped_migrations
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
else
"%.3d" % next_migration_number
end
end

def create_script_file
template 'script', 'script/delayed_job', :chmod => 0755
end

def create_migration_file
if defined?(ActiveRecord)
migration_template 'migration.rb', 'db/migrate/create_delayed_jobs.rb'
end
end

end
21 changes: 21 additions & 0 deletions lib/generators/delayed_job/templates/migration.rb
@@ -0,0 +1,21 @@
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |table|
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
table.text :handler # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.timestamps
end

add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
end

def self.down
drop_table :delayed_jobs
end
end
5 changes: 5 additions & 0 deletions lib/generators/delayed_job/templates/script
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize

0 comments on commit 03f2a69

Please sign in to comment.