Class: SknUtils::ConcurrentJobs

Inherits:
Object
  • Object
show all
Defined in:
lib/skn_utils/concurrent_jobs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker:) ⇒ ConcurrentJobs

Returns a new instance of ConcurrentJobs.



65
66
67
68
# File 'lib/skn_utils/concurrent_jobs.rb', line 65

def initialize(worker:)
  @worker  = worker
  @workers = []
end

Instance Attribute Details

#elapsed_time_stringObject (readonly)

Returns the value of attribute elapsed_time_string.



58
59
60
# File 'lib/skn_utils/concurrent_jobs.rb', line 58

def elapsed_time_string
  @elapsed_time_string
end

Class Method Details

.call(async: true) ⇒ Object



60
61
62
63
# File 'lib/skn_utils/concurrent_jobs.rb', line 60

def self.call(async: true)
  worker = async ? AsyncWorker : SyncWorker
  new(worker: worker)
end

Instance Method Details

#register_job(&blk) ⇒ Object



81
82
83
# File 'lib/skn_utils/concurrent_jobs.rb', line 81

def register_job(&blk)
  @workers << @worker.new(&blk)
end

#register_jobs(commands, callable) ⇒ Object

commands: array of command objects related to callable callable: callable class or proc, ex:SknUtils::HttpProcessor callable must return SknSuccess || SknFailure



73
74
75
76
77
78
79
# File 'lib/skn_utils/concurrent_jobs.rb', line 73

def register_jobs(commands, callable)
  commands.each do |command|
    register_job do
      JobWrapper.call(command,callable)
    end
  end
end

#render_jobsObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/skn_utils/concurrent_jobs.rb', line 85

def render_jobs
  stime = SknUtils.duration
  merged = @workers.each_with_object([]) do |worker, acc|
    acc.push( worker.call )
  end
  @elapsed_time_string = SknUtils.duration(stime)
  Result.new(merged)
rescue => e
  Result.new(merged || [])
end