Skip to content

Commit

Permalink
WR #452436: Prevent excimer processors from doubling up partial saves.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Den Dulk committed Feb 5, 2025
1 parent 7ac5964 commit aeb2a7b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
31 changes: 28 additions & 3 deletions classes/cron_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class cron_processor implements processor {
/** @var int */
protected $samplems;

/** @var bool */
protected static $alreadyprofiling = false;

/** @var bool */
protected $hasoverlapped = false;

/**
* Initialises the processor
*
Expand All @@ -51,7 +57,10 @@ public function init(manager $manager) {
$this->sampletime = $manager->get_starttime();

$manager->get_timer()->setCallback(function () use ($manager) {
$this->on_interval($manager);
if (!$this->hasoverlapped) {
// Once overlapping has happened once, we prevent all future partial saving.
$this->on_interval($manager);
}
});

// Preload config values to avoid DB access during processing. See manager::get_altconnection() for more information.
Expand All @@ -61,7 +70,7 @@ public function init(manager $manager) {
function () use ($manager) {
$manager->get_timer()->stop();
$manager->get_profiler()->stop();
$this->on_interval($manager);
$this->on_interval($manager, true);
if ($this->tasksampleset) {
$this->process($manager, microtime(true));
}
Expand Down Expand Up @@ -91,8 +100,22 @@ public function get_min_duration(): float {
* We then check for a new task with the current sample.
*
* @param manager $manager
* @param bool $isfinal Set to true when this is called during shutdown.
*/
public function on_interval(manager $manager) {
public function on_interval(manager $manager, bool $isfinal = false) {
// We want to prevent doubling up of processing, so skip if an existing process is still executing.
// The profile logs will be kept and processed the next time.
if (self::$alreadyprofiling) {
$this->hasoverlapped = true;
debugging('tool_excimer: starting cron_processor::on_interval when previous call has not yet finished');
if ($isfinal) {
// This should never happen.
debugging('tool_excimer: alreadyprofiling is true during final on_interval.');
}
return;
}
self::$alreadyprofiling = true;

$profiler = $manager->get_profiler();
$log = $profiler->flush();
$memoryusage = memory_get_usage(); // Record and set initial memory usage at this point.
Expand Down Expand Up @@ -135,6 +158,8 @@ public function on_interval(manager $manager) {
// So it needs to be saved each loop.
$this->sampletime = $sampletime;
}

self::$alreadyprofiling = false;
}

/**
Expand Down
24 changes: 23 additions & 1 deletion classes/web_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class web_processor implements processor {
protected $samplems;
/** @var bool */
protected $partialsave;
/** @var bool */
protected static $alreadyprofiling = false;
/** @var bool */
protected $hasoverlapped = false;

/**
* Construct the web processor.
Expand Down Expand Up @@ -78,7 +82,10 @@ public function init(manager $manager) {

if ($this->partialsave) {
$manager->get_timer()->setCallback(function () use ($manager) {
$this->process($manager, false);
// Once overlapping has happened once, we prevent all future partial saving.
if (!$this->hasoverlapped) {
$this->process($manager, false);
}
});
}

Expand Down Expand Up @@ -111,6 +118,19 @@ public function get_min_duration(): float {
* @throws \dml_exception
*/
public function process(manager $manager, bool $isfinal) {
// We want to prevent overlapping of processing, so skip if an existing process is still executing.
// The profile logs will be kept and processed the next time.
if (self::$alreadyprofiling) {
$this->hasoverlapped = true;
debugging('tool_excimer: starting web_processor::process when previous process has not yet finished');
if ($isfinal) {
// This should never happen.
debugging('tool_excimer: alreadyprofiling is true during final process.');
}
return;
}
self::$alreadyprofiling = true;

$log = $manager->get_profiler()->flush();
$this->sampleset->add_many_samples($log);

Expand All @@ -134,5 +154,7 @@ public function process(manager $manager, bool $isfinal) {
}
$this->profile->save_record();
}

self::$alreadyprofiling = false;
}
}

0 comments on commit aeb2a7b

Please sign in to comment.