I want to execute a cron after creation of some entity every 2 minutes and I should be able to stop it through code only. Is it possible ?
2 Answers
For the simplest way that I can think of right now that you can do what @tlehman said and add a "checking flag" in the code. (maybe just a simple variable inside a filename)
so before the cron started, it should check for a file with X variable with value true/false.
if true then it should run the code, if false then exit.
(you could then write another code to modify that "check file" to enable/disable the cron)
-
Thanks Firman. Can you please tell me how can i schedule a cron job in after_create filter of any model ??– PankhuriCommented Mar 16, 2015 at 17:44
That sounds like a job for the whenever gem, you can use it's simple DSL like so:
every 2.minutes do
runner "SomeRubyCommand.do_work"
end
-
how can i schedule a cron after creation of an entity and how can i stop it ? I don't want the process to remain forever.– PankhuriCommented Mar 16, 2015 at 17:28
-
@firman-gautama has a solution, make the code depend on some flag, you could set the flag in your database.– tlehmanCommented Mar 16, 2015 at 17:34
whenever
? github.com/javan/whenever