Class: UberTask::TaskContext

Inherits:
Object
  • Object
show all
Defined in:
lib/uber_task/task_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, retry_info:, vital: true, &block) ⇒ TaskContext

Returns a new instance of TaskContext.



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
# File 'lib/uber_task/task_context.rb', line 18

def initialize(
  name:,
  retry_info:,
  vital: true,
  &block
)
  @body = block
  @handlers = {
    retry: nil,
    report: nil,
    skip: nil,
    subtask_error: nil,
    success: nil,
  }
  @location = Location.new(
    path: block.source_location[0],
    line: block.source_location[1],
  )
  @method = @body.binding.eval('__method__')
  @name = name
  @parent = UberTask.current
  @parent&.add_subtask(self)
  @retry_info = retry_info
  @scope = @body.binding.eval('self')
  @vital = vital
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def body
  @body
end

#handlersObject (readonly)

Returns the value of attribute handlers.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def handlers
  @handlers
end

#locationObject (readonly)

Returns the value of attribute location.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def location
  @location
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def parent
  @parent
end

#retry_infoObject (readonly)

Returns the value of attribute retry_info.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def retry_info
  @retry_info
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def scope
  @scope
end

#subtasksObject (readonly)

Returns the value of attribute subtasks.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def subtasks
  @subtasks
end

#vitalObject (readonly)

Returns the value of attribute vital.



7
8
9
# File 'lib/uber_task/task_context.rb', line 7

def vital
  @vital
end

Instance Method Details

#add_subtask(context) ⇒ Object



45
46
47
48
# File 'lib/uber_task/task_context.rb', line 45

def add_subtask(context)
  @subtasks ||= []
  @subtasks.push(context)
end

#enter_taskObject



96
97
98
99
100
101
# File 'lib/uber_task/task_context.rb', line 96

def enter_task
  thread = Thread.current
  thread[:__uber_task_stack__] ||= []
  thread[:__uber_task_stack__].push(self)
  Internal.trace { enter_message }
end

#executeObject



50
51
52
53
54
55
56
57
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
93
94
# File 'lib/uber_task/task_context.rb', line 50

def execute
  enter_task
  result = @scope.instance_exec(self) do |context|
    context.body.call(context)
  end
  execute_handler(:success)
  result
rescue RetryTask => err
  if retry?(err)
    exit_task
    retry
  end
rescue SkipTask => err
  execute_handler(:skip, err.reason)
# rubocop:disable Lint/RescueException
rescue Exception => err
  parent_asked_to_retry = false

  begin
    # We will allow ancestors to check errors from this task because in some
    # situations, like, for example, a connection issue, a parent task may
    # order the child to retry without having to put the error check on each
    # child task.
    @parent&.execute_handler_chain(self, :subtask_error, err)
  rescue RetryTask => err
    if !retry?(err)
      return
    end
    parent_asked_to_retry = true
  rescue SkipTask => err
    execute_handler(:skip, err.reason)
    return
  end

  if parent_asked_to_retry
    exit_task
    retry
  end

  # None of the parents asked to retry or skip the task.
  raise err
# rubocop:enable Lint/RescueException
ensure
  exit_task
end

#execute_handler(key, *args) ⇒ Object



109
110
111
112
113
# File 'lib/uber_task/task_context.rb', line 109

def execute_handler(key, *args)
  @scope.instance_exec(self) do |context|
    context.handlers[key]&.call(*args)
  end
end

#execute_handler_chain(task, key, *args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/uber_task/task_context.rb', line 115

def execute_handler_chain(task, key, *args)
  begin
    @scope.instance_exec(self) do |context|
      event = Event.new(key.to_s)
      context.handlers[key]&.call(task, event, *args)
    end
  rescue EventHandled
    return
  end

  @parent&.execute_handler_chain(task, key, *args)
end

#exit_taskObject



103
104
105
106
107
# File 'lib/uber_task/task_context.rb', line 103

def exit_task
  thread = Thread.current
  thread[:__uber_task_stack__].pop
  Internal.trace { exit_message }
end

#full_locationObject



128
129
130
# File 'lib/uber_task/task_context.rb', line 128

def full_location
  "#{@location.path}:" + @location.line.to_s.red
end

#full_nameObject



132
133
134
135
136
# File 'lib/uber_task/task_context.rb', line 132

def full_name
  result = @scope.to_s.magenta + ".#{@method}".cyan
  result += " #{@name.to_s.white}" unless @name.nil?
  result
end

#levelObject



138
139
140
# File 'lib/uber_task/task_context.rb', line 138

def level
  @parent.nil? ? 0 : @parent.level + 1
end

#report(level = ::Logger::INFO) ⇒ Object



142
143
144
145
# File 'lib/uber_task/task_context.rb', line 142

def report(level = ::Logger::INFO)
  message = yield
  execute_handler_chain(self, :report, message, level)
end