Module: Cybertrain::WebSocket::FeedTable

Defined in:
cybertrain/websocket/feed_table.rb

Overview

Cybertrain::WebSocket::FeedTable – the feeds’ status, published through the database so that a process that serves no HTTP (the feeds role) can still be watched: GET /up in a server process and ./<app> feeds status read it.

One row per subscription in cybertrain_feeds, which the framework creates itself (CREATE TABLE IF NOT EXISTS, on first use) and leaves out of db/schema.rb, like cybertrain_schedules. The supervisor of every process that runs feeds writes them (publish_status) every Config#feed_status_interval seconds, through the StatusWriter this file registers. Times are epoch seconds.

Class Method Summary collapse

Class Method Details

.delete_process_feeds(process) ⇒ Integer

Deletes the rows process published: a feeds process does it after a clean stop, so /up no longer counts feeds nobody runs.

Parameters:

  • process (String) —

    “:"

Returns:

  • (Integer) —

    how many rows were deleted

Raises:

  • (Cybertrain::DB::Error) —

    when the database is not reachable



113
114
115
116
117
118
119
120
121
# File 'cybertrain/websocket/feed_table.rb', line 113

def self.delete_process_feeds(process)
  ensure_installed
  removed = 0
  DB.with do |c|
    c.execute("DELETE FROM cybertrain_feeds WHERE process = ?", [process])
    removed = c.changes
  end
  removed
end

.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/websocket/feed_table.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

.read_feeds ⇒ Array<Hash{String => Object}>

Every row, ordered by name, as the Hashes Connection#execute gives, keyed by column name:

  • "name" [String] the subscription’s name
  • "process" [String] who published it, "<hostname>:<pid>"
  • "connected" [Integer] 1 when the feed was connected, else 0
  • "started_at" [Integer] when that process started the feed
  • "last_activity_at" [Integer, nil] the last message or pong answering the feed’s ping; nil until the first
  • "restarts" [Integer] how many times the supervisor started the feed’s thread again after it died
  • "updated_at" [Integer] when the row was written

A feed is silent when COALESCE(last_activity_at, started_at) is old, and its publisher stale when updated_at is.

Returns:

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

Raises:

  • (Cybertrain::DB::Error) —

    when the database is not reachable



80
81
82
83
84
85
86
# File 'cybertrain/websocket/feed_table.rb', line 80

def self.read_feeds
  ensure_installed
  DB.with do |c|
    c.execute("SELECT name, process, connected, started_at, last_activity_at, restarts, updated_at " \
              "FROM cybertrain_feeds ORDER BY name")
  end
end

.upsert_feed(name, process, connected, started_at, last_activity_at, restarts, now) ⇒ nil

Writes a feed’s row (inserting it or replacing it).

Parameters:

  • name (String) —

    the subscription’s name

  • process (String) —

    the publishing process, “:"

  • connected (Boolean)
  • started_at (Integer) —

    epoch seconds

  • last_activity_at (Integer) —

    epoch seconds; 0 for none yet (stored as NULL)

  • restarts (Integer)
  • now (Integer) —

    epoch seconds, stored as updated_at

Returns:

  • (nil)

Raises:

  • (Cybertrain::DB::Error) —

    when the database is not reachable



100
101
102
103
104
105
# File 'cybertrain/websocket/feed_table.rb', line 100

def self.upsert_feed(name, process, connected, started_at, last_activity_at, restarts, now)
  ensure_installed
  binds = Cast.binds(name, process, connected ? 1 : 0, started_at, last_activity_at, restarts, now)
  DB.with { |c| c.execute(WRITE_SQL, binds) }
  nil
end