Sha256: 118bbfdd98e74c948485593e38306d23afa7bf0de5efad94fef57e173ccf5112

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# encoding: utf-8

require 'say_when/storage/memory/base'

module SayWhen
  module Storage
    module Memory

      # define a trigger class
      class Job

        include SayWhen::Storage::Memory::Base
        include SayWhen::BaseJob

        has_properties :group, :name, :status, :start_at, :end_at
        has_properties :trigger_strategy, :trigger_options, :last_fire_at, :next_fire_at
        has_properties :job_class, :job_method, :data
        has_properties :scheduled

        cattr_accessor :jobs
        @@jobs = SortedSet.new
        @@acquire_lock = Mutex.new

        def self.acquire_next(no_later_than)
          @@acquire_lock.synchronize {

            next_job = jobs.detect(nil) do |j|
              (j.status == STATE_WAITING) && (j.next_fire_at.to_i <= no_later_than.to_i)
            end

            next_job.status = STATE_ACQUIRED if next_job
            next_job
          }
        end

        def self.create(job)
          job = new(job) if job.is_a?(Hash)
          self.jobs << job
        end

        def initialize(options={})
          super
          self.status = STATE_WAITING unless self.status
          self.next_fire_at = trigger.next_fire_at
        end

        def <=>(job)
          self.next_fire_at.to_i <=> job.next_fire_at.to_i
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
say_when-1.0.0 lib/say_when/storage/memory/job.rb