diff --git a/config/schedule.rb b/config/schedule.rb index 6f8e7b582a..8cc8d7d225 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -7,8 +7,7 @@ env "MAILTO", "rohan@rohanmitchell.com" # If we use -e with a file containing specs, rspec interprets it and filters out our examples job_type :run_file, "cd :path; :environment_variable=:environment bundle exec script/rails runner :task :output" - -job_type :enqueue_job, "cd :path; :environment_variable=:environment bundle exec script/rails runner 'Delayed::Job.enqueue(:task.new, priority: :priority)' :output" +job_type :enqueue_job, "cd :path; :environment_variable=:environment bundle exec script/enqueue :task :priority :output" every 1.hour do @@ -28,7 +27,7 @@ every 4.hours do end every 5.minutes do - enqueue_job 'QueueHeartbeatJob', priority: 0 + enqueue_job 'HeartbeatJob', priority: 0 end every 1.day, at: '1:00am' do diff --git a/script/enqueue b/script/enqueue new file mode 100755 index 0000000000..2071414e4e --- /dev/null +++ b/script/enqueue @@ -0,0 +1,61 @@ +#!/usr/bin/env ruby + +# Push a job onto the Delayed Job queue without booting the Rails stack +# Perfect for calling via cron. +# +# Use like this: +# +# ./script/enqueue +# ./script/enqueue Background::ImportJobs + +require 'erb' +require 'yaml' + +ENV["RAILS_ENV"] ||= "development" + +DATABASE_CONFIG = File.expand_path("../../config/database.yml", __FILE__) + +def psql + raise "Missing database.yml" unless File.exists?(DATABASE_CONFIG) + + file = File.read(DATABASE_CONFIG) + erb = ERB.new(file).result + env = ENV["RAILS_ENV"] + config = YAML.load(erb)[env] + + raise "Missing config for #{env} environment" unless config + + "psql".tap do |s| + s << " --host #{config['host']}" if config['host'] + s << " --user #{config['username']}" if config['username'] + s << " --port #{config['port']}" if config['port'] + s << " #{config['database']}" + end +end + +def enqueue_delayed_job(handler, priority=nil) + time = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S") + priority ||= 50 + + sql = <<-SQL + INSERT INTO delayed_jobs ( + handler, + created_at, + updated_at, + run_at, + priority + ) VALUES ( + '--- !ruby/object:#{handler} {}\n', + '#{time}', + '#{time}', + '#{time}', + #{priority} + ); + SQL + + IO.popen(psql, "w") do |io| + io.write sql + end +end + +enqueue_delayed_job ARGV[0], ARGV[1]