Class: Kie::GeneralTask

Inherits:
Object
  • Object
show all
Includes:
Task
Defined in:
lib/kie/general_task.rb

Overview

Represents an asynchronous task for General API endpoints

Constant Summary collapse

RECORD_INFO_ENDPOINT =
"/api/v1/jobs/recordInfo"
STATUS_MAP =

Status mapping from General API raw states to normalized statuses

{
  waiting: :processing,
  generating: :processing,
  success: :success,
  fail: :failed
}.freeze

Constants included from Task

Task::NORMALIZED_STATUSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Task

#completed?, #failed?, #processing?, #success?, #wait

Constructor Details

#initialize(client:, task_id:) ⇒ GeneralTask

Returns a new instance of GeneralTask.



24
25
26
27
28
29
30
31
32
# File 'lib/kie/general_task.rb', line 24

def initialize(client:, task_id:)
  @client = client
  @task_id = task_id
  @raw_status = nil
  @result_urls = []
  @cost_time = nil
  @fail_code = nil
  @fail_message = nil
end

Instance Attribute Details

#cost_timeObject (readonly)

Returns the value of attribute cost_time.



20
21
22
# File 'lib/kie/general_task.rb', line 20

def cost_time
  @cost_time
end

#fail_codeObject (readonly)

Returns the value of attribute fail_code.



20
21
22
# File 'lib/kie/general_task.rb', line 20

def fail_code
  @fail_code
end

#fail_messageObject (readonly)

Returns the value of attribute fail_message.



20
21
22
# File 'lib/kie/general_task.rb', line 20

def fail_message
  @fail_message
end

#raw_statusObject (readonly)

Returns the value of attribute raw_status.



20
21
22
# File 'lib/kie/general_task.rb', line 20

def raw_status
  @raw_status
end

#task_idObject (readonly) Also known as: id

Returns the value of attribute task_id.



20
21
22
# File 'lib/kie/general_task.rb', line 20

def task_id
  @task_id
end

Instance Method Details

#error_messageObject

Returns the error message from the failed task, or nil if not failed



56
57
58
# File 'lib/kie/general_task.rb', line 56

def error_message
  @fail_message
end

#pending?Boolean

Returns true when task is in pending/waiting state (not yet processing)

Returns:

  • (Boolean)


61
62
63
# File 'lib/kie/general_task.rb', line 61

def pending?
  @raw_status.nil? || @raw_status == :waiting
end

#raise_task_error!Object

Raises the appropriate exception based on the failure reason

Raises:

  • (error_class)


66
67
68
69
# File 'lib/kie/general_task.rb', line 66

def raise_task_error!
  error_class = @fail_code == "CONTENT_POLICY" ? ContentPolicyError : TaskFailedError
  raise error_class.new(@fail_message, fail_code: @fail_code)
end

#refresh!Object



34
35
36
37
38
# File 'lib/kie/general_task.rb', line 34

def refresh!
  response = @client.connection.get(RECORD_INFO_ENDPOINT, taskId: @task_id)
  handle_response(response)
  self
end

#statusObject

Returns normalized status (:processing, :success, :failed)



41
42
43
44
45
# File 'lib/kie/general_task.rb', line 41

def status
  return :processing if @raw_status.nil?

  STATUS_MAP.fetch(@raw_status, :processing)
end

#urlsObject Also known as: result_urls

Returns array of result URLs



48
49
50
# File 'lib/kie/general_task.rb', line 48

def urls
  @result_urls
end