Skip to content

Commit

Permalink
Merge branch 'pu/ccheng/tasl_log_from_file' into '2023.11'
Browse files Browse the repository at this point in the history
fix(Tinebase/Schedular/Task): get task logs from temp file

See merge request tine20/tine20!5591
  • Loading branch information
pschuele committed Jul 4, 2024
2 parents 1875397 + 378072a commit 64073dd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
23 changes: 23 additions & 0 deletions tine20/Tinebase/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ public function addWriterByConfig($loggerConfig)
$this->addWriter($writer);
}


/**
* Remove a writer.
*
* @param Zend_Log_Writer_Abstract $writer
* @return Zend_Log
* @throws Zend_Log_Exception
*/
public function removeWriter($writer)
{
if ($writer instanceof Zend_Log_Writer_Abstract) {
foreach ($this->_writers as $key => $existingWriter) {
if ($existingWriter === $writer) {
unset($this->_writers[$key]);
return $this;
}
}
throw new Zend_Log_Exception('Writer not found in the list of writers.');
} else {
throw new Zend_Log_Exception('Writer must be an instance of Zend_Log_Writer_Abstract.');
}
}

/**
* Chose writer from logger config stream or db
*
Expand Down
43 changes: 18 additions & 25 deletions tine20/Tinebase/Scheduler/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,35 +218,28 @@ public function run()
continue;
}

$classmethod = [$class, $callable[self::METHOD_NAME]];
if (! is_callable($classmethod)) {
$classMethod = [$class, $callable[self::METHOD_NAME]];
if (! is_callable($classMethod)) {
Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__
. ' Could not get callable for scheduler job');
$aggResult = false;
} else {
// TODO activate notifications again
// current problems:
// - logs some strange stuff in docker setup
// - writer is not removed after execution, so each following task adds another writer...
// solution:
// - use a temp file for the writer
// - send the file contents as notification
// - remove/stop the writer after execution

// catch output buffer
// ob_start();
// $writer = new Zend_Log_Writer_Stream('php://output');
// // TODO get priority from scheduler config?
// $priority = 6;
// $writer->addFilter(new Zend_Log_Filter_Priority($priority));
// Tinebase_Core::getLogger()->addWriter($writer);

$result = call_user_func_array($classmethod, $callable[self::ARGS] ?? []);

// send notification with $notificationBody to configured email
// $notificationBody = ob_get_clean();
// $this->_sendNotification($callable, $notificationBody);

// use a temp file for the writer
$tmpPath = tempnam(Tinebase_Core::getTempDir(), 'schedular_task');
$writer = new Zend_Log_Writer_Stream($tmpPath);
$priority = $this->_config['loglevel'] ?? 6;
$writer->addFilter(new Zend_Log_Filter_Priority($priority));
Tinebase_Core::getLogger()->addWriter($writer);

try {
$result = call_user_func_array($classMethod, $callable[self::ARGS] ?? []);
$notificationBody = file_get_contents($tmpPath);
// send the file contents as notification to configured email
$this->_sendNotification($callable, $notificationBody);
} finally {
Tinebase_Core::getLogger()->removeWriter($writer);
unlink($tmpPath);
}
$aggResult = $aggResult && $result;
}
}
Expand Down

0 comments on commit 64073dd

Please sign in to comment.