简体   繁体   中英

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.

This app/clock.rb script is working as it supposed to in development env.:

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 .

So I deployed the app to Heroku. Later in a terminal, I run:

RAILS_ENV=production rails r app/clock.rb

as in this SO topic but only output is:

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).

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.

You can create rake task and call form clock.rb

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

rake task:

lib/tasks/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. I created a rake task, deploy it to heroku, and ran task through heroku scheduler addon. Now it's running as a background job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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