Skip to content
Snippets Groups Projects
Commit d80e0c39 authored by Guillaume Heurtier's avatar Guillaume Heurtier
Browse files

FEAT #17163 TIME 4:30 create/delete scheduler script in crontab

parent 2f384aef
No related branches found
No related tags found
No related merge requests found
...@@ -594,6 +594,8 @@ ...@@ -594,6 +594,8 @@
"periodicNotification": "Notification(s) périodique(s)", "periodicNotification": "Notification(s) périodique(s)",
"batchMasterNotInitialized": "Le planificateur des notifications n'a pas été <b>initialisé</b> ! Les notifications ne pourront pas être envoyé.", "batchMasterNotInitialized": "Le planificateur des notifications n'a pas été <b>initialisé</b> ! Les notifications ne pourront pas être envoyé.",
"initializeBatchMaster": "Initialiser le planificateur", "initializeBatchMaster": "Initialiser le planificateur",
"noScheduledNotif": "Aucune notification périodique n'est disponible." "noScheduledNotif": "Aucune notification périodique n'est disponible.",
"schedulerScriptCreated": "Planificateur créé",
"schedulerScriptDeleted": "Planificateur supprimé"
} }
} }
...@@ -162,10 +162,13 @@ $app->get('/workflowTemplates/{id}', \Workflow\controllers\WorkflowTemplateContr ...@@ -162,10 +162,13 @@ $app->get('/workflowTemplates/{id}', \Workflow\controllers\WorkflowTemplateContr
$app->delete('/workflowTemplates/{id}', \Workflow\controllers\WorkflowTemplateController::class . ':delete'); $app->delete('/workflowTemplates/{id}', \Workflow\controllers\WorkflowTemplateController::class . ':delete');
//NotificationsSchedule //NotificationsSchedule
$app->post('/schedule', \Notification\controllers\NotificationsScheduleController::class.':create'); $app->post('/schedule/initialization', \Notification\controllers\NotificationsScheduleController::class . ':initializeSchedulerScript');
$app->get('/schedule', \Notification\controllers\NotificationsScheduleController::class.':get'); $app->get('/schedule/initialization', \Notification\controllers\NotificationsScheduleController::class . ':checkSchedulerScriptInitialized');
$app->get('/schedule/{id}', \Notification\controllers\NotificationsScheduleController::class.':getById'); $app->delete('/schedule', \Notification\controllers\NotificationsScheduleController::class . ':deleteSchedulerScript');
$app->put('/schedule/{id}', \Notification\controllers\NotificationsScheduleController::class.':update'); $app->post('/schedule', \Notification\controllers\NotificationsScheduleController::class . ':create');
$app->delete('/schedule/{id}', \Notification\controllers\NotificationsScheduleController::class.':delete'); $app->get('/schedule', \Notification\controllers\NotificationsScheduleController::class . ':get');
$app->get('/schedule/{id}', \Notification\controllers\NotificationsScheduleController::class . ':getById');
$app->put('/schedule/{id}', \Notification\controllers\NotificationsScheduleController::class . ':update');
$app->delete('/schedule/{id}', \Notification\controllers\NotificationsScheduleController::class . ':delete');
$app->run(); $app->run();
...@@ -14,12 +14,15 @@ ...@@ -14,12 +14,15 @@
namespace Notification\controllers; namespace Notification\controllers;
use Configuration\models\ConfigurationModel;
use Notification\models\NotificationsScheduleModel; use Notification\models\NotificationsScheduleModel;
use Group\controllers\PrivilegeController; use Group\controllers\PrivilegeController;
use History\controllers\HistoryController; use History\controllers\HistoryController;
use Respect\Validation\Validator; use Respect\Validation\Validator;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
use SrcCore\models\CoreConfigModel;
use SrcCore\models\ValidatorModel;
class NotificationsScheduleController class NotificationsScheduleController
{ {
...@@ -214,6 +217,141 @@ class NotificationsScheduleController ...@@ -214,6 +217,141 @@ class NotificationsScheduleController
return $response->withStatus(204); return $response->withStatus(204);
} }
public function initializeSchedulerScript(Request $request, Response $response)
{
if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_notifications'])) {
return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
}
$configuration = ConfigurationModel::getByIdentifier(['identifier' => 'customization']);
$configuration = $configuration[0];
$configuration['value'] = json_decode($configuration['value'], true);
if (empty($configuration['value']['schedulerPath']) || !is_file($configuration['value']['schedulerPath'])) {
$schedulerScript = NotificationsScheduleController::createSchedulerScript();
$configuration['value']['schedulerPath'] = $schedulerScript['path'];
}
// Check if script already in crontab
exec('crontab -l|grep ' . $configuration['value']['schedulerPath'], $output);
if (empty($output)) {
// run scheduler every minutes
$crontabLine = '*/1 * * * * ' . $configuration['value']['schedulerPath'];
exec('(crontab -l; echo "' . $crontabLine . '")|crontab -');
HistoryController::add([
'code' => 'OK',
'objectType' => 'configurations',
'objectId' => $configuration['id'],
'type' => 'MODIFICATION',
'message' => "{schedulerScriptCreated} : {$configuration['value']['schedulerPath']}"
]);
}
return $response->withStatus(204);
}
public function checkSchedulerScriptInitialized(Request $request, Response $response)
{
if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_notifications'])) {
return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
}
$configuration = ConfigurationModel::getByIdentifier(['identifier' => 'customization']);
$configuration = $configuration[0];
$configuration = json_decode($configuration['value'], true);
if (empty($configuration['schedulerPath']) || !is_file($configuration['schedulerPath'])) {
return $response->withStatus(400)->withJson(['errors' => 'No scheduler created']);
}
exec('crontab -l|grep ' . $configuration['schedulerPath'], $output);
if (empty($output)) {
return $response->withStatus(400)->withJson(['errors' => 'Scheduler not set in crontab']);
}
return $response->withStatus(204);
}
public function deleteSchedulerScript(Request $request, Response $response)
{
if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_notifications'])) {
return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
}
$configuration = ConfigurationModel::getByIdentifier(['identifier' => 'customization']);
$configuration = $configuration[0];
$configuration['value'] = json_decode($configuration['value'], true);
if (empty($configuration['value']['schedulerPath'])) {
return $response->withStatus(204);
}
if (is_file($configuration['value']['schedulerPath'])) {
unlink($configuration['value']['schedulerPath']);
}
exec('crontab -l|grep ' . $configuration['value']['schedulerPath'], $output);
if (empty($output)) {
return $response->withStatus(204);
}
exec('(crontab -l| grep -v "' . $configuration['value']['schedulerPath'] . '")|crontab -');
$configuration['value']['schedulerPath'] = '';
$configuration['value'] = json_encode($configuration['value']);
ConfigurationModel::update(['set' => ['value' => $configuration['value']], 'where' => ['identifier = ?'], 'data' => ['customization']]);
HistoryController::add([
'code' => 'OK',
'objectType' => 'configurations',
'objectId' => $configuration['id'],
'type' => 'SUPPRESSION',
'message' => "{schedulerScriptDeleted} : {$configuration['value']['schedulerPath']}"
]);
return $response->withStatus(204);
}
private static function createSchedulerScript()
{
$filename = 'scheduler_' . rand() . '.sh';
$corePath = str_replace('src/app/notifications/controllers', '', __DIR__);
$corePath = str_replace('src/app/notification/models', '', $corePath);
$configPath = CoreConfigModel::getConfigPath();
$fullPath = $corePath . 'bin/' . $filename;
$file_open = fopen($fullPath, 'w+');
fwrite($file_open, '#!/bin/sh');
fwrite($file_open, "\n");
fwrite($file_open, 'path=\''.$corePath.'\'');
fwrite($file_open, "\n");
fwrite($file_open, 'cd $path');
fwrite($file_open, "\n");
fwrite($file_open, 'filePath=\''.$corePath.'bin/scheduler.php\'');
fwrite($file_open, "\n");
fwrite($file_open, 'php $filePath '.$configPath);
fwrite($file_open, "\n");
fclose($file_open);
shell_exec('chmod +x '.escapeshellarg($fullPath));
$configuration = ConfigurationModel::getByIdentifier(['identifier' => 'customization']);
$configuration = $configuration[0];
$configuration['value'] = json_decode($configuration['value'], true);
$configuration['value']['schedulerPath'] = $fullPath;
$configuration['value'] = json_encode($configuration['value']);
ConfigurationModel::update(['set' => ['value' => $configuration['value']], 'where' => ['id = ?'], 'data' => [$configuration['id']]]);
return ['path' => $fullPath];
}
private static function validateBody($body) private static function validateBody($body)
{ {
if (empty($body)) { if (empty($body)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment