Skip to content

Instantly share code, notes, and snippets.

@aaronvb
Created October 28, 2010 03:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronvb/650570 to your computer and use it in GitHub Desktop.
Save aaronvb/650570 to your computer and use it in GitHub Desktop.
This will insert a job into the delayed_job queue outside of Ruby on Rails environment. Useful for recurring jobs using cron.
#!/usr/bin/env ruby
# put this somewhere in your project, ie: /lib
# use:
# crontab -e
# */30 * * * * /usr/bin/ruby /your_rails_project/lib/this_file.rb
# that will insert this job into your delayed_job queue every 30 minutes.
require 'rubygems'
require 'mysql'
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
# 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("localhost", "DB_USER", "DB_PASSWORD",
"DATABASE_NAME")
# 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 = "YOUR_MODEL"
model_method = "YOUR_METHOD"
args = parse_args({YOUR_ARGUMENTS})
dbh.query("INSERT INTO `delayed_jobs` (`failed_at`, `locked_by`, `created_at`, `handler`, `updated_at`, `priority`, `run_at`, `attempts`, `locked_at`, `last_error`) VALUES(NULL, NULL, '#{db_time}', '--- !ruby/struct:Delayed::PerformableMethod \nobject: LOAD;#{model}\nmethod: :#{model_method}\nargs: #{args}', '#{db_time}', 0, '#{db_time}', 0, NULL, NULL)")
@troex
Copy link

troex commented Jun 1, 2012

That is right idea! Especially when you have heavy application which like mine takes 75 seconds to start only

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