Class: Legate::Tools::SleepyWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/legate/tools/sleepy_tool.rb

Overview

The worker that performs the actual sleep operation.

Instance Method Summary collapse

Instance Method Details

#perform(jid, duration, message) ⇒ Object

Parameters:

  • jid (String)

    The Job ID (passed as first argument by BaseAsyncJobTool).

  • duration (Integer)

    How long to sleep in seconds.

  • message (String)

    The message to return upon completion.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/legate/tools/sleepy_tool.rb', line 47

def perform(jid, duration, message)
  # --- Store initial pending status --- #
  begin
    Legate::Tools::BaseAsyncJobTool.store_job_pending(jid)
  rescue StandardError => e
    # Log error but try to continue if possible
    Legate.logger.error("[SleepyWorker JID: #{jid}] Failed to store initial pending status: #{e.message}")
  end
  # --- End store initial pending status --- #

  Legate.logger.info("[SleepyWorker JID: #{jid}] Starting job. Sleeping for #{duration} seconds...")

  begin
    sleep duration.to_i
    result_message = "Slept for #{duration} seconds. Your message: #{message}"
    Legate.logger.info("[SleepyWorker JID: #{jid}] Job finished. Storing result.")
    # Store the successful result using the helper from BaseAsyncJobTool
    Legate::Tools::BaseAsyncJobTool.store_job_result(jid, result_message)
  rescue StandardError => e
    error_message = "Job failed after starting sleep: #{e.message}"
    Legate.logger.error("[SleepyWorker JID: #{jid}] Job failed! Storing error. Error: #{error_message}")
    # Store the error using the helper
    Legate::Tools::BaseAsyncJobTool.store_job_error(jid, error_message, e.class.name)
  end
end