Class: Postjob::Job

Inherits:
Record
  • Object
show all
Defined in:
lib/postjob/job.rb

Overview

A job

Constant Summary collapse

STATUSES =
%w(ok ready processing sleep err failed timeout)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#initialize

Constructor Details

This class inherits a constructor from Postjob::Record

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



21
22
23
# File 'lib/postjob/job.rb', line 21

def args
  @args
end

#created_atObject (readonly)

Returns the value of attribute created_at.



16
17
18
# File 'lib/postjob/job.rb', line 16

def created_at
  @created_at
end

#errorObject (readonly)

Returns the value of attribute error.



28
29
30
# File 'lib/postjob/job.rb', line 28

def error
  @error
end

#error_backtraceObject (readonly)

Returns the value of attribute error_backtrace.



30
31
32
# File 'lib/postjob/job.rb', line 30

def error_backtrace
  @error_backtrace
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



29
30
31
# File 'lib/postjob/job.rb', line 29

def error_message
  @error_message
end

#failed_attemptsObject (readonly)

Returns the value of attribute failed_attempts.



24
25
26
# File 'lib/postjob/job.rb', line 24

def failed_attempts
  @failed_attempts
end

#full_idObject (readonly)

Returns the value of attribute full_id.



14
15
16
# File 'lib/postjob/job.rb', line 14

def full_id
  @full_id
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/postjob/job.rb', line 12

def id
  @id
end

#is_stickyObject (readonly)

Returns the value of attribute is_sticky.



35
36
37
# File 'lib/postjob/job.rb', line 35

def is_sticky
  @is_sticky
end

#last_worker_session_idObject (readonly)

Returns the value of attribute last_worker_session_id.



34
35
36
# File 'lib/postjob/job.rb', line 34

def last_worker_session_id
  @last_worker_session_id
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



25
26
27
# File 'lib/postjob/job.rb', line 25

def max_attempts
  @max_attempts
end

#next_run_atObject (readonly)

Returns the value of attribute next_run_at.



22
23
24
# File 'lib/postjob/job.rb', line 22

def next_run_at
  @next_run_at
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



13
14
15
# File 'lib/postjob/job.rb', line 13

def parent_id
  @parent_id
end

#queueObject (readonly)

Returns the value of attribute queue.



17
18
19
# File 'lib/postjob/job.rb', line 17

def queue
  @queue
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



31
32
33
# File 'lib/postjob/job.rb', line 31

def recipients
  @recipients
end

#resultsObject (readonly)

Returns the value of attribute results.



27
28
29
# File 'lib/postjob/job.rb', line 27

def results
  @results
end

#root_idObject (readonly)

Returns the value of attribute root_id.



15
16
17
# File 'lib/postjob/job.rb', line 15

def root_id
  @root_id
end

#statusObject (readonly)

Returns the value of attribute status.



26
27
28
# File 'lib/postjob/job.rb', line 26

def status
  @status
end

#sticky_host_idObject (readonly)

Returns the value of attribute sticky_host_id.



36
37
38
# File 'lib/postjob/job.rb', line 36

def sticky_host_id
  @sticky_host_id
end

#tagsObject (readonly)

Returns the value of attribute tags.



33
34
35
# File 'lib/postjob/job.rb', line 33

def tags
  @tags
end

#timed_outObject (readonly)

Returns the value of attribute timed_out.



32
33
34
# File 'lib/postjob/job.rb', line 32

def timed_out
  @timed_out
end

#timing_out_atObject (readonly)

Returns the value of attribute timing_out_at.



23
24
25
# File 'lib/postjob/job.rb', line 23

def timing_out_at
  @timing_out_at
end

#workflowObject (readonly)

Returns the value of attribute workflow.



18
19
20
# File 'lib/postjob/job.rb', line 18

def workflow
  @workflow
end

#workflow_methodObject (readonly)

Returns the value of attribute workflow_method.



19
20
21
# File 'lib/postjob/job.rb', line 19

def workflow_method
  @workflow_method
end

#workflow_versionObject (readonly)

Returns the value of attribute workflow_version.



20
21
22
# File 'lib/postjob/job.rb', line 20

def workflow_version
  @workflow_version
end

Class Method Details

.find(job_id) ⇒ Object



7
8
9
10
# File 'lib/postjob/job.rb', line 7

def self.find(job_id)
  scope = Postjob::Queue.search("postjobs", id: job_id)
  Simple::SQL.ask(scope, into: Postjob::Job)
end

Instance Method Details

#resolveObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/postjob/job.rb', line 40

def resolve
  expect! status => STATUSES

  case status
  when "ok"       then result
  when "ready"    then :pending
  when "processing" then :pending
  when "sleep"    then :pending
  when "timeout"  then raise Timeout::Error
  when "err"      then :pending
  when "failed"   then raise Postjob::Error::Nonrecoverable, self
  end
end

#resolved?Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/postjob/job.rb', line 54

def resolved?
  expect! status => STATUSES
  %w(ok timeout failed).include?(status)
end

#resultObject



59
60
61
# File 'lib/postjob/job.rb', line 59

def result
  results && results.first
end

#to_sObject



63
64
65
66
67
68
69
70
# File 'lib/postjob/job.rb', line 63

def to_s
  full_workflow = workflow
  full_workflow += "@#{workflow_version}" if workflow_version != ""
  full_workflow += ".#{workflow_method}" if workflow_method != "run"

  args = (self.args || []).map(&:inspect).join(", ")
  "Postjob##{full_id}: #{full_workflow}(#{args}) (#{status})"
end