Class: Redom::Task

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/redom/task.rb

Instance Method Summary collapse

Methods included from Utils

#_dispatcher, #_logger, dispatcher=, logger=

Constructor Details

#initialize(conn, worker, info) ⇒ Task

Returns a new instance of Task.



5
6
7
8
9
10
# File 'lib/redom/task.rb', line 5

def initialize(conn, worker, info)
  @conn = conn
  @worker = worker
  @info = info
  @results = []
end

Instance Method Details

#_idObject



48
49
50
# File 'lib/redom/task.rb', line 48

def _id
  "T#{__id__}"
end

#resumeObject



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
# File 'lib/redom/task.rb', line 17

def resume
  if !@results.empty?
    result = @fiber.resume(@results.shift)
  else
    mthd = @conn.method(@info[T_INFO_METHOD_NAME])
    args = @info[T_INFO_ARGUMENTS]
    blck = @info[T_INFO_BLOCK]
    @fiber = Fiber.new do
      if mthd
        if blck
          mthd.call(*args, &blck)
        else
          mthd.call(*args)
        end
      else
        _logger.error "Method '#{@info[T_INFO_METHOD_NAME]}' is not defined in class '#{@conn.class}'."
        @conn.method(:on_error).call("No such method '#{@info[T_INFO_METHOD_NAME]}'.")
      end
      @conn.sync{}
      nil
    end
    result = @fiber.resume
  end

  if result
    result[0]._send [_id, result[1]].to_json
  else
    _dispatcher.delete_task _id
  end
end

#run(proxy_result = nil) ⇒ Object



12
13
14
15
# File 'lib/redom/task.rb', line 12

def run(proxy_result = nil)
  @results << proxy_result if proxy_result
  @worker.do_task self
end