lib/rufus/sc/jobs.rb in rufus-scheduler-2.0.16 vs lib/rufus/sc/jobs.rb in rufus-scheduler-2.0.17

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2006-2011, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2006-2012, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -28,11 +28,11 @@ # # The base class for all types of jobs. # class Job - + # A reference to the scheduler owning this job # attr_accessor :scheduler # The initial, raw, scheduling info (at / in / every / cron) @@ -74,10 +74,12 @@ @scheduler = scheduler @t = t @params = params @block = block || params[:schedulable] + raise_on_unknown_params + @running = false @paused = false raise ArgumentError.new( 'no block or :schedulable passed, nothing to schedule' @@ -233,10 +235,43 @@ # def unschedule @scheduler.unschedule(self.job_id) end + + protected + + def known_params + + [ :allow_overlapping, + :blocking, + :discard_past, + :job_id, + :mutex, + :schedulable, + :tags, + :timeout ] + end + + def self.known_params(*args) + + define_method :known_params do + super() + args + end + end + + def raise_on_unknown_params + + rem = @params.keys - known_params + + raise( + ArgumentError, + "unknown option#{rem.size > 1 ? 's' : '' }: " + + "#{rem.map(&:inspect).join(', ')}", + caller[3..-1] + ) if rem.any? + end end # # The base class of at/in/every jobs. # @@ -244,10 +279,12 @@ # When the job is supposed to trigger # attr_reader :at + # Last time it triggered + # attr_reader :last def determine_at @at = Rufus.at_to_f(@t) @@ -264,16 +301,19 @@ # # Job that occurs once, in a certain amount of time. # class InJob < SimpleJob + known_params :parent + # 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) + @parent = params[:parent] super end protected @@ -296,10 +336,12 @@ # # Recurring job with a certain frequency. # class EveryJob < SimpleJob + known_params :first_in, :first_at + # The frequency, in seconds, of this EveryJob # attr_reader :frequency def initialize(scheduler, t, params, &block) @@ -321,11 +363,14 @@ protected def determine_frequency - @frequency = @t.is_a?(Fixnum) || @t.is_a?(Float) ? - @t : Rufus.parse_duration_string(@t) + @frequency = if @t.is_a?(Fixnum) || @t.is_a?(Float) + @t + else + Rufus.parse_duration_string(@t) + end end def determine_at return unless @frequency