Module: Cybertrain::Scheduler::Timetable

Defined in:
cybertrain/scheduler/timetable.rb

Overview

Cybertrain::Scheduler::Timetable – the scheduler’s persisted timetable: one row per task name with when it last ran and when it runs next, so that a restart or a change of leader continues the timetable instead of starting it again.

The rows live in cybertrain_schedules, which the framework creates itself (CREATE TABLE IF NOT EXISTS, on first use) and leaves out of db/schema.rb, like cybertrain_jobs and cybertrain_leases. Times are epoch seconds; last_run_at is NULL until the task first runs.

Class Method Summary collapse

Class Method Details

.install! ⇒ nil

Creates the table if it is missing. The other methods call it once per connected database.

Returns:

  • (nil)


47
48
49
50
51
52
53
54
# File 'cybertrain/scheduler/timetable.rb', line 47

def self.install!
  DB.with { |c| c.exec_script(TABLE_SQL) }
  # A CREATE inside a transaction that rolls back is undone, so only
  # one outside a transaction is remembered (as Job.install! does).
  pool = DB.pool
  @installed_generation = DB.generation if !pool.nil? && pool.held_index < 0
  nil
end

.prune(names) ⇒ Integer

Deletes the rows whose name is not in names.

Parameters:

  • names (Array<String>) —

    the names to keep

Returns:

  • (Integer) —

    how many rows were deleted

Raises:

  • (Cybertrain::DB::Error) —

    when the database is not reachable



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'cybertrain/scheduler/timetable.rb', line 119

def self.prune(names)
  ensure_installed
  sql = +"DELETE FROM cybertrain_schedules"
  unless names.empty?
    sql << " WHERE name NOT IN ("
    i = 0
    while i < names.size
      sql << ", " if i > 0
      sql << "?"
      i += 1
    end
    sql << ")"
  end
  binds = Array.new(0) { "" }
  names.each { |n| binds << n }
  count = 0
  DB.with do |c|
    c.execute(sql, binds)
    count = c.changes
  end
  count
end

.read_all ⇒ Hash{String => Array<Integer>}

Every row, by task name: [last_run_at, next_run_at], with 0 for a task that has not run yet.

Returns:

Raises:

  • (Cybertrain::DB::Error) —

    when the database is not reachable



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'cybertrain/scheduler/timetable.rb', line 67

def self.read_all
  ensure_installed
  out = { "" => [0, 0] }
  out.delete("")
  rows = DB.with { |c| c.execute("SELECT name, last_run_at, next_run_at FROM cybertrain_schedules ORDER BY name") }
  rows.each do |row|
    pair = Array.new(0) { 0 }
    pair << Cast.int(row["last_run_at"])
    pair << Cast.int(row["next_run_at"])
    out[Cast.str(row["name"])] = pair
  end
  out
end

.write(name, last_run_at, next_run_at, now) ⇒ nil

Records a task’s last and next run (inserting its row or replacing it). A row is never moved backwards: a write whose last run is earlier than the row’s, or the same with an earlier next run, is ignored.

Parameters:

  • name (String) —

    the task’s name

  • last_run_at (Integer) —

    epoch seconds; 0 when it has not run

  • next_run_at (Integer) —

    epoch seconds

  • now (Integer) —

    epoch seconds, stored as updated_at

Returns:

  • (nil)

Raises:

  • (Cybertrain::DB::Error) —

    when the database is not reachable



92
93
94
95
96
97
# File 'cybertrain/scheduler/timetable.rb', line 92

def self.write(name, last_run_at, next_run_at, now)
  ensure_installed
  binds = Cast.binds(name, last_run_at, next_run_at, now)
  DB.with { |c| c.execute(WRITE_SQL, binds) }
  nil
end

.write_missing(name, next_run_at, now) ⇒ nil

Inserts a row for a task that has none (never run, next run next_run_at); an existing row is left as it is.

Parameters:

  • name (String) —

    the task’s name

  • next_run_at (Integer) —

    epoch seconds

  • now (Integer) —

    epoch seconds, stored as updated_at

Returns:

  • (nil)

Raises:

  • (Cybertrain::DB::Error) —

    when the database is not reachable



107
108
109
110
111
112
# File 'cybertrain/scheduler/timetable.rb', line 107

def self.write_missing(name, next_run_at, now)
  ensure_installed
  binds = Cast.binds(name, next_run_at, now)
  DB.with { |c| c.execute(MISSING_SQL, binds) }
  nil
end