lib/rufus/sc/jobs.rb in rufus-scheduler-2.0.6 vs lib/rufus/sc/jobs.rb in rufus-scheduler-2.0.7

- old
+ new

@@ -65,20 +65,27 @@ # The identifier for this job. # attr_reader :job_id + attr_accessor :running # Instantiating the job. # - def initialize (scheduler, t, params, &block) + def initialize(scheduler, t, params, &block) @scheduler = scheduler @t = t @params = params @block = block || params[:schedulable] + @running = false + @allow_overlapping = true + if !params[:allow_overlapping].nil? + @allow_overlapping = params[:allow_overlapping] + end + raise ArgumentError.new( 'no block or :schedulable passed, nothing to schedule' ) unless @block @params[:tags] = Array(@params[:tags]) @@ -96,11 +103,11 @@ end # Sets the list of tags attached to the job (Usually they are set # via the schedule every/at/in/cron method). # - def tags= (tags) + def tags=(tags) @params[:tags] = Array(tags) end # Generally returns the string/float/integer used to schedule the job @@ -111,31 +118,39 @@ @t end # Triggers the job. # - def trigger (t=Time.now) + def trigger(t=Time.now) @last = t job_thread = nil to_job = nil + return if @running && !@allow_overlapping + + @running = true @scheduler.send(:trigger_job, @params[:blocking]) do # # Note that #trigger_job is protected, hence the #send # (Only jobs know about this method of the scheduler) job_thread = Thread.current + job_thread[ + "rufus_scheduler__trigger_thread__#{@scheduler.object_id}" + ] = true @last_job_thread = job_thread begin trigger_block job_thread = nil to_job.unschedule if to_job + @running = false + rescue Exception => e @scheduler.handle_exception(self, e) end end @@ -152,10 +167,11 @@ if job_thread && job_thread.alive? job_thread.raise(Rufus::Scheduler::TimeOutError) end end end + end # Simply encapsulating the block#call/trigger operation, for easy # override. # @@ -205,11 +221,11 @@ # If this InJob is a timeout job, parent points to the job that # is subject to the timeout. # attr_reader :parent - def initialize (scheduler, t, params) + def initialize(scheduler, t, params) @parent = params[:parent] super end protected @@ -236,11 +252,11 @@ # The frequency, in seconds, of this EveryJob # attr_reader :frequency - def initialize (scheduler, t, params, &block) + def initialize(scheduler, t, params, &block) super determine_frequency determine_at end @@ -308,40 +324,41 @@ # attr_reader :block # Creates a new CronJob instance. # - def initialize (scheduler, cron_string, params, &block) + def initialize(scheduler, cron_string, params, &block) super @cron_line = case @t when String then CronLine.new(@t) when CronLine then @t - else raise "cannot initialize a CronJob out of #{@t.inspect}" + else raise ArgumentError.new( + "cannot initialize a CronJob out of #{@t.inspect}") end end - def trigger_if_matches (time) + def trigger_if_matches(time) trigger(time) if @cron_line.matches?(time) end # Returns the next time this job is meant to trigger # - def next_time (from=Time.now) + def next_time(from=Time.now) @cron_line.next_time(from) end protected def determine_at + # empty end end - end end