class RuneRb::System::Types::Task
A Task
object encapsulates a Fiber that will execute code, then transfer execution context to a target Fiber.
Attributes
@!attribute [r] id @return [Integer, Symbol] the Task
ID.
@!attribute [r] priority @return [Symbol] the Task
priority.
@!attribute [r] target @return [Fiber] the target Fiber.
@!attribute [r] worker @return [Fiber] the Fiber that will execute the Task
.
Public Class Methods
Constructs a new Task
object. @param params [Hash] Initial parameters for the Task
. @param _ [Proc] parameters passed to the block operations
# File deployment/app/system/types/task.rb, line 26 def initialize(params = {}, &_) @id = params[:id] || Druuid.gen @assets = params[:assets] || [] @priority = params[:priority] || :LOW @worker = Fiber.new do |assets| loop do result = yield(assets) rescue StandardError => e err 'An error occurred while processing task!', e, e.message, e.backtrace&.join("\n") break ensure break if @completed @target ? @target.transfer(result) : Fiber.yield(result) end end start if params[:autorun] end
Public Instance Methods
# File deployment/app/system/types/task.rb, line 66 def inspect "[id]: #{@id}\t||\t[Priority]: #{@priority}" end
Calls <@worker#resume> causing it to process its work.
# File deployment/app/system/types/task.rb, line 46 def start @worker.resume(@assets) end
Sets the value of <@completed> to true, preventing further execution of the <@worker>.
# File deployment/app/system/types/task.rb, line 51 def stop @completed = true # We do this to ensure the loop completes start end
Update the <@target> Fiber to the passed object. @param fiber [Fiber] the new target Fiber. @todo create a InvalidTarget error to properly handle scenarios where a Fiber isn't supplied to this function.
# File deployment/app/system/types/task.rb, line 60 def target_to(fiber) raise "Invalid target; must be a Fiber! Received: #{fiber.class}" unless fiber.is_a?(Fiber) @target = fiber end