Search code examples
rubydaemons

Can stopping a daemon corrupt my data?


I'm using delayed_job to run some background processes on my rails app. These processes read and write to the DB as well as writing to a log file.

My question is - when I stop the jobs, does it kill the process (which can end up corrupting my data) or does it wait for the job to end or something like that?


Solution

  • That all depends on how you wrote your code.

    Generally killing a job jerks the plug on your code and it immediately stops. That could be in the middle of a write or before a buffer gets flushed or a file is gracefully closed.

    If you have tasks that HAVE to complete you can trap signals. Your code can then choose to ignore the signal and continue running, which isn't an optimal solution, or it could begin shutting down gracefully, flushing buffers, closing DB connections, closing files, etc., followed by quitting.

    And, the reason ignoring an signal isn't good is some are sent by the OS to tell apps to shutdown prior to the system shutting down or restarting. Apps that don't close down can get the user or sysadmin pissed, who will then find a way to MAKE it quit, most likely in an ungraceful way.

    Check out Signal for more info.