Commit dd337852 authored by Roman Ondráček's avatar Roman Ondráček

Config: skip invalid scheduler task files

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent 4f7fde9c
Pipeline #2380 failed with stages
in 7 minutes and 2 seconds
......@@ -30,6 +30,7 @@ use App\ServiceModule\Exceptions\NotSupportedInitSystemException;
use App\ServiceModule\Models\ServiceManager;
use Contributte\FormMultiplier\Multiplier;
use Contributte\Translation\Wrappers\NotTranslate;
use DateTime;
use Nette\Application\UI\Form;
use Nette\Forms\Container;
use Nette\Forms\Controls\SubmitButton;
......@@ -39,6 +40,7 @@ use Nette\SmartObject;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use stdClass;
use Throwable;
/**
* Scheduler's task configuration form factory
......@@ -105,7 +107,13 @@ class SchedulerFormFactory {
$services = [];
}
$form->addGroup();
$form->addInteger('taskId', self::PREFIX . 'taskId');
$taskId = $form->addInteger('taskId', self::PREFIX . 'taskId');
try {
$now = new DateTime();
$taskId->setDefaultValue($now->getTimestamp());
} catch (Throwable $e) {
// Do nothing
}
$form->addSelect('clientId', self::PREFIX . 'clientId')
->setItems($services, false)
->setPrompt(self::PREFIX . 'messages.clientId-prompt')
......@@ -155,14 +163,17 @@ class SchedulerFormFactory {
* Loads the task
* @param int $id Task ID
* @return stdClass Task
* @throws JsonException
*/
private function load(int $id): stdClass {
$configuration = $this->manager->load($id);
foreach ($configuration->task as &$task) {
$task->message = Json::encode($task->message, Json::PRETTY);
try {
$configuration = $this->manager->load($id);
foreach ($configuration->task as &$task) {
$task->message = Json::encode($task->message, Json::PRETTY);
}
return $configuration;
} catch (InvalidJsonException | IOException | JsonException $e) {
return new stdClass();
}
return $configuration;
}
/**
......
......@@ -20,10 +20,12 @@ declare(strict_types = 1);
namespace App\ConfigModule\Models;
use App\CoreModule\Exceptions\InvalidJsonException;
use App\CoreModule\Models\CommandManager;
use App\CoreModule\Models\JsonFileManager;
use App\ServiceModule\Exceptions\NotSupportedInitSystemException;
use App\ServiceModule\Models\ServiceManager;
use Nette\IOException;
use Nette\SmartObject;
use Nette\Utils\Finder;
use Nette\Utils\JsonException;
......@@ -161,20 +163,23 @@ class SchedulerManager {
/**
* Gets tasks
* @return mixed[] Tasks
* @throws JsonException
*/
public function list(): array {
$tasks = [];
foreach ($this->getTaskFiles() as $id => $fileName) {
$data = $this->load($id);
$task = [
'id' => $data->taskId,
'time' => $this->timeManager->getTime($data),
'service' => $data->clientId,
'messaging' => $this->getTaskMessagings($data->task),
'mType' => $this->getTaskMessageTypes($data->task),
];
$tasks[] = $task;
try {
$data = $this->load($id);
$task = [
'id' => $data->taskId,
'time' => $this->timeManager->getTime($data),
'service' => $data->clientId,
'messaging' => $this->getTaskMessagings($data->task),
'mType' => $this->getTaskMessageTypes($data->task),
];
$tasks[] = $task;
} catch (InvalidJsonException | IOException | JsonException $e) {
// Do nothing
}
}
return $tasks;
}
......@@ -218,6 +223,9 @@ class SchedulerManager {
}
$this->fileName = strval($files[$id]);
$config = $this->fileManager->read($this->fileName, false);
if (!$this->schemaManager->validate($config)) {
throw new InvalidJsonException();
}
$this->timeManager->cronToString($config);
$this->fixTasks($config);
return $config;
......
......@@ -71,6 +71,9 @@ class SchedulerSchemaManager extends JsonSchemaManager {
$tasks = $json->task;
}
foreach ($tasks as $id => $task) {
if (!isset($task->message) || !isset($task->message->mType)) {
throw new InvalidJsonException();
}
$this->apiSchemaManager->setSchemaForRequest($task->message->mType);
$this->apiSchemaManager->validate($task->message);
}
......
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