lib/qpush/server/jobs.rb in qpush-0.1.2 vs lib/qpush/server/jobs.rb in qpush-0.1.4
- old
+ new
@@ -1,13 +1,15 @@
module QPush
module Server
module JobHelpers
- def bump_success
+ def mark_success
+ @failed = false
@total_success += 1
end
- def bump_fail
+ def mark_failed
+ @failed = true
@total_fail += 1
end
def retry_job?
@retry_max > @total_fail
@@ -43,19 +45,22 @@
Time.now.to_i + ((@total_fail**4) + 15 + (rand(30) * (@total_fail + 1)))
end
end
class Job < QPush::Job::Base
+ extend Forwardable
+
include QPush::Server::JobHelpers
include ObjectValidator::Validate
- attr_reader :api
-
def initialize(options)
super
- @api = JobApi.new(self)
+ @api = ApiWrapper.new(self)
end
+
+ def_delegators :@api, :queue, :execute, :perform,
+ :delay, :retry, :morgue, :setup
end
class JobValidator
include ObjectValidator::Validator
@@ -70,58 +75,8 @@
validates :created_at, type: Integer
validates :start_at, type: Integer
validates :retry_max, type: Integer
validates :total_fail, type: Integer
validates :total_success, type: Integer
- end
-
- class JobApi
- def initialize(job)
- @job = job
- end
-
- def queue
- QPush.redis.with do |conn|
- conn.incr("#{QPush.config.stats_namespace}:queued")
- conn.lpush("#{QPush.config.queue_namespace}", @job.to_json)
- end
- end
-
- def execute
- execute = Execute.new(@job)
- execute.call
- end
-
- def perform
- QPush.redis.with do |conn|
- conn.incr("#{QPush.config.stats_namespace}:performed")
- conn.lpush("#{QPush.config.perform_namespace}:#{@job.priority}", @job.to_json)
- end
- end
-
- def delay
- send_to_delay('delayed', @job.delay_until)
- end
-
- def retry
- send_to_delay('retries', @job.retry_at)
- end
-
- def setup
- fail unless @job.valid?
- perform if @job.perform_job?
- delay if @job.delay_job?
- rescue
- raise ServerError, 'Invalid job: ' + @job.errors.full_messages.join(' ')
- end
-
- private
-
- def send_to_delay(stat, time)
- QPush.redis.with do |conn|
- conn.incr("#{QPush.config.stats_namespace}:#{stat}")
- conn.zadd(QPush.config.delay_namespace, time, @job.to_json)
- end
- end
end
end
end