Class: DRbQS::Task::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/task/task_generator.rb

Constant Summary collapse

DEBUG_TASK_PROGRESS =
1000

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • data (Hash) (defaults to: {})

    This argument is unnecessary and so deprecated Names of instance variables and their values, which can be accessed in #set.



7
8
9
10
11
12
13
14
# File 'lib/drbqs/task/task_generator.rb', line 7

def initialize(data = {})
  @registrar = DRbQS::Task::Registrar.new(data)
  @fiber = nil
  @iterate = nil
  @task_set = nil
  @fiber_init = nil
  @wait = false
end

Instance Method Details

#debug_all_tasks(opts = {}) ⇒ Object

Create all tasks for test and return [group_number, task_number] if all tasks created properly.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/drbqs/task/task_generator.rb', line 97

def debug_all_tasks(opts = {})
  limit = opts[:limit]
  progress = opts[:progress]
  group_number = 0
  task_number = 0
  while ary = new_tasks
    ary.each do |t|
      unless DRbQS::Task === t
        raise RuntimeError, "Invalid #{i}th task: #{t.inspect}"
      end
      task_number += 1
      if progress && (task_number % DEBUG_TASK_PROGRESS == 0)
        puts "#{task_number} tasks have been created."
      end
      if limit && task_number > limit
        break
      end
    end
    group_number += 1
  end
  [group_number, task_number]
end

#have_next?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/drbqs/task/task_generator.rb', line 16

def have_next?
  !!@fiber
end

#initObject

Initialize fider to create tasks. This method must be called in thread to create tasks.



53
54
55
# File 'lib/drbqs/task/task_generator.rb', line 53

def init
  @fiber_init.call if @fiber_init
end

#new_tasksObject

Return an array of new tasks.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/drbqs/task/task_generator.rb', line 58

def new_tasks
  if @fiber
    @wait = false
    task_ary = []
    iteration = @iterate
    iteration *= @task_set if @task_set
    iteration.times do |i|
      if task_new = @fiber.resume
        case task_new
        when DRbQS::Task
          task_ary << task_new
        when Array
          task_ary.concat(task_new)
        when :wait
          @wait = true
          break
        else
          raise RuntimeError, "Invalid object created by fiber to create tasks."
        end
      else
        @fiber = nil
        break
      end
    end
    if task_ary.size > 0
      if @task_set
        task_ary = task_ary.each_slice(@task_set).map do |ary|
          DRbQS::Task::TaskSet.new(ary)
        end
      end
      return task_ary
    end
  end
  nil
end

#set(opts = {}, &block) ⇒ Object

Specify how to generate tasks.

Parameters:

  • opts (Hash) (defaults to: {})

    The options of task generation

Options Hash (opts):

  • :generate (Fixnum)

    Set the number of tasks per one generation

  • :collect (Fixnum)

    The generator creates a task set consisting of opts[:collect] tasks.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/drbqs/task/task_generator.rb', line 28

def set(opts = {}, &block)
  unless block_given?
    raise ArgumentError, "Creation of a task generator needs block."
  end
  @iterate = opts[:generate] || 1
  @task_set = opts[:collect]
  if @iterate < 1 || (@task_set && @task_set < 1)
    raise ArgumentError, "Invalid options of task creation on generator."
  end
  @fiber_init = lambda do
    @fiber = Fiber.new do
      begin
        @registrar.instance_eval(&block)
      rescue => err
        new_err = err.class.new("Error on generating tasks: #{err.to_s} (#{err.class})")
        new_err.set_backtrace(err.backtrace)
        raise new_err
      end
      nil
    end
  end
end

#waiting?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/drbqs/task/task_generator.rb', line 20

def waiting?
  @wait
end