Commit ee90df73 by Alexis Ragot

Bug #6229 - fix execution of scheduled processes

parent 16872d66
......@@ -119,22 +119,21 @@ class scheduling
}
/**
* Execute a scheduling
* Execute a scheduling task
*
* @param string $schedulingId Scheduling identifiant
* @param string $schedulingId The Scheduling identifier
*
* @return batchProcessing/scheduling
*/
public function execute($schedulingId)
{
$info = "";
$status = true;
$scheduling = $this->sdoFactory->read("batchProcessing/scheduling", $schedulingId);
$task = $this->sdoFactory->read("batchProcessing/task", (string) $scheduling->taskId);
if (!$scheduling) {
throw \laabs::newException("batchProcessing/schedulingException", "Invalid identifiant.");
throw \laabs::newException("batchProcessing/schedulingException", "Invalid identifier.");
}
if ($accountToken = \laabs::getToken('AUTH')) {
......@@ -157,14 +156,18 @@ class scheduling
$this->changeStatus($schedulingId, "error");
$status = false;
$info = $e;
throw \laabs::newException("batchProcessing/schedulingException", "Execution error : %s", 500, $e, [$e->getMessage()]);
}
$scheduling->lastExecution = \laabs::newDateTime(null, 'UTC');
$scheduling->status = "scheduled";
if ($status) {
$scheduling->lastExecution = \laabs::newDateTime(null, 'UTC');
$scheduling->status = "scheduled";
$frequency = explode(";", $scheduling->frequency);
$scheduling->nextExecution = $this->nextExecution($frequency);
} else {
$scheduling->status = "error";
}
$frequency = explode(";", $scheduling->frequency);
$scheduling->nextExecution = $this->nextExecution($frequency);
$this->update($scheduling);
$this->logSchedulingController->add($schedulingId, $scheduling->executedBy, $launchedBy, $status, $info);
......@@ -183,94 +186,31 @@ class scheduling
$currentDate = \laabs::newDateTime(null, 'UTC');
$res = [];
/**
* [0] start Minutes
* [1] start Hours
* [2] Day week ex : Fri
* [3] Day month ex : 1,2,3
* [4] month ex : apr, may, jun
* [5] number of case 7
* [6] Frequence unity minute (m) or hour (h)
* [7] end Minutes
* [8] end Hours
* Exemple frequency = [0;18;"Thu";"";"";"5";"m";"0";"20"];
* Thursday 18h -> 20h every 5 Minutes
*/
foreach ($schedulings as $scheduling) {
if ($scheduling->status == "scheduled") {
$executedTask = null;
if ($scheduling->status != "scheduled") {
continue;
}
if (empty($scheduling->nextExecution)) {
$frequency = explode(";", $scheduling->frequency);
if ($scheduling->lastExecution && $scheduling->nextExecution) {
/*$firstExecution = \laabs::newDateTime();
$firstExecution->setTime($frequency[1], $frequency[0], "0");*/
$interval = $currentDate->diff($scheduling->nextExecution, false);
if ($interval->invert == 1) {
if ($frequency[2] == "" && $frequency[3] == "" && $frequency[8] == "") {
$this->execute($scheduling->schedulingId);
} elseif ($frequency[7] != "" && $frequency[8] != "") {
$scheduling->nextExecution->setTime($frequency[8], $frequency[7], "0");
$interval = $currentDate->diff($scheduling->nextExecution, false);
if ($interval->invert == 0) {
if ($frequency[2] == "" && $frequency[3] == "") {
$this->execute($scheduling->schedulingId);
} elseif ($frequency[2] != "") {
strpos($frequency[2], strtoupper(date("D"))) > -1 ? $this->execute($scheduling->schedulingId) : false;
} else {
if ($frequency[4] != "") {
if (strpos($frequency[4], date("M")) == false) {
continue;
}
}
$daysMonth = explode(",", $frequency[3]);
$currentDay = date("j");
foreach ($daysMonth as $day) {
if ($day == $currentDay) {
$this->execute($scheduling->schedulingId);
}
}
}
}
} else {
if ($frequency[2] != "") {
strpos($frequency[2], strtoupper(date("D"))) > -1 ? $this->execute($scheduling->schedulingId) : false;
} else {
if ($frequency[4] != "") {
if (strpos($frequency[4], strtoupper(date("M"))) == false) {
continue;
}
}
$daysMonth = explode(",", $scheduling->frequency[3]);
$currentDay = date("j");
foreach ($daysMonth as $day) {
if ($day == $currentDay) {
$this->execute($scheduling->schedulingId);
}
}
}
}
}
} else {
if ($scheduling->nextExecution) {
$interval = $currentDate->diff($scheduling->nextExecution, false);
if ($interval->invert == 1) {
$this->execute($scheduling->schedulingId);
}
} else {
$scheduling->nextExecution = $this->nextExecution($frequency);
$this->sdoFactory->update($scheduling,"batchProcessing/scheduling");
}
}
$scheduling->nextExecution = $this->nextExecution($frequency);
$this->sdoFactory->update($scheduling,"batchProcessing/scheduling");
}
$interval = $scheduling->nextExecution->getTimestamp() - $currentDate->getTimestamp();
if ($interval <= 0) {
$executedTask = $this->execute($scheduling->schedulingId);
}
if (!empty($executedTask)) {
$res[$executedTask->schedulingId] = $executedTask->status;
}
}
return $res;
}
......@@ -291,7 +231,7 @@ class scheduling
}
if (!$scheduling) {
throw \laabs::newException("batchProcessing/schedulingException", "Invalid identifiant.");
throw \laabs::newException("batchProcessing/schedulingException", "Invalid identifier.");
}
$scheduling->status = $status;
......
......@@ -62,8 +62,6 @@ class scheduling
$tasks = \laabs::callService('batchProcessing/scheduling/readTasks');
$serviceAccount = \laabs::callService('auth/serviceAccount/readIndex');
// $bundle = \laabs::newController('batchProcessing/scheduling');
// $bundle->process();
foreach ($scheduledTasks as $scheduledTask) {
$scheduledTask->taskName = $tasks[$scheduledTask->taskId]->description;
......@@ -181,9 +179,14 @@ class scheduling
*/
public function execute($result)
{
$this->json->status = true;
if ($result->status == "error") {
$this->json->status = false;
$this->json->message = "An error occurred during execution";
} else {
$this->json->status = true;
$this->json->message = "Task execution triggered";
}
$this->json->message = "Task execution triggered";
$this->json->message = $this->translator->getText($this->json->message);
$result->lastExecution = \laabs::newDateTime($result->lastExecution)->setTimezone(timezone_open(date_default_timezone_get()));
$result->lastExecution = $result->lastExecution->format("Y-m-d H:i:s P");
......
......@@ -227,3 +227,6 @@ msgstr "Historique"
msgid "Logs scheduling list"
msgstr "Liste d'évènement de planification"
msgid "An error occurred during execution"
msgstr "Une erreur s'est produite lors de l'exécution"
......@@ -23,7 +23,7 @@
<a href="#" class="list-group-item clearfix" data-json="[?merge .json ?]">
<span class="col-xs-9">
<h4 class="list-group-item-heading schedulingName"><?merge .name ?></h4>
<h4 class="text-danger [?merge .status.ifeq('error').then('', 'hide') ?]"><code><i class="fa fa-warning">&nbsp;</i>Execution error</code></h4>
<h4 class="text-danger [?merge .status.ifeq('error').then('', 'hide') ?]" name="displayError"><code><i class="fa fa-warning">&nbsp;</i>Execution error</code></h4>
<p class="list-group-item-text">
<span class="row-fluid"><i class="fa fa-clock-o"/> <span class="startTime"><?merge .startHours ?><b>:</b><?merge .startMinutes ?></span></span>
<?merge .weekDays.count().bool() ?>
......@@ -83,10 +83,6 @@
</span>
<span class="col-xs-2 text-right">
<input type="checkbox" class="status" name="status" data-toggle="toggle" checked="[?merge .status.ifeq('scheduled').then('checked') ?]">
<!--h4>
<span class="playTask [?merge .status.ifeq('paused').then('text-muted', 'text-success') ?]" title="Play"><i class="fa fa-play"/></span>
<span class="pauseTask [?merge .status.ifeq('paused').then('text-success', 'text-muted') ?]" title="Pause"><i class="fa fa-pause"/></span>
</h4-->
</span>
<span class="col-xs-1 text-right">
<div class="deleteTask" title="Remove"><i class="fa fa-times"></i></div>
......@@ -275,10 +271,10 @@
});
// play task
$('#taskList .status').on('click', function(e) {
$('#taskList').on('click', "[name=status]", function(e) {
var task = $(this).closest('a');
if ($(this).is(':checked')) {
if ($(this).prop('checked')) {
TaskList.enable(task);
} else {
TaskList.disable(task);
......@@ -431,8 +427,13 @@
success : function (response) {
gritter.show(response.message, response.status, response.errors);
if (response.status == true) {
a.find('.nextExecution').html(response.object.nextExecution);
a.find('.lastExecution').html(response.object.lastExecution);
a.find('.nextExecution').text(response.object.nextExecution);
a.find('.lastExecution').text(response.object.lastExecution);
a.find("[name=displayError]").hide();
a.find('.status').bootstrapToggle('on');
} else {
a.find("[name=displayError]").show();
a.find('.status').bootstrapToggle('off');
}
},
error : function(response) {
......@@ -451,7 +452,7 @@
success : function (response) {
gritter.show(response.message, response.status, response.errors);
if (response.status == true) {
a.find('.nextExecution').html("");
a.find('.nextExecution').text("");
}
},
error : function(response) {
......
......@@ -496,9 +496,14 @@
this.form.find("[name='endHours']").val(scheduling.endHours);
}
}
var liste = this.form.find(".parametersList");
liste.empty();
if (!scheduling.parameters) {
scheduling.parameters = [];
}
$.each(scheduling.parameters, function(key,value){
var li = $('<li/>')
.attr('parameters', value)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment