Skip to content

Commit

Permalink
4.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Feb 10, 2025
1 parent ab620b9 commit 5edfb18
Show file tree
Hide file tree
Showing 22 changed files with 285 additions and 62 deletions.
16 changes: 16 additions & 0 deletions www/controllers/App/Config/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ public static function get()
}
}

if (!defined('TASK_QUEUING')) {
if (!empty($settings['TASK_QUEUING'])) {
define('TASK_QUEUING', $settings['TASK_QUEUING']);
} else {
define('TASK_QUEUING', 'false');
}
}

if (!defined('TASK_QUEUING_MAX_SIMULTANEOUS')) {
if (!empty($settings['TASK_QUEUING_MAX_SIMULTANEOUS'])) {
define('TASK_QUEUING_MAX_SIMULTANEOUS', $settings['TASK_QUEUING_MAX_SIMULTANEOUS']);
} else {
define('TASK_QUEUING_MAX_SIMULTANEOUS', 2);
}
}

if (!defined('TASK_EXECUTION_MEMORY_LIMIT')) {
if (!empty($settings['TASK_EXECUTION_MEMORY_LIMIT'])) {
define('TASK_EXECUTION_MEMORY_LIMIT', $settings['TASK_EXECUTION_MEMORY_LIMIT']);
Expand Down
30 changes: 30 additions & 0 deletions www/controllers/Layout/Table/vars/tasks/list-queued.vars.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
$myTask = new \Controllers\Task\Task();
$reloadableTableOffset = 0;

/**
* Retrieve offset from cookie if exists
*/
if (!empty($_COOKIE['tables/tasks/list-queued/offset']) and is_numeric($_COOKIE['tables/tasks/list-queued/offset'])) {
$reloadableTableOffset = $_COOKIE['tables/tasks/list-queued/offset'];
}

/**
* Get list of queued tasks, with offset
*/
$reloadableTableContent = $myTask->listNewest(true, $reloadableTableOffset);

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

/**
* Count total pages for the pagination
*/
$reloadableTableTotalPages = ceil($reloadableTableTotalItems / 10);

/**
* Calculate current page number
*/
$reloadableTableCurrentPage = ceil($reloadableTableOffset / 10) + 1;
52 changes: 27 additions & 25 deletions www/controllers/Repo/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,35 +288,37 @@ public function delete(int $snapId, array $packages)
* Check that the file path starts with REPOS_DIR
* Prevents a malicious person from providing a path that has nothing to do with the repo directory (e.g. /etc/...)
*/
if (preg_match("#^" . REPOS_DIR . "#", $path)) {
/**
* Check that the file ends with .deb or .rpm otherwise we move on to the next one
*/
if (!preg_match("#.deb$#", $path) and !preg_match("#.rpm$#", $path)) {
continue;
}

/**
* If the file does not exist, we ignore it and move on to the next one
*/
if (!file_exists($path)) {
continue;
}
if (!preg_match("#^" . REPOS_DIR . "#", realpath($path))) {
throw new Exception('Invalid package path ' . $path);
}

/**
* Delete package
*/
if (!unlink($path)) {
throw new Exception('Unable to delete package ' . $path);
}
/**
* Check that the file ends with .deb or .rpm otherwise we move on to the next one
*/
if (!preg_match("#.deb$#", $path) and !preg_match("#.rpm$#", $path)) {
continue;
}

$deletedPackages[] = str_replace($repoPath . '/', '', $path);
/**
* If the file does not exist, we ignore it and move on to the next one
*/
if (!file_exists($path)) {
continue;
}

/**
* Set repo rebuild status to 'needed'
*/
$myrepo->snapSetRebuild($snapId, 'needed');
/**
* Delete package
*/
if (!unlink($path)) {
throw new Exception('Unable to delete package ' . $path);
}

$deletedPackages[] = str_replace($repoPath . '/', '', $path);

/**
* Set repo rebuild status to 'needed'
*/
$myrepo->snapSetRebuild($snapId, 'needed');
}

return $deletedPackages;
Expand Down
12 changes: 12 additions & 0 deletions www/controllers/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public function apply(array $sendSettings)
$settingsToApply['PROXY'] = '';
}

if (!empty($sendSettings['task-queuing'])) {
if ($sendSettings['task-queuing'] == 'true') {
$settingsToApply['TASK_QUEUING'] = 'true';
} else {
$settingsToApply['TASK_QUEUING'] = 'false';
}
}

if (!empty($sendSettings['task-queuing-max-simultaneous']) and is_numeric($sendSettings['task-queuing-max-simultaneous']) and $sendSettings['task-queuing-max-simultaneous'] > 0) {
$settingsToApply['TASK_QUEUING_MAX_SIMULTANEOUS'] = \Controllers\Common::validateData($sendSettings['task-queuing-max-simultaneous']);
}

if (!empty($sendSettings['task-execution-memory-limit']) and is_numeric($sendSettings['task-execution-memory-limit']) and $sendSettings['task-execution-memory-limit'] > 2) {
$settingsToApply['TASK_EXECUTION_MEMORY_LIMIT'] = \Controllers\Common::validateData($sendSettings['task-execution-memory-limit']);
}
Expand Down
23 changes: 20 additions & 3 deletions www/controllers/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ public function updateDuration(int $id, string $duration) : void
$this->model->updateDuration($id, $duration);
}

