Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Feb 12, 2025
1 parent 13ca2de commit 8693027
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 71 deletions.
11 changes: 10 additions & 1 deletion www/controllers/Layout/Container/vars/tasks/log.vars.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* Get the log file of the task
*/
try {
/**
* If a task Id is provided in the URL, use it
* Otherwise, get the latest task Id
*/
if (!empty(__ACTUAL_URI__[2]) and (is_numeric(__ACTUAL_URI__[2]))) {
$taskId = __ACTUAL_URI__[2];
} else {
Expand All @@ -29,7 +33,12 @@
$taskLogController = new \Controllers\Task\Log\Log($taskId);

// Get raw params from the task
$rawParams = json_decode($taskInfo['Raw_params'], true);
try {
$rawParams = json_decode($taskInfo['Raw_params'], true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new Exception('Error while reading log: could not get task params from task #' . $taskId . ': ' . $e->getMessage());
}

$repoId = null;
$snapId = null;
$envId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/**
* Get list of queued tasks, with offset
*/
$reloadableTableContent = $myTask->listNewest(true, $reloadableTableOffset);
$reloadableTableContent = $myTask->listQueued('', true, $reloadableTableOffset);

/**
* Get list of ALL queued tasks, without offset, for the total count
*/
$reloadableTableTotalItems = count($myTask->listNewest());
$reloadableTableTotalItems = count($myTask->listQueued());

/**
* Count total pages for the pagination
Expand Down
5 changes: 5 additions & 0 deletions www/controllers/Service/ScheduledTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,15 @@ public function execute()
echo $this->getDate() . ' Launching scheduled task #' . $taskId . '...' . PHP_EOL;

try {
// Add the scheduled task to the queue and execute it
$this->taskController->updateStatus($taskId, 'queued');
$this->taskController->executeId($taskId);
} catch (Exception $e) {
$this->logController->log('error', 'Service', 'Error while launching scheduled task: ' . $e->getMessage());
}

// Let some time between each task, to make sure the queue system works properly
sleep(1);
}
}
}
Expand Down
30 changes: 19 additions & 11 deletions www/controllers/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ public function updateDuration(int $id, string $duration) : void
}

/**
* List all newest tasks
* List all queued tasks
* It is possible to filter the type of task ('immediate' or 'scheduled')
* It is possible to add an offset to the request
*/
public function listNewest(bool $withOffset = false, int $offset = 0)
public function listQueued(string $type = '', bool $withOffset = false, int $offset = 0)
{
return $this->model->listNewest($withOffset, $offset);
return $this->model->listQueued($type, $withOffset, $offset);
}

/**
Expand Down Expand Up @@ -290,17 +291,18 @@ private function new(array $params) : int
{
/**
* Default values
* By default the task is new and immediate
* By default the task is immediate and is queued
*/
$status = 'new';
$type = 'immediate';
$type = 'immediate';
$status = 'queued';

/**
* If task is scheduled
* If task is scheduled then overwrite the type and status
* Task is not queued immediately, it will be queued at the scheduled time (when the service will launch the task)
*/
if ($params['schedule']['scheduled'] == 'true') {
$type = 'scheduled';
$status = 'scheduled';
$type = 'scheduled';
}

/**
Expand Down Expand Up @@ -334,10 +336,16 @@ private function new(array $params) : int
}
}

try {
$paramsJson = json_encode($params, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new Exception('Could not encode task parameters: ' . $e->getMessage());
}

/**
* Add the task in database
*/
$taskId = $this->model->new($type, json_encode($params), $status);
$taskId = $this->model->new($type, $paramsJson, $status);

return $taskId;
}
Expand Down Expand Up @@ -755,15 +763,15 @@ public function getChildrenPid(int $pid)
}

/**
* Enable a task
* Enable a recurrent task
*/
public function enable(int $id)
{
$this->model->enable($id);
}

/**
* Disable a task
* Disable a recurrent task
*/
public function disable(int $id)
{
Expand Down
44 changes: 30 additions & 14 deletions www/models/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,29 @@ public function somethingRunning()
* List all newest tasks
* It is possible to add an offset to the request
*/
public function listNewest(bool $withOffset, int $offset)
public function listQueued(string $type, bool $withOffset, int $offset)
{
$data = array();

try {
$query = "SELECT * FROM tasks
WHERE Status = 'new'
ORDER BY Date DESC, Time DESC";
/**
* Case where we want all types
*/
if (empty($type)) {
$query = "SELECT * FROM tasks
WHERE Status = 'queued'
ORDER BY Date DESC, Time DESC";
}

/**
* Case where we want to filter by type
*/
if (!empty($type)) {
$query = "SELECT * FROM tasks
WHERE Type = :type
AND Status = 'queued'
ORDER BY Date DESC, Time DESC";
}

/**
* Add offset if needed
Expand All @@ -161,9 +176,10 @@ public function listNewest(bool $withOffset, int $offset)
* Prepare query
*/
$stmt = $this->db->prepare($query);
$stmt->bindValue(':type', $type);
$stmt->bindValue(':offset', $offset, SQLITE3_INTEGER);
$result = $stmt->execute();
} catch (\Exception $e) {
} catch (Exception $e) {
$this->db->logError($e);
}

Expand Down Expand Up @@ -281,8 +297,7 @@ public function listDone(string $type, bool $withOffset, int $offset)
*/
if (empty($type)) {
$query = "SELECT * FROM tasks
WHERE Status = 'new'
OR Status = 'error'
WHERE Status = 'error'
OR Status = 'done'
OR Status = 'stopped'
ORDER BY Date DESC, Time DESC";
Expand All @@ -294,8 +309,7 @@ public function listDone(string $type, bool $withOffset, int $offset)
if (!empty($type)) {
$query = "SELECT * FROM tasks
WHERE Type = :type
AND (Status = 'new'
OR Status = 'error'
AND (Status = 'error'
OR Status = 'done'
OR Status = 'stopped')
ORDER BY Date DESC, Time DESC";
Expand Down Expand Up @@ -335,7 +349,9 @@ public function getLastTaskId() : int

try {
$result = $this->db->query("SELECT Id FROM tasks
WHERE Status != 'scheduled'
WHERE Status != 'queued'
AND Status != 'scheduled'
AND Status !='disabled'
ORDER BY Id DESC LIMIT 1");
} catch (\Exception $e) {
$this->db->logError($e);
Expand Down Expand Up @@ -383,7 +399,7 @@ public function getNextScheduledTask()
$data = array();

try {
$result = $this->db->query("SELECT * FROM tasks WHERE Type = 'scheduled' AND Status = 'scheduled'");
$result = $this->db->query("SELECT * FROM tasks WHERE Type = 'scheduled' AND Status = 'queued'");
} catch (\Exception $e) {
$this->db->logError($e);
}
Expand Down Expand Up @@ -422,7 +438,7 @@ public function new(string $type, string $rawParams, string $status) : int
public function duplicate(int $id) : int
{
try {
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) SELECT Type, Raw_params, 'new' FROM tasks WHERE Id = :id");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) SELECT Type, Raw_params, 'queued' FROM tasks WHERE Id = :id");
$stmt->bindValue(':id', $id);
$stmt->execute();
} catch (\Exception $e) {
Expand Down Expand Up @@ -452,7 +468,7 @@ public function close(int $id, string $status, string $duration)
}

/**
* Enable a task
* Enable a recurrent task
*/
public function enable(int $id)
{
Expand All @@ -466,7 +482,7 @@ public function enable(int $id)
}

/**
* Disable a task
* Disable a recurrent task
*/
public function disable(int $id)
{
Expand Down
2 changes: 1 addition & 1 deletion www/public/resources/js/functions/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function autorefresh()
// Remove autorefresh lock
localStorage.removeItem('autorefreshLock');
});
}, 2000);
}, 2500);
}

/**
Expand Down
41 changes: 32 additions & 9 deletions www/tasks/execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,32 @@
/**
* Retrieve task details
*/
$taskParams = $myTask->getById($taskId);
$task = $myTask->getById($taskId);

if (empty($taskParams)) {
if (empty($task)) {
throw new Exception('Cannot get task details from task #' . $taskId . ': empty results.');
}

$taskParams = json_decode($taskParams['Raw_params'], true);
try {
$taskRawParams = json_decode($task['Raw_params'], true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new Exception('Cannot decode task params from task #' . $taskId . ': ' . $e->getMessage());
}

if (empty($taskParams['action'])) {
if (empty($taskRawParams['action'])) {
throw new Exception('Action not specified');
}

/**
* Generate controller name
*/
$controllerPath = '\Controllers\Task\Repo\\' . ucfirst($taskParams['action']);
$controllerPath = '\Controllers\Task\Repo\\' . ucfirst($taskRawParams['action']);

/**
* Check if class exists, otherwise the action might be invalid
*/
if (!class_exists($controllerPath)) {
throw new Exception('Invalid action: ' . $taskParams['action']);
throw new Exception('Invalid action: ' . $taskRawParams['action']);
}

/**
Expand All @@ -80,12 +84,31 @@
}

/**
* Get the newest task in the queue (tasks with Status = 'new')
* If this task type is 'scheduled', the task can be started now.
* It has more priority than any 'immediate' tasks because it has a specific time to be run.
*/
if ($task['Type'] == 'scheduled') {
break;
}

/**
* If the task type is 'immediate', get all currently queued tasks
*/
$newestTask = $myTask->listNewest();
$newestTask = $myTask->listQueued();

/**
* If there are tasks of type 'scheduled' in the queue list, we wait, they have more priority
*/
foreach ($newestTask as $task) {
if ($task['Type'] == 'scheduled') {
sleep(5);
continue 2;
}
}

/**
* If the first task in the newest tasks list has the same Id as $taskId, then this task can be started
* If there is no task of type 'scheduled' in the queue list, this task may be started
* If the first task in the list has the same Id as $taskId, then this task can be started
*/
if ($newestTask[0]['Id'] == $taskId) {
break;
Expand Down
2 changes: 1 addition & 1 deletion www/update/database/ci/deb/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
'schedule-recipient' => ['']
];

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
2 changes: 1 addition & 1 deletion www/update/database/ci/deb/duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
'schedule-recipient' => ['']
];

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
2 changes: 1 addition & 1 deletion www/update/database/ci/deb/env.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
'schedule-recipient' => ['']
];

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
2 changes: 1 addition & 1 deletion www/update/database/ci/deb/mirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
];
$rawParams['repo-id'] = 'debian|bookworm|contrib';

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
2 changes: 1 addition & 1 deletion www/update/database/ci/deb/rebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
'schedule-recipient' => ['']
];

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
2 changes: 1 addition & 1 deletion www/update/database/ci/deb/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
'schedule-recipient' => ['']
];

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
2 changes: 1 addition & 1 deletion www/update/database/ci/rpm/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
'schedule-recipient' => ['']
];

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
2 changes: 1 addition & 1 deletion www/update/database/ci/rpm/duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
'schedule-recipient' => ['']
];

$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'new');");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) VALUES ('immediate', :rawParams, 'queued');");
$stmt->bindParam(':rawParams', json_encode($rawParams));
$stmt->execute();
Loading

0 comments on commit 8693027

Please sign in to comment.