Sha256: b3d4a8acbc2c2b476fef00a2d0862687e0e51552746e2da0b2d02ae278904c77

Contents?: true

Size: 1002 Bytes

Versions: 8

Compression:

Stored size: 1002 Bytes

Contents

module Writefully
  module Tools
    class Dispatcher
      include Celluloid

      attr_reader :job

      def initialize
        every 1.second do 
          async.heartbeat
        end
      end

      def get_job_data
        Writefully.redis.with { |c| c.spop 'jobs' }
      end

      def heartbeat
        @job      = Marshal.load(get_job_data)
        run_job if job_valid?
      end

      def run_job
        if    retry_valid?   then retry_job
        elsif job_valid?     then dispatch
        end
      end

      def dispatch
        Celluloid::Actor[job[:worker]].perform(job[:message])
      end

      def retry_job
        Celluloid::Actor[:retryer].retry(job)
      end

      def is_retry?
        job_valid? and job[:message].has_key?(:tries) and job[:message].has_key?(:run)
      end

      def job_valid?
        job.has_key?(:worker) and job.has_key?(:message)
      end

      def retry_valid?
        is_retry? and (job[:message][:run] == false)
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
writefully-0.4.10 lib/writefully/tools/dispatcher.rb
writefully-0.4.8 lib/writefully/tools/dispatcher.rb
writefully-0.4.7 lib/writefully/tools/dispatcher.rb
writefully-0.4.6 lib/writefully/tools/dispatcher.rb
writefully-0.4.5 lib/writefully/tools/dispatcher.rb
writefully-0.4.4 lib/writefully/tools/dispatcher.rb
writefully-0.4.2 lib/writefully/tools/dispatcher.rb
writefully-0.4.1 lib/writefully/tools/dispatcher.rb