/**
* List all newest tasks
* It is possible to add an offset to the request
*/
public function listNewest(bool $withOffset = false, int $offset = 0)
{
return $this->model->listNewest($withOffset, $offset);
}

/**
* List all running tasks
* It is possible to filter the type of task ('immediate' or 'scheduled')
Expand Down Expand Up @@ -426,9 +435,9 @@ public function execute(array $tasksParams)
*/
public function executeId(int $id)
{
$myprocess = new \Controllers\Process('/usr/bin/php ' . ROOT . '/tasks/execute.php --id="' . $id . '" >/dev/null 2>/dev/null &');
$myprocess->execute();
$myprocess->close();
// $myprocess = new \Controllers\Process('/usr/bin/php ' . ROOT . '/tasks/execute.php --id="' . $id . '" >/dev/null 2>/dev/null &');
// $myprocess->execute();
// $myprocess->close();
}

/**
Expand Down Expand Up @@ -548,6 +557,10 @@ public function end()
*/
public function relaunch(int $id)
{
if (!IS_ADMIN) {
throw new Exception('You are not allowed to relaunch a task');
}

/**
* First, duplicate task in database
*/
Expand All @@ -574,6 +587,10 @@ private function duplicate(int $id) : int
*/
public function kill(string $taskId)
{
if (!IS_ADMIN) {
throw new Exception('You are not allowed to relaunch a task');
}

if (!file_exists(PID_DIR . '/' . $taskId . '.pid')) {
throw new Exception('Specified task PID does not exist');
}
Expand Down
11 changes: 9 additions & 2 deletions www/controllers/ajax/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,20 @@
* Get websocker server log content
*/
if ($action == 'get-wss-log' and !empty([$_POST['logfile']])) {
$logfile = \Controllers\Common::validateData($_POST['logfile']);

// Check if the log file is allowed and is not outside the logs directory. Verify that the user is not trying to do something malicious.
if (!preg_match('#^' . WS_LOGS_DIR . '#' , realpath(WS_LOGS_DIR . '/' . $logfile))) {
response(HTTP_BAD_REQUEST, 'Invalid log file');
}

// Check if the log file exists
if (!file_exists(WS_LOGS_DIR . '/' . $_POST['logfile'])) {
if (!file_exists(WS_LOGS_DIR . '/' . $logfile)) {
response(HTTP_BAD_REQUEST, 'Log file not found');
}

// Get the log content
$content = file_get_contents(WS_LOGS_DIR . '/' . $_POST['logfile']);
$content = file_get_contents(WS_LOGS_DIR . '/' . $logfile);

// Check if the log content was read successfully
if ($content === false) {
Expand Down
6 changes: 6 additions & 0 deletions www/models/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ private function generateMainTables()
EMAIL_RECIPIENT VARCHAR(255),
PROXY VARCHAR(255),
TASK_EXECUTION_MEMORY_LIMIT INTEGER,
TASK_QUEUING CHAR(5),
TASK_QUEUING_MAX_SIMULTANEOUS INTEGER,
/* Repo settings */
RETENTION INTEGER,
REPO_CONF_FILES_PREFIX VARCHAR(255),
Expand Down Expand Up @@ -587,6 +589,8 @@ private function generateMainTables()
REPO_CONF_FILES_PREFIX,
TIMEZONE,
TASK_EXECUTION_MEMORY_LIMIT,
TASK_QUEUING,
TASK_QUEUING_MAX_SIMULTANEOUS,
MIRRORING_PACKAGE_DOWNLOAD_TIMEOUT,
RPM_REPO,
RPM_SIGN_PACKAGES,
Expand Down Expand Up @@ -614,6 +618,8 @@ private function generateMainTables()
'repomanager-',
'Europe/Paris',
'1024',
'false',
'2',
'300',
'true',
'true',
Expand Down
39 changes: 38 additions & 1 deletion www/models/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ public function somethingRunning()
return false;
}

/**
* List all newest tasks
* It is possible to add an offset to the request
*/
public function listNewest(bool $withOffset, int $offset)
{
$data = array();

try {
$query = "SELECT * FROM tasks
WHERE Status = 'new'
ORDER BY Date DESC, Time DESC";

/**
* Add offset if needed
*/
if ($withOffset === true) {
$query .= " LIMIT 10 OFFSET :offset";
}

/**
* Prepare query
*/
$stmt = $this->db->prepare($query);
$stmt->bindValue(':offset', $offset, SQLITE3_INTEGER);
$result = $stmt->execute();
} catch (\Exception $e) {
$this->db->logError($e);
}

while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$data[] = $row;
}

return $data;
}

/**
* List all running tasks
* It is possible to filter the type of task ('immediate' or 'scheduled')
Expand Down Expand Up @@ -385,7 +422,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) SELECT Type, Raw_params FROM tasks WHERE Id = :id");
$stmt = $this->db->prepare("INSERT INTO tasks (Type, Raw_params, Status) SELECT Type, Raw_params, 'new' FROM tasks WHERE Id = :id");
$stmt->bindValue(':id', $id);
$stmt->execute();
} catch (\Exception $e) {
Expand Down
6 changes: 3 additions & 3 deletions www/public/resources/js/events/task/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,17 @@ $(document).on('click','.enable-scheduled-task-btn',function (e) {
});

/**
* Event: cancel scheduled task
* Event: cancel unlaunched task
*/
$(document).on('click','.cancel-scheduled-task-btn',function (e) {
$(document).on('click','.cancel-task-btn',function (e) {
// Prevent parent to be triggered
e.stopPropagation();

var taskId = $(this).attr('task-id');

confirmBox(
{
'title': 'Cancel and delete scheduled task',
'title': 'Cancel task',
'message': 'Are you sure you want to cancel and delete this task?',
'buttons': [
{
Expand Down
5 changes: 5 additions & 0 deletions www/public/resources/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ function getGetParams()
return array;
}

/**
* Return true if the value is empty
* @param {*} value
* @returns
*/
function empty(value)
{
// Check if the value is null or undefined
Expand Down
35 changes: 35 additions & 0 deletions www/tasks/execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@
throw new Exception('Invalid action: ' . $taskParams['action']);
}

/**
* If task queuing is enabled and the maximum number of simultaneous tasks is set, check if the task can be started
*/
if (!empty(TASK_QUEUING) and TASK_QUEUING == 'true' and !empty(TASK_QUEUING_MAX_SIMULTANEOUS)) {
while (true) {
/**
* Get running tasks
*/
$runningTasks = $myTask->listRunning();

/**
* If number of running tasks is greater than or equal to the maximum number of simultaneous tasks, we wait
*/
if (count($runningTasks) >= TASK_QUEUING_MAX_SIMULTANEOUS) {
sleep(5);
continue;
}

/**
* Get the newest task in the queue (tasks with Status = 'new')
*/
$newestTask = $myTask->listNewest();

/**
* If the first task in the newest tasks list has the same Id as $taskId, then this task can be started
*/
if ($newestTask[0]['Id'] == $taskId) {
break;
}

// Just for safety
sleep(5);
}
}

/**
* Instantiate controller and execute action
*/
Expand Down
18 changes: 18 additions & 0 deletions www/update/database/4.17.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* 4.17.0 update
*/

/**
* Add 'TASK_QUEUING' column to settings table
*/
if (!$this->db->columnExist('settings', 'TASK_QUEUING')) {
$this->db->exec("ALTER TABLE settings ADD COLUMN TASK_QUEUING BOOLEAN DEFAULT 'false'");
}

/**
* Add 'TASK_QUEUING_MAX_SIMULTANEOUS' column to settings table
*/
if (!$this->db->columnExist('settings', 'TASK_QUEUING_MAX_SIMULTANEOUS')) {
$this->db->exec("ALTER TABLE settings ADD COLUMN TASK_QUEUING_MAX_SIMULTANEOUS INTEGER DEFAULT '2'");
}
2 changes: 1 addition & 1 deletion www/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.16.4
4.17.0
2 changes: 1 addition & 1 deletion www/views/includes/containers/settings/health.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<?php
if (!empty($users)) : ?>
<div id="currentUsers">
<h6>CURRENT USERS</h6>
<h6 class="margin-bottom-5">CURRENT USERS</h6>

<?php
foreach ($users as $user) : ?>
Expand Down
Loading

0 comments on commit 5edfb18

Please sign in to comment.