Class: RCron::Task::TaskThread

Inherits:
Object
  • Object
show all
Defined in:
lib/rcron/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, started_at) ⇒ TaskThread

Returns a new instance of TaskThread.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rcron/task.rb', line 137

def initialize task, started_at
  @started_at = started_at
  @thread = Thread.new {
    begin
      task.run
      @ended_at = Time.now

      task.rcron.send :info, "[#{task.name}] completed (#{'%.2f' % elapsed}s)"
    rescue Exception => e
      @ended_at = Time.now

      task.rcron.send :error, "[#{task.name}] terminated: #{[e, e.backtrace].join($/)}"
      # Ignore exception?
    end
    task.rcron.send :wake_up
  }
end

Instance Attribute Details

#ended_atObject (readonly)

Returns the value of attribute ended_at.



134
135
136
# File 'lib/rcron/task.rb', line 134

def ended_at
  @ended_at
end

#exceptionObject (readonly)

Returns the value of attribute exception.



135
136
137
# File 'lib/rcron/task.rb', line 135

def exception
  @exception
end

#started_atObject (readonly)

Returns the value of attribute started_at.



134
135
136
# File 'lib/rcron/task.rb', line 134

def started_at
  @started_at
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/rcron/task.rb', line 163

def alive?
  @thread.alive?
end

#cleanupObject



155
156
157
# File 'lib/rcron/task.rb', line 155

def cleanup
  @thread.join
end

#elapsedObject



159
160
161
# File 'lib/rcron/task.rb', line 159

def elapsed
  (@ended_at || Time.now) - @started_at
end

#kill!Object



167
168
169
170
# File 'lib/rcron/task.rb', line 167

def kill!
  @thread.raise RCron::Timeout.new("Timeout: task terminated by rcron", @started_at, Time.now)
  cleanup
end