Class: UberTask::TaskContext
- Inherits:
-
Object
- Object
- UberTask::TaskContext
- Defined in:
- lib/uber_task/task_context.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#retry_info ⇒ Object
readonly
Returns the value of attribute retry_info.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#subtasks ⇒ Object
readonly
Returns the value of attribute subtasks.
-
#vital ⇒ Object
readonly
Returns the value of attribute vital.
Instance Method Summary collapse
- #add_subtask(context) ⇒ Object
- #enter_task ⇒ Object
- #execute ⇒ Object
- #execute_handler(key, *args) ⇒ Object
- #execute_handler_chain(task, key, *args) ⇒ Object
- #exit_task ⇒ Object
- #full_location ⇒ Object
- #full_name ⇒ Object
-
#initialize(name:, retry_info:, vital: true, &block) ⇒ TaskContext
constructor
A new instance of TaskContext.
- #level ⇒ Object
- #report(level = ::Logger::INFO) ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def body @body end |
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def handlers @handlers end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def location @location end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def parent @parent end |
#retry_info ⇒ Object (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 |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def scope @scope end |
#subtasks ⇒ Object (readonly)
Returns the value of attribute subtasks.
7 8 9 |
# File 'lib/uber_task/task_context.rb', line 7 def subtasks @subtasks end |
#vital ⇒ Object (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_task ⇒ Object
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 { } end |
#execute ⇒ Object
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_task ⇒ Object
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 { } end |
#full_location ⇒ Object
128 129 130 |
# File 'lib/uber_task/task_context.rb', line 128 def full_location "#{@location.path}:" + @location.line.to_s.red end |
#full_name ⇒ Object
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 |
#level ⇒ Object
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) = yield execute_handler_chain(self, :report, , level) end |