Class: PWork::Async::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/pwork/async/manager.rb

Instance Method Summary collapse

Instance Method Details

#add_task(task) ⇒ Object

This method is called to add a task to the queue



29
30
31
32
33
34
35
# File 'lib/pwork/async/manager.rb', line 29

def add_task(task)
  raise PWork::Async::Exceptions::InvalidOptionsError.new(
    'A valid async task must be specified.'
  ) unless task.is_a?(PWork::Async::Task)
  task.thread_local_storage = PWork::Helpers::Threads.get_thread_vars
  process_task(task)
end

#process_task(task) ⇒ Object

This method is called to process an async task from the queue



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pwork/async/manager.rb', line 7

def process_task(task)
  task.thread = Thread.new do
    task.state = :active
    thread_helper.set_thread_vars(task.thread_local_storage)
    begin
      task.block.call
      task.state = :complete
    rescue => e
      task.error = e
      task.state = :error
    ensure
      thread_helper.reset_thread_vars
    end
  end
end

#thread_helperObject

This method is called to expose the thread helper



24
25
26
# File 'lib/pwork/async/manager.rb', line 24

def thread_helper
  PWork::Helpers::Threads
end