Class: Tumugi::Executor::LocalExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/tumugi/executor/local_executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(dag, worker_num: 1) ⇒ LocalExecutor

Returns a new instance of LocalExecutor.



10
11
12
13
14
15
# File 'lib/tumugi/executor/local_executor.rb', line 10

def initialize(dag, worker_num: 1)
  @dag = dag
  @main_task = dag.tsort.last
  @options = { worker_num: worker_num }
  @mutex = Mutex.new
end

Instance Method Details

#executeObject



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
# File 'lib/tumugi/executor/local_executor.rb', line 17

def execute
  pool = Concurrent::ThreadPoolExecutor.new(
    min_threads: @options[:worker_num],
    max_threads: @options[:worker_num]
  )

  setup_task_queue(@dag)
  loop do
    task = dequeue_task
    break if task.nil?

    Concurrent::Future.execute(executor: pool) do
      if !task.runnable?(Time.now)
        logger.trace { "task_cannot_run: #{task.id}" }
        enqueue_task(task)
      else
        begin
          logger.info { "task_start: #{task.id}" }
          task.trigger!(:start)
          MuchTimeout.optional_timeout(task_timeout(task), Tumugi::TimeoutError) do
            task.run
          end
          task.trigger!(:complete)
          logger.info { "task_#{task.state}: #{task.id}" }
        rescue => e
          handle_error(task, e)
        end
      end
    end
  end

  pool.shutdown
  pool.wait_for_termination

  @dag.tsort.all? { |t| t.success? }
end