how to release the rufus process

128 views
Skip to first unread message

edward

unread,
Aug 24, 2012, 5:10:45 AM8/24/12
to rufus...@googlegroups.com
Hello everybody
I need to compile a ruby script into a window exe file using OCRA, in my script, I used fufus to execute a scheduler task, including 
scheduler.every...   
scheduler cron...
I use job.unschedule to unload the scheuler, but the process is still live. so the OCRA could not get all script informaiton.  until the whole process is done.
my question is:
how to stop or release the script/process?
Any help would be great appreciate

Edward 

John Mettraux

unread,
Aug 24, 2012, 5:13:11 AM8/24/12
to rufus...@googlegroups.com
Hello,

you can do

exit(0)

to exit the process (and of course terminate the scheduler).


Hope it helps, best regards,

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

edward

unread,
Aug 24, 2012, 6:28:24 AM8/24/12
to rufus...@googlegroups.com
Thanks for your so quickly reply, John.

edward

unread,
Aug 24, 2012, 6:57:26 AM8/24/12
to rufus...@googlegroups.com
Dear John
where is the place to put [exit(0)]?

the following is my script:
-----<8----
def time_format(t) ;t.strftime("%Y-%m-%d %a %H:%M") ;end

begin
scheduler = Rufus::Scheduler.start_new
i=0
scheduler.every '30s' do |job|
shut_time=time_format(Time.now)
 p shut_time
if shut_time =="2012-08-24 Fri 18:53"
    puts "crop is ready #{i}."
    job.unschedule
    Process.exit(0)
  else
    i+=1
    puts "crop not yet ready..#{i}."
  end
end
scheduler.join
end
-------->8--------
it caused a "scheduler caught exception:"
 
Cheers
Edward

John Mettraux

unread,
Aug 24, 2012, 7:24:37 AM8/24/12
to rufus...@googlegroups.com
On Fri, Aug 24, 2012 at 03:57:26AM -0700, edward wrote:
>
> the following is my script:
> -----<8----
> def time_format(t) ;t.strftime("%Y-%m-%d %a %H:%M") ;end
>
> begin
> scheduler = Rufus::Scheduler.start_new
> i=0
> scheduler.every '30s' do |job|
> shut_time=time_format(Time.now)
> p shut_time
> if shut_time =="2012-08-24 Fri 18:53"
> puts "crop is ready #{i}."
> job.unschedule
> Process.exit(0)
> else
> i+=1
> puts "crop not yet ready..#{i}."
> end
> end
> scheduler.join
> end
> -------->8--------

Hello,

too bad you didn't mention what exception got caught.

Two variants, a:

---8<----
require 'rufus-scheduler'

def time_format(t); t.strftime("%Y-%m-%d %a %H:%M"); end

scheduler = Rufus::Scheduler.start_new

def scheduler.handle_exception(job, exception)
exit(0) if exception.is_a?(SystemExit)
p [ job, exception ]
end

i = 0

scheduler.every '30s' do |job|
shut_time = time_format(Time.now)
p shut_time
if shut_time == "2012-08-24 Fri 18:53"
puts "crop is ready #{i}."
#job.unschedule # no need to unschedule if we exit
Process.exit(0)
end
i += 1
puts "crop not yet ready..#{i}."
end
scheduler.join
--->8---

b:

---8<----
require 'rufus-scheduler'

def time_format(t); t.strftime("%Y-%m-%d %a %H:%M"); end

scheduler = Rufus::Scheduler.start_new

def scheduler.handle_exception(job, exception)
exit(0) if exception.message == 'over.'
p [ job, exception ]
end

i = 0

scheduler.every '30s' do |job|
shut_time = time_format(Time.now)
p shut_time
if shut_time == "2012-08-24 Fri 18:53"
puts "crop is ready #{i}."
#job.unschedule # no need to unschedule if we exit
raise "over."
end
i += 1
puts "crop not yet ready..#{i}."
end
scheduler.join
--->8---

And a third variant, because I think it's overkill to use rufus-scheduler for
that:

---8<---
require 'time'
# to enable Time.parse(s)

shutdown_time = Time.parse('2012-08-24 Fri 18:53')
i = 0

loop do

sleep 30

if Time.now >= shutdown_time
puts "#{Time.now} - crop is ready #{i}."
break
end

i = i + 1
puts "#{Time.now} - crop not yet ready..#{i}."
end
--->8---


Best regards,

edwar li

unread,
Aug 24, 2012, 7:29:51 PM8/24/12
to rufus...@googlegroups.com
Thank you very much for your very detail scripts,John.
--
you received this message because you are subscribed to the "rufus ruby" group.
to post : send email to rufus...@googlegroups.com
to unsubscribe : send email to rufus-ruby+...@googlegroups.com
more options : http://groups.google.com/group/rufus-ruby?hl=en

Eric Platon

unread,
Nov 7, 2012, 4:31:37 AM11/7/12
to rufus...@googlegroups.com
Jown, Edward,

Does any of the two alternative actually work? As for version 2.0.18, none worked for me. The reason they do not work is that the exit / Process.exit raise a SystemExit that Rufus evaluates as an error in the exception handler:

In lib/rufus/sc/scheduler.rb:
---
   def do_handle_exception(job, exception)

      begin

        [ :log_exception, :handle_exception, :on_exception ].each do |m|

          next unless self.respond_to?(m)

          if method(m).arity == 1
            self.send(m, exception)
          else
            self.send(m, job, exception)
          end

          return
            # exception was handled successfully
        end

      rescue Exception => e

        $stderr.puts '*' * 80
        $stderr.puts 'the exception handling method itself had an issue:'
        $stderr.puts e
        $stderr.puts *e.backtrace
        $stderr.puts '*' * 80
      end

      $stderr.puts '=' * 80
      $stderr.puts 'scheduler caught exception:'
      $stderr.puts exception
      $stderr.puts *exception.backtrace
      $stderr.puts '=' * 80
    end
---

The rescue catches all exceptions, including SystemExit.

Eric

John Mettraux

unread,
Nov 7, 2012, 4:38:46 AM11/7/12
to rufus...@googlegroups.com

On Wed, Nov 07, 2012 at 01:31:37AM -0800, Eric Platon wrote:
>
> Does any of the two alternative actually work? As for version 2.0.18, none
> worked for me. The reason they do not work is that the exit / Process.exitraise a
> SystemExit that Rufus evaluates as an error in the exception handler:
>
> In lib/rufus/sc/scheduler.rb:
>
> (...)
>
> The rescue catches all exceptions, including SystemExit.

Salut Eric,

glad to read from you.

Yes this rescue is "excessive", been thinking about removing it for a while,
but nobody complained [too much].

I'd be willing to be a good citizen and rescue the default (StandardError)
and let Exception go through.

What do you guys think?

Brandon

unread,
Nov 29, 2012, 10:17:01 AM11/29/12
to rufus...@googlegroups.com
This would be very helpful for me.  I am trying to use Rufus from a Windows daemon and cannot get the application to exit when the service is stopped. 

Brandon
Reply all
Reply to author
Forward
0 new messages