Class: Gush::Job

Inherits:
Object
  • Object
show all
Includes:
Metadata
Defined in:
lib/gush/job.rb

Constant Summary collapse

RECURSION_LIMIT =
1000
DEFAULTS =
{
  finished: false,
  enqueued: false,
  failed: false,
  running: false
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metadata

included

Constructor Details

#initialize(opts = {}) ⇒ Job

Returns a new instance of Job.



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

def initialize(opts = {})
  options = DEFAULTS.dup.merge(opts)
  assign_variables(options)
end

Instance Attribute Details

#enqueuedObject

Returns the value of attribute enqueued.



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

def enqueued
  @enqueued
end

#failedObject

Returns the value of attribute failed.



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

def failed
  @failed
end

#failed_atObject

Returns the value of attribute failed_at.



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

def failed_at
  @failed_at
end

#finishedObject

Returns the value of attribute finished.



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

def finished
  @finished
end

#finished_atObject

Returns the value of attribute finished_at.



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

def finished_at
  @finished_at
end

#incomingObject

Returns the value of attribute incoming.



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

def incoming
  @incoming
end

#jidObject

Returns the value of attribute jid.



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

def jid
  @jid
end

#loggerObject



137
138
139
140
# File 'lib/gush/job.rb', line 137

def logger
  fail "You cannot log when the job is not running" unless running?
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#outgoingObject

Returns the value of attribute outgoing.



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

def outgoing
  @outgoing
end

#runningObject

Returns the value of attribute running.



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

def running
  @running
end

#started_atObject

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#workflow_idObject

Returns the value of attribute workflow_id.



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

def workflow_id
  @workflow_id
end

Class Method Details

.from_hash(hash) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gush/job.rb', line 48

def self.from_hash(hash)
  hash[:klass].constantize.new(
    name:     hash[:name],
    finished: hash[:finished],
    enqueued: hash[:enqueued],
    failed: hash[:failed],
    incoming: hash[:incoming],
    outgoing: hash[:outgoing],
    failed_at: hash[:failed_at],
    finished_at: hash[:finished_at],
    started_at: hash[:started_at],
    running: hash[:running]
  )
end

Instance Method Details

#after_workObject



69
70
# File 'lib/gush/job.rb', line 69

def after_work
end

#as_jsonObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gush/job.rb', line 28

def as_json
  {
    name: @name,
    klass: self.class.to_s,
    finished: @finished,
    enqueued: @enqueued,
    failed: @failed,
    incoming: @incoming,
    outgoing: @outgoing,
    finished_at: @finished_at,
    started_at: @started_at,
    failed_at: @failed_at,
    running: @running
  }
end

#before_workObject



63
64
# File 'lib/gush/job.rb', line 63

def before_work
end

#can_be_started?(flow) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
# File 'lib/gush/job.rb', line 124

def can_be_started?(flow)
  !running? &&
    !enqueued? &&
      !finished? &&
        !failed? &&
          dependencies_satisfied?(flow)
end

#dependencies(flow, level = 0) ⇒ Object



132
133
134
135
# File 'lib/gush/job.rb', line 132

def dependencies(flow, level = 0)
  fail DependencyLevelTooDeep if level > RECURSION_LIMIT
  (incoming.map {|name| flow.find_job(name) } + incoming.flat_map{ |name| flow.find_job(name).dependencies(flow, level + 1) }).uniq
end

#enqueue!Object



78
79
80
81
82
83
84
85
# File 'lib/gush/job.rb', line 78

def enqueue!
  @enqueued = true
  @running = false
  @failed = false
  @started_at = nil
  @finished_at = nil
  @failed_at = nil
end

#enqueued?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/gush/job.rb', line 104

def enqueued?
  !!enqueued
end

#fail!Object



95
96
97
98
99
100
101
102
# File 'lib/gush/job.rb', line 95

def fail!
  @finished = true
  @running = false
  @failed = true
  @enqueued = false
  @finished_at = Time.now.to_i
  @failed_at = Time.now.to_i
end

#failed?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/gush/job.rb', line 112

def failed?
  !!failed
end

#finish!Object



87
88
89
90
91
92
93
# File 'lib/gush/job.rb', line 87

def finish!
  @running = false
  @finished = true
  @enqueued = false
  @failed = false
  @finished_at = Time.now.to_i
end

#finished?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/gush/job.rb', line 108

def finished?
  !!finished
end

#running?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/gush/job.rb', line 120

def running?
  !!running
end

#start!Object



72
73
74
75
76
# File 'lib/gush/job.rb', line 72

def start!
  @enqueued = false
  @running = true
  @started_at = Time.now.to_i
end

#succeeded?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/gush/job.rb', line 116

def succeeded?
  finished? && !failed?
end

#to_json(options = {}) ⇒ Object



44
45
46
# File 'lib/gush/job.rb', line 44

def to_json(options = {})
  Yajl::Encoder.new.encode(as_json)
end

#workObject



66
67
# File 'lib/gush/job.rb', line 66

def work
end