Class: Threatstack::Jobs::RecurrentJob
- Inherits:
-
Object
- Object
- Threatstack::Jobs::RecurrentJob
- Defined in:
- lib/jobs/recurrent_job.rb
Instance Attribute Summary collapse
-
#exec_count ⇒ Object
readonly
Returns the value of attribute exec_count.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#stopped ⇒ Object
readonly
Returns the value of attribute stopped.
Instance Method Summary collapse
-
#initialize(name_or_logger, repeat_every_sec = 10, initial_delay_sec = 0, total_times = nil, *args, &block) ⇒ RecurrentJob
constructor
A new instance of RecurrentJob.
- #stop ⇒ Object
Constructor Details
#initialize(name_or_logger, repeat_every_sec = 10, initial_delay_sec = 0, total_times = nil, *args, &block) ⇒ RecurrentJob
Returns a new instance of RecurrentJob.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jobs/recurrent_job.rb', line 15 def initialize(name_or_logger, repeat_every_sec = 10, initial_delay_sec = 0, total_times = nil, *args, &block) raise 'Block not specified' unless block_given? # create logger or use passed one if available if name_or_logger.respond_to?(:debug) && name_or_logger.respond_to?(:info) && name_or_logger.respond_to?(:error) @logger = name_or_logger else @logger = Threatstack::Utils::TSLogger.create "#{name_or_logger}Job" end @stopped = false @exec_count = 0 @logger.debug "Creating recurrent job with #{initial_delay_sec} sec delay, repeat every #{repeat_every_sec} sec, #{total_times || 'infinite'} time(s)" Thread.new do # wait before starting sleep initial_delay_sec # repeat every X secs Thread.every(repeat_every_sec) do @logger.debug 'Starting job iteration...' # stop if stop method called or exec limit reached if @stopped || (!total_times.nil? && @exec_count == total_times) @logger.debug 'Job stopped' @stopped = true Thread.stop return end # increment @exec_count += 1 # perform main action begin @logger.debug 'Calling job action' block.call *args @logger.debug 'Job action called' rescue StandardError => e @logger.error "StandardError in job action: #{e.inspect}" end end end end |
Instance Attribute Details
#exec_count ⇒ Object (readonly)
Returns the value of attribute exec_count.
11 12 13 |
# File 'lib/jobs/recurrent_job.rb', line 11 def exec_count @exec_count end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/jobs/recurrent_job.rb', line 13 def logger @logger end |
#stopped ⇒ Object (readonly)
Returns the value of attribute stopped.
12 13 14 |
# File 'lib/jobs/recurrent_job.rb', line 12 def stopped @stopped end |
Instance Method Details
#stop ⇒ Object
55 56 57 58 |
# File 'lib/jobs/recurrent_job.rb', line 55 def stop @logger.debug 'Stopping recurrent job' @stopped = true end |