我想设置cron job
来删除超过两分钟的屏幕截图,并且每两分钟运行一次cron作业。
schedule.rb文件的内容
every 2.minutes do
runner 'screenshot.delete_old', output: {error: "#{path}/log/error.log", standard: "#{path}/log/cron.log"}
end
Model Screenshot.rb文件的内容
def self.delete_old
# @old_screenshots = Screenshot.where('Screenshot.created_at < ?', Time.now)
#@old_screenshots.delete_all
#@screenshot.delete_old
#Screenshot.delete
screenshot_ids = screenshot.find(:all, :conditions => ["created_at ", 2.minutes.ago])
if screenshot_ids.size > 0
screenshot.destroy(screenshot_ids)
puts "#{screenshot_ids.size} screenshots have been deleted!"
end
end
我更新了crontab文件并运行了rails服务器,希望删除screeshots。但它没有用。有人可以帮我吗?感谢。
答案 0 :(得分:0)
Ruby中的类应该有首字母,也许你应该尝试:
every 2.minutes do
runner 'Screenshot.delete_old', output: {error: "#{path}/log/error.log", standard: "#{path}/log/cron.log"}
end
和你的模特:
def self.delete_old
screenshot_ids = Screenshot.where('created_at < ?', 2.minutes.ago))
if screenshot_ids.size > 0
Screenshot.destroy(screenshot_ids)
puts "#{screenshot_ids.size} screenshots have been deleted!"
end
end