Class: ScoutAgent::Database::Statuses

Inherits:
ScoutAgent::Database show all
Defined in:
lib/scout_agent/database/statuses.rb

Overview

This small database keeps track of the current status of all running processes withing the agent. This allows tracking of process function (represented as the process name), ID, and current task.

Instance Attribute Summary

Attributes inherited from ScoutAgent::Database

#log

Instance Method Summary collapse

Methods inherited from ScoutAgent::Database

#initialize, load, #locked?, #maintain, #migrate, #path, path, #prepare_connection, #query, #read_from_sqlite, #read_locked?, #schema_version, #write_locked?, #write_to_sqlite

Constructor Details

This class inherits a constructor from ScoutAgent::Database

Instance Method Details

#clear_status(name = IDCard.me && IDCard.me.process_name) ⇒ Object

Removes the status record for name (pulled from IDCard::me()#process_name() by default). This is generally done by processes in an at_exit() block.



52
53
54
55
56
57
58
59
# File 'lib/scout_agent/database/statuses.rb', line 52

def clear_status(name = IDCard.me && IDCard.me.process_name)
  write_to_sqlite do |sqlite|
    sqlite.execute("DELETE FROM statuses WHERE name = ?", name)
  end
rescue Amalgalite::SQLite3::Error => error  # failed to delete status
  # do nothing:  new process will replace
  log.error("Database status clearing error:  #{error.message}.")
end

#current_status(name = IDCard.me && IDCard.me.process_name) ⇒ Object

Returns the current status message for name (pulled from IDCard::me()#process_name() by default).



79
80
81
82
83
84
85
86
87
88
# File 'lib/scout_agent/database/statuses.rb', line 79

def current_status(name = IDCard.me && IDCard.me.process_name)
  read_from_sqlite { |sqlite|
    sqlite.first_value_from("    SELECT status FROM statuses WHERE name = ?\n    END_FIND_STATUS\n  }\nrescue Amalgalite::SQLite3::Error => error  # failed to find status\n  log.error(\"Database current status error:  \#{error.message}.\")\n  nil  # return no results\nend\n", name)

#current_statusesObject

Returns the current statuses (name, pid, status, and last_updated_at) for all known processes. An empty Array is returned if no processes are currently active.



66
67
68
69
70
71
72
73
# File 'lib/scout_agent/database/statuses.rb', line 66

def current_statuses
  query("  SELECT name, pid, status, last_updated_at FROM statuses ORDER BY ROWID\n  END_FIND_STATUSES\nrescue Amalgalite::SQLite3::Error => error  # failed to find statuses\n  log.error(\"Database statuses error:  \#{error.message}.\")\n  Array.new  # return empty results\nend\n".trim)

#update_schema(version = schema_version) ⇒ Object

Builds a schema for the process statuses table.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scout_agent/database/statuses.rb', line 13

def update_schema(version = schema_version)
  case version
  when 0
    "    CREATE TABLE statuses (\n      name            TEXT NOT NULL PRIMARY KEY\n        CHECK( name IN ( 'lifeline',      'master', 'mission',\n                         'communication', 'queue',  'snapshot' ) ),\n      pid             INTEGER NOT NULL,\n      status          REQUIRED_TEXT_TYPE,\n      last_updated_at DATETIME_TYPE\n    );\n    DEFAULT_LOCALTIME_TRIGGER statuses last_updated_at\n    END_INITIAL_SCHEMA\n  end\nend\n".trim

#update_status(status, name = IDCard.me && IDCard.me.process_name) ⇒ Object

Record the current status for the calling process. The process ID is determined with Process::pid() and name is pulled from IDCard::me()#process_name() when available.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scout_agent/database/statuses.rb', line 35

def update_status(status, name = IDCard.me && IDCard.me.process_name)
  write_to_sqlite do |sqlite|
    sqlite.execute("    INSERT OR REPLACE INTO statuses(name, pid, status, last_updated_at)\n                           VALUES(     ?,   ?,      ?,            null)\n    END_UPDATE_STATUS\n  end\nrescue Amalgalite::SQLite3::Error => error  # failed to update status\n  # do nothing:  try again later\n  log.error(\"Database status update error:  \#{error.message}.\")\nend\n".trim, name, Process.pid, status)