diff --git a/config/config.php b/config/config.php index d8778aaa..5ed97dab 100644 --- a/config/config.php +++ b/config/config.php @@ -316,7 +316,7 @@ return (function () { DispatchCommand::TASK_NAME => [ 'command' => DispatchCommand::ROUTE, 'name' => DispatchCommand::TASK_NAME, - 'info' => 'The new events dispatcher system. That will replace the old events systems eventually. (cannot be disabled).', + 'info' => 'Dispatch queued events to their respective listeners.', 'enabled' => true, 'timer' => '* * * * *', 'args' => '-v', diff --git a/frontend/pages/tasks.vue b/frontend/pages/tasks.vue index e0337302..2f79e54d 100644 --- a/frontend/pages/tasks.vue +++ b/frontend/pages/tasks.vue @@ -50,7 +50,7 @@
{{ task.name }}
- + @@ -70,13 +70,21 @@
Timer:  - + + {{ task.timer }} + + {{ task.timer }}
Args:  - + + {{ task.args }} + + {{ task.args }}
@@ -162,7 +170,7 @@ import 'assets/css/bulma-switch.css' import moment from 'moment' import request from '~/utils/request' -import {awaitElement, makeConsoleCommand, notification, TOOLTIP_DATE_FORMAT} from '~/utils/index' +import {awaitElement, makeConsoleCommand, notification, parse_api_response, TOOLTIP_DATE_FORMAT} from '~/utils/index' import cronstrue from 'cronstrue' import Message from '~/components/Message' import {useStorage} from '@vueuse/core' @@ -199,11 +207,21 @@ onMounted(async () => await loadContent()) const toggleTask = async task => { try { const keyName = `WS_CRON_${task.name.toUpperCase()}` - await request(`/system/env/${keyName}`, { + + const oldState = task.enabled + + const update = await request(`/system/env/${keyName}`, { method: 'POST', body: JSON.stringify({"value": !task.enabled}) }) + if (200 !== update.status) { + const json = await parse_api_response(update) + notification('error', 'Error', `Failed to toggle task '${task.name}' status. ${json.error.message}`) + tasks.value[tasks.value.findIndex(b => b.name === task.name)].enabled = oldState + return + } + const response = await request(`/tasks/${task.name}`) tasks.value[tasks.value.findIndex(b => b.name === task.name)] = await response.json() } catch (e) { diff --git a/frontend/utils/index.js b/frontend/utils/index.js index 209b13e4..2edff8d9 100644 --- a/frontend/utils/index.js +++ b/frontend/utils/index.js @@ -465,6 +465,14 @@ const basename = (path, ext = '') => { return base } +const parse_api_response = async r => { + try { + return await r.json() + } catch (e) { + return {error: {code: r.status, message: r.statusText}} + } +} + export { r, ag_set, @@ -485,5 +493,6 @@ export { TOOLTIP_DATE_FORMAT, makeSecret, explode, - basename + basename, + parse_api_response, } diff --git a/src/API/Tasks/Index.php b/src/API/Tasks/Index.php index a25229ff..457db275 100644 --- a/src/API/Tasks/Index.php +++ b/src/API/Tasks/Index.php @@ -27,7 +27,7 @@ final class Index * @throws InvalidArgumentException */ #[Get(self::URL . '[/]', name: 'tasks.index')] - public function tasksIndex(iRequest $request): iResponse + public function tasksIndex(): iResponse { $queuedTasks = $this->cache->get('queued_tasks', []); $response = [ @@ -39,6 +39,8 @@ final class Index foreach (TasksCommand::getTasks() as $task) { $task = self::formatTask($task); $task['queued'] = in_array(ag($task, 'name'), $queuedTasks); + + $response['tasks'][] = $task; } @@ -49,12 +51,8 @@ final class Index * @throws InvalidArgumentException */ #[Route(['GET', 'POST', 'DELETE'], self::URL . '/{id:[a-zA-Z0-9_-]+}/queue[/]', name: 'tasks.task.queue')] - public function taskQueue(iRequest $request, array $args = []): iResponse + public function taskQueue(iRequest $request, string $id): iResponse { - if (null === ($id = ag($args, 'id'))) { - return api_error('No id was given.', Status::BAD_REQUEST); - } - $task = TasksCommand::getTasks($id); if (empty($task)) { @@ -85,12 +83,8 @@ final class Index * @throws InvalidArgumentException */ #[Get(self::URL . '/{id:[a-zA-Z0-9_-]+}[/]', name: 'tasks.task.view')] - public function taskView(iRequest $request, array $args = []): iResponse + public function taskView(string $id): iResponse { - if (null === ($id = ag($args, 'id'))) { - return api_error('No id was given.', Status::BAD_REQUEST); - } - $task = TasksCommand::getTasks($id); if (empty($task)) { @@ -127,6 +121,9 @@ final class Index $item['command'] = get_debug_type($item['command']); } + $ff = getEnvSpec('WS_CRON_' . strtoupper(ag($task, 'name'))); + $item['allow_disable'] = !empty($ff); + if (true === $isEnabled) { try { $item['next_run'] = makeDate($timer->getNextRunDate());