-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli_timeout_monitoring.php
More file actions
104 lines (84 loc) · 4.3 KB
/
Copy pathcli_timeout_monitoring.php
File metadata and controls
104 lines (84 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* This CLI script performs various monitoring tasks. It is started when the user starts a recording, and stopped when they stop recording.
* This script is called by qtbstart and qtbstop.
* Current checks performed:
* - timeout check (checks whether the user has forgotten to stop recording, and publish the recording if they did)
*/
/**
* Timeout check:
* For the first "threshold" seconds (typically 2 or 3 hours), we decide to trust the user.
* After that, we check that there has been activity at least once every "timeout" seconds (typically 15 min).
* This program is meant to be run as a crontask at least once every "timeout" seconds
*/
require_once __DIR__.'/global_config.inc';
require_once __DIR__.'/lib_model.php';
require_once __DIR__.'/lib_recording_session.php';
global $service;
$service = true;
Logger::$print_logs = true;
$logger->log(EventType::RECORDER_TIMEOUT_MONITORING, LogLevel::INFO, "Monitoring started", array(basename(__FILE__)));
RecordingSession::restore_session_if_any();
if(RecordingSession::is_locked() === false) {
echo "No current recording session, nothing to do";
die;
}
// Saves the time when the recording has been init
$init_time = RecordingSession::instance()->get_init_time();
// Delays, in seconds
$threshold_timeout = 7200; // Threshold before we start worrying about the user
$timeout = 900; // Timeout after which we consider a user has forgotten to stop their recording
$sleep_time = 60; // Duration of the sleep between two checks
set_time_limit(0);
$pid = getmypid();
fwrite(fopen($recorder_monitoring_pid, 'w'), $pid);
//get asset name ?
// This is the main loop. Runs until the lock file disappears
while (true) {
$now = time();
//Stop conditions:
// We stop if the pid file does not exist anymore ("kill -9" simulation)
// or the file contains an other pid
// or the recorder is not locked anymore
if (!file_exists($recorder_monitoring_pid)) {
$logger->log(EventType::RECORDER_TIMEOUT_MONITORING, LogLevel::INFO, "Monitoring stopped. Cause: Monitoring pid file not found", array(basename(__FILE__)));
die;
}
if ($pid != file_get_contents($recorder_monitoring_pid)) {
$logger->log(EventType::RECORDER_TIMEOUT_MONITORING, LogLevel::INFO, "Monitoring stopped. Cause: Could not read monitoring file", array(basename(__FILE__)));
die;
}
if(!RecordingSession::is_locked()) {
$logger->log(EventType::RECORDER_TIMEOUT_MONITORING, LogLevel::INFO, "Monitoring stopped. Cause: Recorder is not locked anymore", array(basename(__FILE__)));
die;
}
//should we start to worry about timeout ?
$diff_init = $now - $init_time;
if ($diff_init < $threshold_timeout) {
sleep($sleep_time);
continue;
}
// get last request
$lastmod_time = RecordingSession::instance()->get_last_request();
if($lastmod_time == false) {
//failed to get time
$logger->log(EventType::RECORDER_TIMEOUT_MONITORING, LogLevel::CRITICAL, "Monitoring stopped because we couldn't get the last request time", array(basename(__FILE__)));
//consider last request was around 3 hours after init
$lastmod_time = $init_time + 10800 - $timeout; //10800 = 3h
}
// if record was started at least $threshold_timeout seconds ago
// and if no request received in the last $timeout seconds
// force stop the recorder
$diff_lastmod = $now - $lastmod_time;
if ($diff_lastmod > $timeout) {
$date_format = "Y_M_D_H:s";
$logger->log(EventType::RECORDER_TIMEOUT_MONITORING, LogLevel::INFO, "Timeout triggered after $diff_lastmod seconds. Init: $init_time / Last request: $lastmod_time.", array(basename(__FILE__)));
mail($mailto_admins, 'Recording timed out', 'The recording in classroom ' . $classroom
. ' was stopped and published in private album because there has been no user activity since '
. ($diff_lastmod) . ' seconds ago. Time: ' . date($date_format,$now) . ' .Last request: ' . date($date_format, $lastmod_time)
. ' Start time: ' . date($date_format,$init_time) . '');
recording_force_quit();
}
//$logger->log(EventType::RECORDER_TIMEOUT_MONITORING, LogLevel::DEBUG, "diffmod: $diff_lastmod. diffinit: $diff_init", array(basename(__FILE__)));
sleep($sleep_time);
}