Module: Cybertrain::JobsPage

Defined in:
cybertrain/jobs_page.rb

Overview

What the jobs page shows and does. The HTML is built with the tag builder (Html.tag), which escapes every value; there is no template file and nothing in the application’s app/views. It renders inside the application’s layout, and its buttons are button_to forms with the CSRF token.

Every row action is WHERE id = ? AND state IN ('failed', 'discarded'), so a row that is queued or that a worker has claimed is left alone, and every value is a bind.

Class Method Summary collapse

Class Method Details

.delete_job(id) ⇒ Boolean

Deletes a failed or discarded job.

Parameters:

Returns:

  • (Boolean) —

    false when there is no such row or it is not failed or discarded



154
155
156
157
158
159
160
161
162
# File 'cybertrain/jobs_page.rb', line 154

def self.delete_job(id)
  Job.ensure_installed
  changed = 0
  DB.with do |c|
    c.execute("DELETE FROM cybertrain_jobs WHERE id = ? AND state IN ('failed', 'discarded')", [id])
    changed = c.changes
  end
  changed > 0
end

.discard_all_failed ⇒ Integer

Marks every failed row discarded.

Returns:



184
185
186
187
188
189
190
191
192
# File 'cybertrain/jobs_page.rb', line 184

def self.discard_all_failed
  Job.ensure_installed
  count = 0
  DB.with do |c|
    c.execute("UPDATE cybertrain_jobs SET state = 'discarded', updated_at = ? WHERE state = 'failed'", [Clock.now.to_i])
    count = c.changes
  end
  count
end

.discard_job(id) ⇒ Boolean

Marks a failed job discarded (a discarded one stays so).

Parameters:

Returns:

  • (Boolean) —

    false when there is no such row or it is not failed or discarded



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'cybertrain/jobs_page.rb', line 135

def self.discard_job(id)
  Job.ensure_installed
  changed = 0
  DB.with do |c|
    # A row already discarded keeps its updated_at, so discarding it
    # again does not put off its pruning.
    c.execute("UPDATE cybertrain_jobs SET state = 'discarded', " \
              "updated_at = CASE WHEN state = 'discarded' THEN updated_at ELSE ? END " \
              "WHERE id = ? AND state IN ('failed', 'discarded')", Cast.binds(Clock.now.to_i, id))
    changed = c.changes
  end
  changed > 0
end

.index_html(page, per_page, path, helpers) ⇒ String

The page’s HTML, its texts the current locale’s cybertrain.jobs_page.* (Jobs, Failed (%{count}), …): the count per state, the queued count and when the next queued job is due, then the failed, discarded and running lists, per_page rows each (newest first), and Newer / Older links when a list has more. Failed and discarded rows (kind, arguments, error, attempts, run at, updated) carry retry / discard / delete buttons; running rows show who claimed them and how long ago their process last said so.

Parameters:

  • page (Integer) —

    from 1

  • per_page (Integer) —

    at least 1

  • path (String) —

    the page’s path; the buttons post below it

  • helpers (Template::Helpers) —

    the request’s view helpers, whose button_to adds the CSRF token

Returns:

  • (String) —

    HTML



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'cybertrain/jobs_page.rb', line 67

def self.index_html(page, per_page, path, helpers)
  per = per_page < 1 ? 1 : per_page
  number = page < 1 ? 1 : page
  now = Clock.now.to_i
  counts = Array.new(0) { 0 }
  STATES.each { |state| counts << Job.state_count(state) }
  parts = Array.new(0) { SafeString.of("") }
  parts << Html.tag("h1", {}, jobs_text("title"))
  parts << counts_table(counts)
  parts << queued_line(counts[0])
  parts << Html.tag("h2", {}, jobs_counted("tabs.failed", counts[3]))
  parts << bulk_buttons(path, helpers) if counts[3] > 0
  parts << finished_list("failed", number, per, path, helpers)
  parts << Html.tag("h2", {}, jobs_counted("tabs.discarded", counts[4]))
  parts << finished_list("discarded", number, per, path, helpers)
  parts << Html.tag("h2", {}, jobs_counted("tabs.running", counts[1]))
  parts << running_list(number, per, now)
  most = counts[1]
  most = counts[3] if counts[3] > most
  most = counts[4] if counts[4] > most
  parts << pagination(number, per, most, path)
  Html.tag("section", { "class" => "cybertrain-jobs" }, Html.safe_join(parts, "\n")).to_s
