简体   繁体   English

如何在生产中运行发条脚本

[英]How to run clockwork script in production

I am using clockwork gem. 我正在使用发条宝石。 I want to automatically delete all comments from comments table every day at midnight. 我想每天午夜自动从comments表中删除所有评论。

This app/clock.rb script is working as it supposed to in development env.: 这个app/clock.rb脚本可以在开发环境中正常工作:

require './config/boot'
require './config/environment'
require 'clockwork'

module Clockwork
  every(1.day, 'delete.comment', at: '00:00') {
    Comment.destroy_all
  }
end

when it's running on my local terminal with $ clockwork app/clock.rb . 当它在$ clockwork app/clock.rb本地终端上运行时。

So I deployed the app to Heroku. 因此,我将应用程序部署到了Heroku。 Later in a terminal, I run: 稍后在终端中,我运行:

RAILS_ENV=production rails r app/clock.rb

as in this SO topic but only output is: 像本SO主题中一样,但只有输出是:

Running via Spring preloader in process 13973

and it quits to prompt and comments are still there (assuming the value in at: hash has passed). 并退出以提示,并且注释仍然存在(假定at:哈希值已通过)。

This is my first individual project. 这是我的第一个个人项目。 My question is how to make this script running nonstop in production to remove comments automatically every day at midnight? 我的问题是如何使此脚本在生产中不间断运行,以便每天午夜自动删除评论?
OR 要么
more general: 更一般的:
how to run script nonstop in production. 如何在生产中不停地运行脚本。

I read this SO topic but the answers are not very thorough and first link above is neither, as for a beginner to understand. 我读了这个SO主题,但是答案不是很彻底,对于初学者来说,上面的第一个链接都不是。

You can create rake task and call form clock.rb 您可以创建rake任务并调用表格clock.rb

every(1.day, 'name', at: '09:00')  do
    #Load task
    Rake::Task['comment:destroy'].invoke
end

rake task: 耙任务:

lib/tasks/comment.rake lib /任务/comment.rake

namespace :comment do
  desc 'Destroy all comment'
  task destroy: :environment do
    Comment.destroy_all
  end
end

Thanks for effort. 感谢您的努力。 After all, I managed this by rake task and without clockwork gem. 毕竟,我是通过rake任务来完成此任务的,并且没有clockwork宝石。 I created a rake task, deploy it to heroku, and ran task through heroku scheduler addon. 我创建了一个rake任务,将其部署到heroku,然后通过heroku Scheduler插件运行任务。 Now it's running as a background job. 现在它正在作为后台作业运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM