Class: DirectoryWatcher::Threaded::ThreadContainer

Inherits:
Struct
  • Object
show all
Defined in:
lib/directory_watcher/threaded.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#continue_on_errorObject

Returns the value of attribute continue_on_error

Returns:

  • (Object)

    the current value of continue_on_error



212
213
214
# File 'lib/directory_watcher/threaded.rb', line 212

def continue_on_error
  @continue_on_error
end

#intervalObject

Returns the value of attribute interval

Returns:

  • (Object)

    the current value of interval



212
213
214
# File 'lib/directory_watcher/threaded.rb', line 212

def interval
  @interval
end

#iterationsObject

Returns the value of attribute iterations

Returns:

  • (Object)

    the current value of iterations



212
213
214
# File 'lib/directory_watcher/threaded.rb', line 212

def iterations
  @iterations
end

#maximum_iterationsObject

Returns the value of attribute maximum_iterations

Returns:

  • (Object)

    the current value of maximum_iterations



212
213
214
# File 'lib/directory_watcher/threaded.rb', line 212

def maximum_iterations
  @maximum_iterations
end

#runningObject Also known as: running?

Returns the value of attribute running

Returns:

  • (Object)

    the current value of running



212
213
214
# File 'lib/directory_watcher/threaded.rb', line 212

def running
  @running
end

#threadObject

Returns the value of attribute thread

Returns:

  • (Object)

    the current value of thread



212
213
214
# File 'lib/directory_watcher/threaded.rb', line 212

def thread
  @thread
end

#workingObject

Returns the value of attribute working

Returns:

  • (Object)

    the current value of working



212
213
214
# File 'lib/directory_watcher/threaded.rb', line 212

def working
  @working
end

Instance Method Details

#do_work(threaded) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/directory_watcher/threaded.rb', line 256

def do_work( threaded )
  if working then
    threaded.run

    if maximum_iterations
      self.iterations += 1
      if finished_iterations?
        self.running = false
      end
    end
  end
end

#finished_iterations?Boolean

Returns:

  • (Boolean)


269
270
271
272
# File 'lib/directory_watcher/threaded.rb', line 269

def finished_iterations?
  return true if maximum_iterations and (iterations >= maximum_iterations)
  return false
end

#join(limit = nil) ⇒ Object



251
252
253
254
# File 'lib/directory_watcher/threaded.rb', line 251

def join( limit = nil )
  return if thread.nil?
  limit ? thread.join(limit) : thread.join
end

#run(threaded) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/directory_watcher/threaded.rb', line 227

def run( threaded )
  loop do
    begin
      break unless running?
      do_work( threaded )

      sleep interval if running?
    rescue SystemExit; raise
    rescue Exception => err
      if continue_on_error
        $stderr.puts err
      else
        $stderr.puts err
        raise err
      end
    end
  end
ensure
  if threaded.respond_to?(:after_stopping) and !self.running
    threaded.after_stopping
  end
  self.running = false
end

#start(threaded) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/directory_watcher/threaded.rb', line 213

def start( threaded )

  self.working = true
  self.running = true
  self.iterations = 0
  self.thread = Thread.new { run threaded }
  Thread.pass
end

#stopObject



222
223
224
225
# File 'lib/directory_watcher/threaded.rb', line 222

def stop
  self.running = false
  thread.wakeup
end