Created
March 23, 2013 15:14
-
-
Save jshow/5228049 to your computer and use it in GitHub Desktop.
# 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 "MadmimiMan…
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment