Class: Async::Runner::Job

Inherits:
Object
  • Object
show all
Includes:
Console
Defined in:
lib/async/runner/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, **options) ⇒ Job

Returns a new instance of Job.



8
9
10
11
12
13
14
# File 'lib/async/runner/job.rb', line 8

def initialize(file, **options)
  path = File.realpath(file)
  @options = options
  @barrier = Async::Barrier.new
  @semaphore = Async::Semaphore.new(options[:jobs], parent: @barrier)
  instance_eval File.read(path), path
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



6
7
8
# File 'lib/async/runner/job.rb', line 6

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/async/runner/job.rb', line 6

def options
  @options
end

Class Method Details

.to_sObject



65
66
67
# File 'lib/async/runner/job.rb', line 65

def self.to_s
  "Runner"
end

Instance Method Details

#async(&block) ⇒ Object



45
46
47
48
49
50
# File 'lib/async/runner/job.rb', line 45

def async(&block)
  @semaphore.async do
    block.call
    @progress&.increment
  end
end

#before(container = nil, options = nil, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/async/runner/job.rb', line 16

def before(container=nil, options=nil, &block)
  if block_given?
    @before_block = block
  else
    @before_block&.call(container, options)
  end
end

#executeObject



32
33
34
35
# File 'lib/async/runner/job.rb', line 32

def execute
  @block.call(job)
  @barrier.wait
end

#introObject



60
61
62
63
# File 'lib/async/runner/job.rb', line 60

def intro
  type = options[:container].to_s.capitalize
  Console.logger.info "=> Starting #{type} async containers"
end

#jobObject



24
25
26
# File 'lib/async/runner/job.rb', line 24

def job
  self
end

#limit(limit) ⇒ Object



37
38
39
# File 'lib/async/runner/job.rb', line 37

def limit(limit)
  @semaphore = Async::Semaphore.new(limit, parent: @barrier)
end

#progress(total:) ⇒ Object



41
42
43
# File 'lib/async/runner/job.rb', line 41

def progress(total:)
  @progress = logger.progress("jobs", total)
end

#run(&block) ⇒ Object



28
29
30
# File 'lib/async/runner/job.rb', line 28

def run(&block)
  @block = block
end

#setup(&block) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/async/runner/job.rb', line 52

def setup(&block)
  if block_given?
    @setup_block = block
  else
    @setup_block&.call job
  end
end