Files
namaste/scripts/expireOneSession.php
gramps 373ebc8c93 Archive: Namaste PHP AMQP framework v1.0 (2017-2020)
952 days continuous production uptime, 40k+ tp/s single node.
Original corpo Bitbucket history not included — clean archive commit.
2026-04-05 09:49:30 -07:00

160 lines
6.0 KiB
PHP

<?php
/**
* expireOneSession.php
*
* this script is invoked by atd (8) and it's sole purpose is to take the GUID passed-in the program as a cli param,
* fetch the session record indicated by the GUID, expire the session, then update the corresponding system event
* record to evolve the status of that record to expired.
*
* Previous versions of this program had extensive error checking and some unnecessary instantiations - all this code
* really needs to do is take the session GUID passed-in and build an update query for the tercero::sessions table that
* will expire the session record and pass that to the tercero broker before locally updating the system event record.
*
* If any processing prevents the script from issuing and successfully processing the expire command, then we'll create
* and entry in the failed-sessions table so that we can make a second attempt, later, to clean-up the "orphaned"
* sessions.
*
* This script can only run on the Admin service.
*
* NOTE:
* -----
* This is an example of the CLI directive registered with AT(1):
*
* /usr/bin/php -f /home/mshallop/code/php/namaste/scripts/expireOneSession.php DD0ABEE5-77F3-74F1-DF0E-581D72C3A8C9
*
* The token value parameter corresponds to gaSystemEvents_sev.idses_sev and to gaSessions_ses.token_ses -- meaning
* there's no longer any need to go through the system event to fetch the token.
*
* @author mike@givingassistant.org
* @version 1.0
*
*
* HISTORY:
* ========
* 09-30-20 mks DB-168: original coding
*
*/
@require_once(dirname(__DIR__) . '/config/sneakerstrap.inc');
$res = '_X1S: '; // expire-one-session-script
$errors = [];
$sessionGUID = null;
$sessionData = null;
$sysEvData = null;
/** @var gacMongoDB $feWidget */
$feWidget = null;
$file = basename(__FILE__);
$ext = COLLECTION_MONGO_SESS_EXT;
$logger = new gacErrorLogger();
if (!gasConfig::$settings[ENV_ADMIN][CONFIG_IS_LOCAL]) {
exit(ERROR_REMOTE_NOT_ADMIN);
}
function logout(string $_msg):void
{
global $logger, $res;
consoleLog($res, CON_SYSTEM, $_msg);
$logger->error($_msg);
exit($_msg);
}
// set-up the meta data arrays
$metaFailedSessions = [
META_SESSION_MISC => $file,
META_SESSION_IP => STRING_SESSION_HOME,
META_CLIENT => CLIENT_SYSTEM,
META_SESSION_DAEMON => 1,
META_TEMPLATE => TEMPLATE_CLASS_FAILED_SESSIONS
];
$metaSession = [
META_SESSION_MISC => $file,
META_SESSION_IP => STRING_SESSION_HOME,
META_CLIENT => CLIENT_SYSTEM,
META_SESSION_DAEMON => 1,
META_TEMPLATE => TEMPLATE_CLASS_SESSIONS
];
$metaSysEv = [
META_SESSION_MISC => $file,
META_SESSION_IP => STRING_SESSION_HOME,
META_CLIENT => CLIENT_SYSTEM,
META_TEMPLATE => TEMPLATE_CLASS_SYS_EVENTS
];
// validate the session record GUID passed as a CLI parameter to the program
if (!empty($argv) and !empty($argv[1])) {
$sessionGUID = $argv[1];
if (!validateGUID($sessionGUID)) {
// if the eventGUID fails validation, then create a systemEvent and exit
consoleLog($res, CON_ERROR, ERROR_INVALID_GUID . $sessionGUID);
$eventData = [
MONGO_FAILED_EVENT_GUID => $sessionGUID,
DB_CREATED => new MongoDate(),
MONGO_FAILED_EVENT_NAME => MONGO_FAILED_EVENT_BAD_GUID,
MONGO_FAILED_EVENT_DESC => sprintf(MONGO_FAILED_EVENT_BAD_GUID_DESC, $sessionGUID),
MONGO_FAILED_EVENT_SEV => MONGO_FAILED_EVENT_SEV_LOW
];
/** @var gacMongoDB $feWidget */
if (!is_null($feWidget = grabWidget($meta, '', $errors))) {
$feWidget->_createRecord([$eventData]);
if (!$feWidget->status) {
// saving the failed-session record failed
$hdr = sprintf(INFO_LOC, $file, __LINE__);
$msg = sprintf(ERROR_MDB_QUERY_FAIL, DB_EVENT_CREATE);
logout($hdr . $msg);
}
} else {
// todo -- post a new system event for the inability to create a failed session record
$data = [
SYSTEM_EVENT_NAME => SYSEV_NAME_FAIL_TO_INIT . TEMPLATE_CLASS_SESSIONS,
SYSTEM_EVENT_TYPE => SYSEV_TYPE_CLASS,
SYSTEM_EVENT_CODE_LOC => $file . AT . __LINE__,
SYSTEM_EVENT_META_DATA => $metaSysEv,
];
@postSystemEvent($data, $sessionGUID, $logger);
logout(ERROR_FAILED_TO_INSTANTIATE . TEMPLATE_CLASS_SESSIONS);
}
}
} else {
$hdr = sprintf(INFO_LOC, $file, __LINE__);
logout($hdr . ERROR_DATA_ARRAY_ARGV_EMPTY);
}
// next step: build the data payload for the tercero broker(sBroker) event to expire a session
$request = [
BROKER_REQUEST => BROKER_REQUEST_EXPIRE_SESSION,
BROKER_DATA => [
STRING_GUID_KEY => $sessionGUID,
STRING_TOK_TYPE => STRING_TOK_TYPE_SES,
SESSION_CUSTOM_FIELD => INFO_CLOSED_BY,
SESSION_CUSTOM_VALUE => INFO_CLOSED_BY_EOS
],
BROKER_META_DATA => [
META_TEMPLATE => TEMPLATE_CLASS_SESSIONS,
META_CLIENT => CLIENT_SYSTEM,
META_SESSION_DAEMON => 1,
META_SESSION_MISC => basename(__FILE__)
]
];
// instantiate a tercero broker client
$bc = new gacWorkQueueClient($file . AT . __LINE__, BROKER_QUEUE_S);
if (!$bc->status) {
// create a new failed-session event and exit
$data = [
SYSTEM_EVENT_NAME => ERROR_BROKER_CLIENT_DECLARE . BROKER_QUEUE_S,
SYSTEM_EVENT_TYPE => SYSEV_TYPE_CLASS,
SYSTEM_EVENT_CODE_LOC => $file . AT . __LINE__,
SYSTEM_EVENT_META_DATA => $metaSysEv,
];
/** @var gacMongoDB $feWidget */
if (is_null($feWidget = grabWidget($metaFailedSessions, '', $errors))) {
logout(ERROR_FAILED_TO_INSTANTIATE . TEMPLATE_CLASS_FAILED_SESSIONS);
}
$feWidget->_createRecord([$data]);
$hdr = sprintf(INFO_LOC, $file, __LINE__);
logout($hdr. ERROR_BROKER_CLIENT_DECLARE . BROKER_QUEUE_S);
}
// publish the tercero request
$bc->call(gzcompress(json_encode($request)));
// tercero will publish an update event for the original system event record if the session record was successfully saved