Files
openfoodnetwork/script/enqueue
Maikel Linke 38e9976ce4 Make inserting delayed jobs from cron quiet
`config/schedule.rb` defines entries for cron jobs. Every five minutes
it insert several delayed jobs via `script/enqueue`. A recent upgrade of
Postgresql (a year or two ago) made that script noisy. Every time we
insert a delayed job with that script it outputs:

  INSERT 0 1

Standard output like this is seen as error message by cron an forwarded
to the unix user's local mailbox in `/var/mail/openfoodnetwork`. This
file grows over time and accumulates tens of thousands of error
messages, hiding any important failures.

This patch makes inserting delayed jobs quiet. A failure is still
reported.
2019-05-22 03:36:07 +10:00

63 lines
1.4 KiB
Ruby
Executable File

#!/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 <JobClassName>
# ./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 << " --quiet"
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]