Class: Isono::NodeModules::JobWorker::JobContext

Inherits:
OpenStruct
  • Object
show all
Includes:
Logger
Defined in:
lib/isono/node_modules/job_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

included, initialize

Constructor Details

#initializeJobContext

Returns a new instance of JobContext.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/isono/node_modules/job_worker.rb', line 113

def initialize()
  super({:job_id=>Util.gen_id,
          :parent_job_id=>nil,
          :started_at=>nil,
          :finished_at=>nil,
          :finish_status=>nil,
        })

  @run_cb=proc{}
  @fail_cb=nil

  @state = :init
end

Instance Attribute Details

#fail_cbObject

Returns the value of attribute fail_cb.



110
111
112
# File 'lib/isono/node_modules/job_worker.rb', line 110

def fail_cb
  @fail_cb
end

#run_cbObject

Returns the value of attribute run_cb.



110
111
112
# File 'lib/isono/node_modules/job_worker.rb', line 110

def run_cb
  @run_cb
end

#stateObject (readonly)

Returns the value of attribute state.



111
112
113
# File 'lib/isono/node_modules/job_worker.rb', line 111

def state
  @state
end

Instance Method Details

#elapsed_timeObject



152
153
154
155
156
157
158
# File 'lib/isono/node_modules/job_worker.rb', line 152

def elapsed_time
  if finished_at && started_at
    finished_at - started_at
  else
    0
  end
end

#process_event(ev, *args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/isono/node_modules/job_worker.rb', line 127

def process_event(ev, *args)
  case [ev,@state]
  when [:on_start, :init]
    @state = :running
    self.started_at = Time.now
    logger.info("Job start #{job_id}")
  when [:on_done, :running]
    @state = :done
    self.finished_at = Time.now
    logger.info("Job complete #{job_id}: #{elapsed_time} sec")
  when [:on_fail, :running]
    @state = :failed
    on_fail(args[0])
  when [:on_fail, :init]
    @state = :failed
    on_fail(args[0])
  else
    raise "Unknown state transition: #{ev}, #{@state}"
  end
end

#to_hashObject



148
149
150
# File 'lib/isono/node_modules/job_worker.rb', line 148

def to_hash
  @table.dup.merge({:state=>@state})
end