public

# create delayed_job entries without rails # # based on this http://aaronvb.com/articles/28-recurring-delayed-job-with-cron # enhanced to # take a params hash # read database.yml # delayed_job queue # use: # */30 * * * cd /path/to/current && RAILS_ENV=production /path/to/current/lib/delayed_job_cron_jobs/create_delayed_job.rb --model "MadmimiManager" --method "unsubscribed_update" --queue "general" --arguments "{:days_ago => 7}"

  • Download Gist
enhanced recurring-delayed-job-with-cron
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
require 'rubygems'
require 'mysql'
require 'yaml'
require 'optparse'
 
def usage(s)
$stderr.puts(s)
$stderr.puts("Usage: #{File.basename($0)}: [-l <logfile] [-q] file ...")
exit(2)
end
 
$model = false
$method = nil
$queue = nil
$arguments = {}
loop { case ARGV[0]
when '--model' then ARGV.shift; $model = ARGV.shift
when '--method' then ARGV.shift; $method = ARGV.shift
when '--queue' then ARGV.shift; $queue = ARGV.shift
when '--arguments'
ARGV.shift;
str = ARGV.shift;
 
# convert string to Hash - only non-nested hashes
$arguments = str.gsub(/[{}:]/,'').split(', ').map{|h| h1,h2 = h.split('=>'); {h1.strip.to_sym => h2}}.reduce(:merge)
when /^-/ then usage("Unknown option: #{ARGV[0].inspect}")
else break
end; }
 
def parse_args(args)
if args.empty?
str = '[]\n\n'
else
str = '\n-'
args.each do |k,v|
str += ' :' + k.to_s + ': ' + v.to_s + '\n'
end
end
return str
end
 
RAILS_ENV = ENV['RAILS_ENV'] || 'development' #only used load database.yml
script_folder = File.expand_path(File.dirname(__FILE__))
yaml = YAML::load(File.open(File.join(script_folder, '../../config/database.yml')))[RAILS_ENV]
 
unless yaml
$stderr.puts "no database configuration for #{RAILS_ENV} environment"
exit(-1)
end
 
# Connect to database
# replace these values with your own:
# DB_USER is your database user
# DB_PASSWORD is your database user password
# DATABASE_NAME is your database name, ie: sample_app_development
dbh = Mysql.real_connect(yaml['host'], yaml['username'], yaml['password'], yaml['database'])
# Get the current time in db format
db_time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
# Insert data into table
# replace these values with your own:
# YOUR_MODEL is your model
# YOUR_METHOD is your method, or function
# YOUR_ARGUMENTS are your agrument(s), ie {:nws => 4, :asdf => "haha"}
# leave blank if no arguments
model = $model
model_method = $method
# args = parse_args({})
args = parse_args($arguments)
queue = $queue
if queue
dbh.query("INSERT INTO `delayed_jobs` (`failed_at`, `locked_by`, `created_at`, `handler`, `updated_at`, `priority`, `run_at`, `attempts`, `locked_at`, `last_error`, `queue`) VALUES(NULL, NULL, '#{db_time}', '--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/class \\\'#{model}\\\'\nmethod_name: :#{model_method}\nargs: #{args}', '#{db_time}', 0, '#{db_time}', 0, NULL, NULL, '#{queue}')")
else
dbh.query("INSERT INTO `delayed_jobs` (`failed_at`, `locked_by`, `created_at`, `handler`, `updated_at`, `priority`, `run_at`, `attempts`, `locked_at`, `last_error`, `queue`) VALUES(NULL, NULL, '#{db_time}', '--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/class \'#{model}\'\nmethod_name: :#{model_method}\nargs: #{args}', '#{db_time}', 0, '#{db_time}', 0, NULL, NULL, NULL)")
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.