How to automatically stop a cron-like service(whenever / clockwork) with Rails server shutdown?

StackOverflow https://stackoverflow.com/questions/16719360

سؤال

I implemented "whenever" service in my Rails app to execute an operation periodically. The operation is expected to be executed only while the Rails app is running, and automatically stopped when the app is shutdown.

I succeeded to automatically start the cron service by calling it in a initializer script, but I can't guess how to stop the service execpt call a shell script manually. How can I do that?

I don't care the kind of cron-like service(whenever, clockwork etc).

هل كانت مفيدة؟

المحلول

They're two opposing services, so you will probably have to handle them differently.

Whenever outputs the cron commands to a crontab. To stop the commands from running, you remove the crontab. You can shell out to the system on exit and issue the

whenever clear

command to clear the job schedules.

Clockwork is a cron replacement and it has its own daemon. Because it's daemonized, you should be able to stop the daemon on exit.

If you want to generalize this, you might have to write two classes describing behaviour for either and then write a superclass that invokes one or the other or both (and handles errors coming from a command not existing).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top