Skip to content

Commit

Permalink
Merge pull request #4446 from corentin-soriano/improve_cron_detection
Browse files Browse the repository at this point in the history
Improve teampass cron detection.
  • Loading branch information
nilsteampassnet authored Nov 6, 2024
2 parents 0a5efec + b70dbfe commit e3318a4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
17 changes: 11 additions & 6 deletions pages/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
* @license GPL-3.0
* @see https://www.teampass.net
*/
use TiBeN\CrontabManager\CrontabJob;
use TiBeN\CrontabManager\CrontabAdapter;
use TiBeN\CrontabManager\CrontabRepository;
use TeampassClasses\SessionManager\SessionManager;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use TeampassClasses\Language\Language;
Expand Down Expand Up @@ -199,9 +196,17 @@

// Instantiate the adapter and repository
try {
$crontabRepository = new CrontabRepository(new CrontabAdapter());
$results = $crontabRepository->findJobByRegex('/Teampass\ scheduler/');
if (count($results) === 0) {
// Get last cron execution timestamp
$result = DB::query(
'SELECT valeur
FROM ' . prefixTable('misc') . '
WHERE type = %s AND intitule = %s and valeur >= %d',
'admin',
'last_cron_exec',
time() - 600 // max 10 minutes
);

if (DB::count() === 0) {
?>
<div class="callout callout-info alert-dismissible mt-3" role="alert">
<h5><i class="fa-solid fa-info mr-2"></i><?php echo $lang->get('information'); ?></h5>
Expand Down
17 changes: 11 additions & 6 deletions pages/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
* @license GPL-3.0
* @see https://www.teampass.net
*/
use TiBeN\CrontabManager\CrontabJob;
use TiBeN\CrontabManager\CrontabAdapter;
use TiBeN\CrontabManager\CrontabRepository;
use TeampassClasses\SessionManager\SessionManager;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use TeampassClasses\Language\Language;
Expand Down Expand Up @@ -126,9 +123,17 @@

// Instantiate the adapter and repository
try {
$crontabRepository = new CrontabRepository(new CrontabAdapter());
$results = $crontabRepository->findJobByRegex('/Teampass\ scheduler/');
if (count($results) === 0) {
// Get last cron execution timestamp
$result = DB::query(
'SELECT valeur
FROM ' . prefixTable('misc') . '
WHERE type = %s AND intitule = %s and valeur >= %d',
'admin',
'last_cron_exec',
time() - 600 // max 10 minutes
);

if (DB::count() === 0) {
?>
<div class="callout callout-info alert-dismissible mt-3">
<h5><i class="fa-solid fa-info mr-2"></i><?php echo $lang->get('information'); ?></h5>
Expand Down
17 changes: 17 additions & 0 deletions sources/scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();

// Update row if exists
$updated = DB::update(
prefixTable('misc'),
['valeur' => time()],
'type = "admin" AND intitule = "last_cron_exec"'
);

// Insert row if not exists
if ($updated === 0) {
DB::insert(
prefixTable('misc'), [
'type' => 'admin',
'intitule' => 'last_cron_exec',
'valeur' => time()
]);
}

// Build the scheduler jobs
// https://github.com/peppeocchi/php-cron-scheduler
$scheduler->php(__DIR__.'/../scripts/background_tasks___userKeysCreation.php')->everyMinute($SETTINGS['user_keys_job_frequency'] ?? '1');
Expand Down

0 comments on commit e3318a4

Please sign in to comment.