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.



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

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

Instance Attribute Details

#elapsed_time_stringObject (readonly)

Returns the value of attribute elapsed_time_string.



73
74
75
# File 'lib/skn_utils/concurrent_jobs.rb', line 73

def elapsed_time_string
  @elapsed_time_string
end

Class Method Details

.call(async: true) ⇒ Object



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

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

Instance Method Details

#register_job(&blk) ⇒ Object



96
97
98
# File 'lib/skn_utils/concurrent_jobs.rb', line 96

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



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

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

#render_jobsObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/skn_utils/concurrent_jobs.rb', line 100

def render_jobs
  stime = SknUtils.duration
  merged = @workers.each_with_object([]) do |worker, acc|
    begin
      res = worker.call
      acc.push( res.nil? ? SknFailure.("Unknown", {cause: "Nil Return Value to render Jobs", backtrace: []}) : res )
    rescue => ex
      failures = ex.backtrace.map {|x| x.split("/").last }.join(",")
      acc.push SknFailure.(ex.class.name, { cause: ex.message, backtrace: failures})
    end
  end
  @elapsed_time_string = SknUtils.duration(stime)
  Result.new(merged)
rescue => e
  Result.new(merged || [])
end