Skip to content

Instantly share code, notes, and snippets.

@kei-p
Last active October 27, 2023 01:39
Show Gist options
  • Save kei-p/8832e217ebb73e8c7a4df4cfe4f0c52f to your computer and use it in GitHub Desktop.
Save kei-p/8832e217ebb73e8c7a4df4cfe4f0c52f to your computer and use it in GitHub Desktop.
whenever to good_job_cron
require 'whenever'
ENV['RAILS_ENV'] = 'production'
ENV['MASTER_HOST'] = Socket.gethostname
options = {
crontab_command: 'crontab',
file: 'config/schedule.rb',
}
output = Whenever::JobList.new(options).send(:cron_jobs)
def parse(match)
[
match[:task_name].gsub('cron:', '').tr(':', '_').to_sym,
{
cron: match[:cron_time],
class: 'Jobmon::TaskJob',
kwargs: {
task: match[:task_name],
estimate_time: (match[:estimate_time] || 180).to_i,
},
},
]
end
tasks = output.split("\n").compact_blank.map do |cron|
match = cron.match(/^(?<cron_time>.*) cd.* --estimate-time (?<estimate_time>\d+) (?<task_name>.*) >>/) || cron.match(/^(?<cron_time>.*) cd.* (?<task_name>.*) >>/)
unless match
puts "[ERROR] Not match: #{cron}"
next
end
parse(match.named_captures.symbolize_keys)
end.compact_blank
out_path = Rails.root.join('tmp/cron.rb')
puts "\nOutput to: #{out_path}"
File.open(out_path, 'w') do |f|
f.write(JSON.pretty_generate(tasks.to_h))
end
`bundle exec rubocop -a #{out_path} &> /dev/null`
puts `cat #{out_path}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment