Skip to content

Commit

Permalink
refactor clear functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jan 2, 2025
1 parent 69d6f41 commit 4a4db9d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/SimpleJobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function trigger_next_task()
*
* If unspecified, it will run all jobs and the next task
*
* @return void|string
* @return void
*/
public function trigger()
{
Expand Down Expand Up @@ -248,7 +248,8 @@ public function trigger()
if ($t && $nowt) {
$nowMinusFive = strtotime("-5 minutes", $nowt);
if (strtotime($t) > $nowMinusFive) {
die("Prevent running concurrent queues");
$this->output("Prevent running concurrent queues");
return;
}
}

Expand All @@ -263,8 +264,11 @@ public function trigger()

$tasks = CronJob::allTasks();
if (empty($tasks)) {
return "There are no implementators of CronTask to run";
$this->output("There are no implementators of CronTask to run");
return;
}

// Do we have a cron job to run ?
if (!$type || $type == "cron") {
foreach ($tasks as $subclass) {
$cronJob = CronJob::getByTaskClass($subclass);
Expand All @@ -277,12 +281,14 @@ public function trigger()
$this->runTask($task);
}

if (empty($tasks)) {
$this->output("No jobs");
}

// Avoid the table to be full of stuff
if (self::config()->auto_clean) {
$time = date('Y-m-d', strtotime(self::config()->auto_clean_threshold));
if (self::config()->store_results) {
$sql = "DELETE FROM \"CronTaskResult\" WHERE \"Created\" < '$time'";
DB::query($sql);
self::clearResultsTable();
}
}
}
Expand All @@ -296,11 +302,10 @@ public function trigger()
} else {
$this->output("No task");
}

// Avoid the table to be full of stuff
if (self::config()->auto_clean) {
$time = date('Y-m-d', strtotime(self::config()->auto_clean_threshold));
$sql = "DELETE FROM \"SimpleTask\" WHERE \"Created\" < '$time'";
DB::query($sql);
self::clearTasksTable();
}
}

Expand All @@ -310,6 +315,32 @@ public function trigger()
}
}

/**
* @return void
*/
public static function clearTasksTable()
{
if (!self::config()->auto_clean_threshold) {
return;
}
$time = date('Y-m-d', strtotime(self::config()->auto_clean_threshold));
$sql = "DELETE FROM \"SimpleTask\" WHERE \"Created\" < '$time'";
DB::query($sql);
}

/**
* @return void
*/
public static function clearResultsTable()
{
if (!self::config()->auto_clean_threshold) {
return;
}
$time = date('Y-m-d', strtotime(self::config()->auto_clean_threshold));
$sql = "DELETE FROM \"CronTaskResult\" WHERE \"Created\" < '$time'";
DB::query($sql);
}

/**
* Determine if a task should be run
*
Expand Down
14 changes: 14 additions & 0 deletions tests/SimpleJobsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\Security\Security;
use SilverStripe\Control\Controller;
use LeKoala\SimpleJobs\SimpleJobsController;
use SilverStripe\ORM\DB;
use SilverStripe\Security\DefaultAdminService;

/**
Expand Down Expand Up @@ -93,4 +94,17 @@ public function testHasTasks(): void
{
$this->assertNotEmpty(CronJob::allTasks());
}

public function testClearResults(): void
{
$t = date('Y-m-d', strtotime('-1 year'));
DB::query("INSERT INTO CronTaskResult (Created) VALUES ('$t')");

$count = DB::query('SELECT COUNT(*) FROM CronTaskResult')->value();

SimpleJobsController::clearResultsTable();

$newCount = DB::query('SELECT COUNT(*) FROM CronTaskResult')->value();
$this->assertNotEquals($count, $newCount);
}
}

0 comments on commit 4a4db9d

Please sign in to comment.