Skip to content
Kward Search API index

Class: Kward::Workers::Job

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_shaObject (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_atObject (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

#errorObject (readonly)

Returns the value of attribute error.



27
28
29
# File 'lib/kward/workers/job.rb', line 27

def error
  @error
end

#finished_atObject (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

#idObject (readonly)

Returns the value of attribute id.



27
28
29
# File 'lib/kward/workers/job.rb', line 27

def id
  @id
end

#positionObject (readonly)

Returns the value of attribute position.



27
28
29
# File 'lib/kward/workers/job.rb', line 27

def position
  @position
end

#session_pathObject (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_atObject (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_refObject (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

#titleObject (readonly)

Returns the value of attribute title.



27
28
29
# File 'lib/kward/workers/job.rb', line 27

def title
  @title
end

#updated_atObject (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_rootObject (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

#statusObject



29
30
31
# File 'lib/kward/workers/job.rb', line 29

def status
  @status
end

#to_hObject



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" => timestamp(enqueued_at),
    "started_at" => timestamp(started_at),
    "finished_at" => timestamp(finished_at),
    "updated_at" => timestamp(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