is there a way to "watch" for new database table inserts using rake task?
I have rufus scheduler and i was planning to watch my schedules table for new schedules. Is there a way to do that? something like:
schedule开发者_StackOverflow中文版.every "1m" do
#check for new entries in database
end
ANSWERED
scheduler.every '1m' do
#check for new stuff in the database
newest_jobs = Schedule.all_cron_jobs.select{ |x| x.created_at > 1.minute.ago } - all_schedules_with_cron
unless newest_jobs.empty?
newest_jobs.each do |new_job|
scheduler.every new_job.cron_string do
job.call
end
end
end
end
jobs.call is a call to delayed job to queue that schedule. So basically, get the newest schedules, and call rufus on them to put the jobs on queue every time the cron matches
Use the created_at
column on the table to check for new entries since you last checked.
last_checked = 1.minute.ago
schedule.every "1m" do
new_entries = Schedule.where(['created_at >= ?', last_checked])
last_checked = Time.now
# deal with entries here.
end
You could improve this by minimising the gap between last_checked
cycles even more.
Would you not be better to use an observer on the model instead of this? Guarantees no misses due to timing issues.
精彩评论