end

.jobs_rows(state, page, per_page) ⇒ Array<Hash{String => Object}>

One page of the rows in state, newest first, each a Hash of the table’s columns ("id", "kind", "payload", "state", "run_at", "attempts", "last_error", "claimed_by", "heartbeat_at", "created_at", "updated_at"; times are epoch seconds).

Parameters:

Returns:

  • (Array<Hash{String => Object}>)


101
102
103
104
105
106
107
108
109
# File 'cybertrain/jobs_page.rb', line 101

def self.jobs_rows(state, page, per_page)
  Job.ensure_installed
  per = per_page < 1 ? 1 : per_page
  offset = page < 1 ? 0 : (page - 1) * per
  DB.with do |c|
    c.execute("SELECT #{COLUMNS} FROM cybertrain_jobs WHERE state = ? ORDER BY id DESC LIMIT ? OFFSET ?",
              Cast.binds(state, per, offset))
  end
end

.page_html(controller, path) ⇒ SafeString

The page in the application’s layout, for CybertrainJobsController#index: index_html for ?page=N and Config#jobs_page_per_page, inside Views.layout_name when that layout exists (the HTML alone otherwise).

Parameters:

  • controller (Controller) —

    the controller rendering it

  • path (String) —

    the page’s path ("/admin/jobs")

Returns:



39
40
41
42
43
44
45
46
47
48
49
# File 'cybertrain/jobs_page.rb', line 39

def self.page_html(controller, path)
  helpers = Views.helpers_builder.call(controller)
  body = index_html(controller.params.page_number, Cybertrain.config.jobs_page_per_page, path, helpers)
  engine = Views.engine
  frame = Views.layout_name
  return SafeString.of(body) if engine.nil? || frame.empty? || !engine.exists?(frame)

  env = controller.view_env({})
  env["__content"] = SafeString.of(body)
  SafeString.of(engine.render(frame, env, helpers))
end

.retry_all ⇒ Integer

retry_job for every failed row, one UPDATE per row (each writes that row’s concurrency key), in one transaction.

Returns:

  • (Integer) —

    how many were queued again



168
169
170
171
172
173
174
175
176
177
178
179
# File 'cybertrain/jobs_page.rb', line 168

def self.retry_all
  Job.ensure_installed
  now = Clock.now.to_i
  count = 0
  DB.transaction(immediate: true) do |c|
    rows = c.execute("SELECT id, kind, payload FROM cybertrain_jobs WHERE state = 'failed' ORDER BY id")
    rows.each do |row|
      count += requeue(c, Cast.int(row["id"]), Cast.str(row["kind"]), Cast.str(row["payload"]), now)
    end
  end
  count
end

.retry_job(id) ⇒ Boolean

Queues a failed or discarded job again: state = 'queued', run_at now, attempts = 0 (the retry policy starts afresh), last_error = '', and the Cybertrain::Job.concurrency_key_for of its kind and payload written in the same UPDATE (a row failed before its kind had a key gets one).

Parameters:

Returns:

  • (Boolean) —

    false when there is no such row or it is not failed or discarded



120
121
122
123
124
125
126
127
128
# File 'cybertrain/jobs_page.rb', line 120

def self.retry_job(id)
  Job.ensure_installed
  changed = 0
  DB.with do |c|
    rows = c.execute("SELECT kind, payload FROM cybertrain_jobs WHERE id = ? AND state IN ('failed', 'discarded')", [id])
    changed = requeue(c, id, Cast.str(rows[0]["kind"]), Cast.str(rows[0]["payload"]), Clock.now.to_i) unless rows.empty?
  end
  changed > 0
end