How to exit into the shell after scheduling jobs in a rake task

32 views
Skip to first unread message

Tony Jiang

unread,
Mar 3, 2013, 2:13:10 AM3/3/13
to rufus...@googlegroups.com
I have a rake task in which I schedule some jobs like this:

  task :start => :environment do
    scheduler = Rufus::Scheduler.start_new
    
    scheduler.every '2s' do
       File.open("tmp/foo", 'a') do |file|
          file.puts "foo"
      end
    end
  end

After I do 'bundle exec rake start', the task doesn't exit - I have to kill it with Ctrl-C. Is there a way to make it exit while the scheduled job keeps running?

My second question is: once that's accomplished, how can I write a rake task to kill the scheduler?


 

John Mettraux

unread,
Mar 3, 2013, 3:51:21 AM3/3/13
to rufus...@googlegroups.com

On Sat, Mar 02, 2013 at 11:13:10PM -0800, Tony Jiang wrote:
>
> After I do 'bundle exec rake start', the task doesn't exit - I have to kill
> it with Ctrl-C. Is there a way to make it exit while the scheduled job
> keeps running?

Hello Tony,

you cannot. When the process exits the scheduler is dies, as expected.

> My second question is: once that's accomplished, how can I write a rake
> task to kill the scheduler?

Since you cannot accomplish the first one, your second question doesn't hold.


Ever used "&" and "kill" in your bash environment? Ever came across the term
"daemon"?


Best regards,

--
John Mettraux - http://lambda.io/jmettraux

Tony Jiang

unread,
Mar 3, 2013, 3:26:36 PM3/3/13
to rufus...@googlegroups.com
Hi John,

Thank you for the reply.

I guess I was looking for something elegant and doesn't involve the shell. Some things that look like scheduler.detach, Rufus.schedulers & scheduler.exit, Rufus.exit_all_schedulers, etc. so that I can do 'bundle exec rake scheduler:stop'. 

This actually works inside the rake task stop:

    processes = systemu("ps eu")[1].split("\n")
    command = processes.find {|process| process =~ /scheduler:start/}
    process_id = command.split(" ")[1]
    systemu("kill -9 #{process_id}")

Not ideal, but might be my final solution.

John Mettraux

unread,
Mar 3, 2013, 9:29:47 PM3/3/13
to rufus...@googlegroups.com

On Sun, Mar 03, 2013 at 12:26:36PM -0800, Tony Jiang wrote:
>
> I guess I was looking for something elegant and doesn't involve the shell.
> Some things that look like *scheduler.detach, Rufus.schedulers &
> scheduler.exit, Rufus.exit_all_schedulers,* etc. so that I can do 'bundle
> exec rake scheduler:stop'.

Hello Tony,

sorry, rufus-scheduler is a library, not an application.

All the methods you mention imply design decisions, like the one you took in
the code below when you decided to take the first listed processed that
matched the "scheduler:start" string. Another classic would have been to go
with a PID file.

Your design decisions certainly conflict with other integrators' design
decisions.

There are a lot of daemon tools for Ruby out there, better to let integrators
pick the one they know and like and fits their use case.


> This actually works inside the rake task *stop*:
>
> * processes = systemu("ps eu")[1].split("\n")*
> * command = processes.find {|process| process =~ /scheduler:start/}*
> * process_id = command.split(" ")[1]*
> * systemu("kill -9 #{process_id}")*
>
> Not ideal, but might be my final solution.

Might be ideal if you're OK with 1+ scheduling process.

Tony Jiang

unread,
Mar 3, 2013, 11:03:45 PM3/3/13
to rufus...@googlegroups.com
I am using JRuby so daemonizing tools don't work - Process.fork is not supported.
Reply all
Reply to author
Forward
0 new messages