Class: Delayed::HomeWorker

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/delayed/home_worker.rb

Constant Summary collapse

DEFAULT_MAX_ATTEMPTS =
25
DEFAULT_TIMEOUT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ HomeWorker

Returns a new instance of HomeWorker.



13
14
15
16
17
18
19
20
21
# File 'lib/delayed/home_worker.rb', line 13

def initialize(options=nil)
  if (options && options.class == Hash)
    @max_attempts = options[:max_attempts]
    @timeout = options[:timeout]
  end

  @max_attempts ||= DEFAULT_MAX_ATTEMPTS
  @timeout ||= DEFAULT_TIMEOUT
end

Instance Attribute Details

#max_attemptsObject

Returns the value of attribute max_attempts.



11
12
13
# File 'lib/delayed/home_worker.rb', line 11

def max_attempts
  @max_attempts
end

#timeoutObject

Returns the value of attribute timeout.



11
12
13
# File 'lib/delayed/home_worker.rb', line 11

def timeout
  @timeout
end

Instance Method Details

#work(job) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/delayed/home_worker.rb', line 23

def work(job)
  delayable_type = job.delayable_type
  delayable_id = job.delayable_id
  jid = job.id

  t = DateTime.now.to_f
  if (job && job.respond_to?(:invoke_job))
    begin
      Timeout.timeout(@timeout) do
        job.invoke_job
        job.delete
      end
    rescue
      if (job.attempts <= @max_attempts)
        job.attempts += 1
        job.run_at = (job.attempts ** 4 + 5).seconds.from_now
        job.save
      else
        job.delete
      end
    end

    delta = DateTime.now.to_f - t

    if delayable_type && delayable_id
      DelayedJobActiveRecordThreaded.logger.info "[#{DateTime.now.to_s}] Job for #{delayable_type} delayable_id #{delayable_id} completed in #{delta} seconds" if DelayedJobActiveRecordThreaded.logger
    else
      DelayedJobActiveRecordThreaded.logger.info "[#{DateTime.now.to_s}] Job #{jid} completed in #{delta} seconds" if DelayedJobActiveRecordThreaded.logger
    end
  end

ensure
  #attempt closing connection
  begin
    if (ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?)
      ActiveRecord::Base.connection.close
    end
  rescue
  end
end