Unscheduling running tasks.

29 views
Skip to first unread message

Tim Uckun

unread,
Oct 20, 2009, 6:24:27 PM10/20/09
to rufus...@googlegroups.com
What happens when I unschedule a task that is running?

Basically I am looking to gracefully exit on kill signal. I want to
make sure I unschedule all tasks and wait till all jobs are running.

John Mettraux

unread,
Oct 21, 2009, 3:08:40 AM10/21/09
to rufus...@googlegroups.com
On Wed, Oct 21, 2009 at 7:24 AM, Tim Uckun <timu...@gmail.com> wrote:
>
> What happens when I unschedule a task that is running?

Hello Tim,

the scheduler will simply not reschedule afterwards. It doesn't take
care of killing the running task for you.

> Basically I am looking to gracefully exit on kill signal. I want to
> make sure I unschedule all tasks and wait till all jobs are running.

The scheduler doesn't know what your tasks are about. It just wants to
know when to fire them.

Maybe it would be interesting to have a hook, a method of the job
called when the task get unscheduled. Most people schedule tasks with
ruby blocks, adding a hook to a Schedulable would be easy but to a
block, it would requiring some thinking. Maybe you have suggestions.


Best regards,

--
John Mettraux - http://jmettraux.wordpress.com

Tim Uckun

unread,
Oct 22, 2009, 3:23:24 AM10/22/09
to rufus...@googlegroups.com
> Maybe it would be interesting to have a hook, a method of the job
> called when the task get unscheduled. Most people schedule tasks with
> ruby blocks, adding a hook to a Schedulable would be easy but to a
> block, it would requiring some thinking. Maybe you have suggestions.
>

I first attempted to simply stop the scheduler and check the job count
but that didn't work because "in" jobs drop off the list as soon as
they start (this is different than the old behavior BTW).

In the end I ended up looking at Thread.list and waiting till that
number was 1. I don't know if this approach would work with EM,
probably not.

It seems to me if you only removed the jobs from the list after they
were done (like the old way) instead of when they start I could keep
polling the list till they were done.

another option might be to create a "running_jobs" list where you
could move the job from the jobs list to the running jobs list.

John Mettraux

unread,
Oct 22, 2009, 3:31:46 AM10/22/09
to rufus...@googlegroups.com
On Thu, Oct 22, 2009 at 4:23 PM, Tim Uckun <timu...@gmail.com> wrote:
>
>> Maybe it would be interesting to have a hook, a method of the job
>> called when the task get unscheduled. Most people schedule tasks with
>> ruby blocks, adding a hook to a Schedulable would be easy but to a
>> block, it would requiring some thinking. Maybe you have suggestions.
>>
>
> I first attempted to simply stop the scheduler and check the job count
> but that didn't work because "in" jobs drop off the list as soon as
> they start (this is different than the old behavior BTW).

Hello Tim,

yes, this is different from the old behaviour.

> In the end I ended up looking at Thread.list and waiting till that
> number was 1. I don't know if this approach would work with EM,
> probably not.

Indeed.

> It seems to me if you only removed the jobs from the list after they
> were done (like the old way) instead of when they start I could keep
> polling the list till they were done.
>
> another option might be to create a "running_jobs" list where you
> could move the job from the jobs list to the running jobs list.

I will have a look at that. IIRC, the "running jobs" list idea would
be simpler :

http://github.com/jmettraux/rufus-scheduler/issues/#issue/1

Tim Uckun

unread,
Oct 22, 2009, 4:47:53 PM10/22/09
to rufus...@googlegroups.com
>
> I will have a look at that. IIRC, the "running jobs" list idea would
> be simpler :
>
>  http://github.com/jmettraux/rufus-scheduler/issues/#issue/1


Here is a bit of code my friend Aries wrote.


module Rufus
module Scheduler
class SchedulerCore
def running_jobs
@jobs.running_jobs
end

alias_method :all_jobs_old, :all_jobs

def all_jobs
all_jobs_old.merge running_jobs
end
end

class JobQueue
alias_method :initialize_old, :initialize

def initialize
initialize_old
@running_jobs = []
end

def running_jobs
@running_jobs.inject({}) { |h, j| h[j.job_id] = j; h }
end

alias_method :job_to_trigger_old, :job_to_trigger

def job_to_trigger
@mutex.synchronize do
@running_jobs = @running_jobs.select { |j| j.job_thread &&
j.job_thread.alive? }

if @jobs.size > 0 && Time.now.to_f >= @jobs.first.at
running_job = @jobs.shift
old_job = @running_jobs.find { |j| j.job_id == running_job.job_id }
@running_jobs.delete(old_job) if old_job
@running_jobs << running_job

running_job
else
nil
end
end
end
end
end
end

John Mettraux

unread,
Oct 23, 2009, 3:02:37 AM10/23/09
to rufus...@googlegroups.com
On Fri, Oct 23, 2009 at 5:47 AM, Tim Uckun <timu...@gmail.com> wrote:
>
>>
>> I will have a look at that. IIRC, the "running jobs" list idea would
>> be simpler :
>>
>>  http://github.com/jmettraux/rufus-scheduler/issues/#issue/1
>
>
> Here is a bit of code my friend Aries wrote.
>
>
> module Rufus
>  module Scheduler
>
> ...

OK, thanks for the inspiration.

Reply all
Reply to author
Forward
0 new messages