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(<<-END_FIND_STATUS, name)
    SELECT status FROM statuses WHERE name = ?
    END_FIND_STATUS
  }
rescue Amalgalite::SQLite3::Error => error  # failed to find status
  log.error("Database current status error:  #{error.message}.")
  nil  # return no results
end

#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(<<-END_FIND_STATUSES.trim)
  SELECT name, pid, status, last_updated_at FROM statuses ORDER BY ROWID
  END_FIND_STATUSES
rescue Amalgalite::SQLite3::Error => error  # failed to find statuses
  log.error("Database statuses error:  #{error.message}.")
  Array.new  # return empty results
end

#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
    <<-END_INITIAL_SCHEMA.trim
    CREATE TABLE statuses (
      name            TEXT NOT NULL PRIMARY KEY
        CHECK( name IN ( 'lifeline',      'master', 'mission',
                         'communication', 'queue',  'snapshot' ) ),
      pid             INTEGER NOT NULL,
      status          REQUIRED_TEXT_TYPE,
      last_updated_at DATETIME_TYPE
    );
    DEFAULT_LOCALTIME_TRIGGER statuses last_updated_at
    END_INITIAL_SCHEMA
  end
end

#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(<<-END_UPDATE_STATUS.trim, name, Process.pid, status)
    INSERT OR REPLACE INTO statuses(name, pid, status, last_updated_at)
                           VALUES(     ?,   ?,      ?,            null)
    END_UPDATE_STATUS
  end
rescue Amalgalite::SQLite3::Error => error  # failed to update status
  # do nothing:  try again later
  log.error("Database status update error:  #{error.message}.")
end