Class: Kward::Workers::Job
- Inherits:
-
Object
- Object
- Kward::Workers::Job
- Defined in:
- lib/kward/workers/job.rb
Overview
Persistent queue entry for a session-backed worker.
Constant Summary collapse
- STATUSES =
%w[queued running suspended ready_for_review failed blocked cancelled archived].freeze
Instance Attribute Summary collapse
-
#commit_sha ⇒ Object
readonly
Returns the value of attribute commit_sha.
-
#enqueued_at ⇒ Object
readonly
Returns the value of attribute enqueued_at.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#finished_at ⇒ Object
readonly
Returns the value of attribute finished_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#session_path ⇒ Object
readonly
Returns the value of attribute session_path.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#stash_ref ⇒ Object
readonly
Returns the value of attribute stash_ref.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
-
#workspace_root ⇒ Object
readonly
Returns the value of attribute workspace_root.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id: SecureRandom.hex(4), title:, session_path:, workspace_root: Dir.pwd, status: "queued", position: nil, commit_sha: nil, stash_ref: nil, error: nil, enqueued_at: Time.now.utc, started_at: nil, finished_at: nil, updated_at: nil) ⇒ Job
constructor
A new instance of Job.
- #status ⇒ Object
- #to_h ⇒ Object
- #update_status(status, commit_sha: nil, stash_ref: nil, error: nil, position: nil) ⇒ Object
Constructor Details
#initialize(id: SecureRandom.hex(4), title:, session_path:, workspace_root: Dir.pwd, status: "queued", position: nil, commit_sha: nil, stash_ref: nil, error: nil, enqueued_at: Time.now.utc, started_at: nil, finished_at: nil, updated_at: nil) ⇒ Job
Returns a new instance of Job.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/kward/workers/job.rb', line 11 def initialize(id: SecureRandom.hex(4), title:, session_path:, workspace_root: Dir.pwd, status: "queued", position: nil, commit_sha: nil, stash_ref: nil, error: nil, enqueued_at: Time.now.utc, started_at: nil, finished_at: nil, updated_at: nil) @id = id.to_s @title = title.to_s @session_path = session_path.to_s @workspace_root = ConfigFiles.canonical_workspace_root(workspace_root) @status = status.to_s @position = position @commit_sha = commit_sha @stash_ref = stash_ref @error = error @enqueued_at = enqueued_at @started_at = started_at @finished_at = finished_at @updated_at = updated_at || enqueued_at end |
Instance Attribute Details
#commit_sha ⇒ Object (readonly)
Returns the value of attribute commit_sha.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def commit_sha @commit_sha end |
#enqueued_at ⇒ Object (readonly)
Returns the value of attribute enqueued_at.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def enqueued_at @enqueued_at end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def error @error end |
#finished_at ⇒ Object (readonly)
Returns the value of attribute finished_at.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def finished_at @finished_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def id @id end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def position @position end |
#session_path ⇒ Object (readonly)
Returns the value of attribute session_path.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def session_path @session_path end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def started_at @started_at end |
#stash_ref ⇒ Object (readonly)
Returns the value of attribute stash_ref.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def stash_ref @stash_ref end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def title @title end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def updated_at @updated_at end |
#workspace_root ⇒ Object (readonly)
Returns the value of attribute workspace_root.
27 28 29 |
# File 'lib/kward/workers/job.rb', line 27 def workspace_root @workspace_root end |
Class Method Details
.from_h(record) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kward/workers/job.rb', line 64 def self.from_h(record) new( id: record.fetch("id"), title: record.fetch("title"), session_path: record.fetch("session_path"), workspace_root: record["workspace_root"] || Dir.pwd, status: record["status"] || "queued", position: record["position"], commit_sha: record["commit_sha"], stash_ref: record["stash_ref"], error: record["error"], enqueued_at: parse_time(record["enqueued_at"]) || Time.now.utc, started_at: parse_time(record["started_at"]), finished_at: parse_time(record["finished_at"]), updated_at: parse_time(record["updated_at"]) ) end |
Instance Method Details
#status ⇒ Object
29 30 31 |
# File 'lib/kward/workers/job.rb', line 29 def status @status end |
#to_h ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kward/workers/job.rb', line 46 def to_h { "id" => id, "title" => title, "session_path" => session_path, "workspace_root" => workspace_root, "status" => status, "position" => position, "commit_sha" => commit_sha, "stash_ref" => stash_ref, "error" => error, "enqueued_at" => (enqueued_at), "started_at" => (started_at), "finished_at" => (finished_at), "updated_at" => (updated_at) } end |
#update_status(status, commit_sha: nil, stash_ref: nil, error: nil, position: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kward/workers/job.rb', line 33 def update_status(status, commit_sha: nil, stash_ref: nil, error: nil, position: nil) @status = status.to_s @commit_sha = commit_sha unless commit_sha.nil? @stash_ref = stash_ref unless stash_ref.nil? @error = error unless error.nil? @position = position unless position.nil? now = Time.now.utc @updated_at = now @started_at ||= now if @status == "running" @finished_at = now if %w[ready_for_review failed blocked cancelled archived].include?(@status) self end |