0

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

linzhixing
  • 146
  • 2
  • 12

1 Answers1

2

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

Srdjan Pejic
  • 8,152
  • 2
  • 28
  • 24
  • How can I issue automatically the command when Rails exits? Is there any method to check it or something like Rails "initializer" for shutdown? Java Quartz only works while the server is running without clearing crontab or daemon manually, I expected such one in Rails. – linzhixing May 24 '13 at 13:46