From 667487602ea94cf3fb4911c9088dccedbb1aac66 Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A." Date: Mon, 8 Jul 2024 15:16:15 +0300 Subject: [PATCH] Added Task runner status to Tasks page in WebUI. --- container/files/init-container.sh | 4 +-- container/files/job-runner.sh | 2 +- frontend/pages/tasks.vue | 24 +++++++++++++++ src/API/Tasks/Index.php | 1 + src/Commands/System/ReportCommand.php | 41 ++++++++----------------- src/Libs/helpers.php | 43 +++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 31 deletions(-) diff --git a/container/files/init-container.sh b/container/files/init-container.sh index 182f0bc9..c1fc3074 100755 --- a/container/files/init-container.sh +++ b/container/files/init-container.sh @@ -116,9 +116,9 @@ echo "[$(date +"%Y-%m-%dT%H:%M:%S%z")] Ensuring state table has correct indexes. /opt/bin/console system:apikey -q if [ 0 = "${DISABLE_CRON}" ] && [ 0 = "${WS_DISABLE_CRON}" ]; then - if [ -f "/tmp/job-runner.pid" ]; then + if [ -f "/tmp/ws-job-runner.pid" ]; then echo "[$(date +"%Y-%m-%dT%H:%M:%S%z")] Found pre-existing tasks scheduler pid file. Removing it." - rm -f "/tmp/job-runner.pid" + rm -f "/tmp/ws-job-runner.pid" fi echo "[$(date +"%Y-%m-%dT%H:%M:%S%z")] Starting tasks scheduler." diff --git a/container/files/job-runner.sh b/container/files/job-runner.sh index 117ede1f..74b174c1 100644 --- a/container/files/job-runner.sh +++ b/container/files/job-runner.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -PID="/tmp/job-runner.pid" +PID="/tmp/ws-job-runner.pid" # shellcheck disable=SC2064 trap 'rm -f "${PID}"; exit' EXIT SIGQUIT SIGINT SIGTERM ERR diff --git a/frontend/pages/tasks.vue b/frontend/pages/tasks.vue index 03546e57..b056117b 100644 --- a/frontend/pages/tasks.vue +++ b/frontend/pages/tasks.vue @@ -7,6 +7,13 @@
+

+ +

+
+ + {{ status.message }} +

+ + To restart the task runner, you have to restart the container. +

+
+
+
@@ -168,8 +189,10 @@ useHead({title: 'Tasks'}) const tasks = ref([]) const queued = ref([]) +const status = ref({}) const isLoading = ref(false) const show_page_tips = useStorage('show_page_tips', true) +const show_worker_status = useStorage('show_worker_status', false) const loadContent = async () => { isLoading.value = true @@ -179,6 +202,7 @@ const loadContent = async () => { const json = await response.json() tasks.value = json.tasks queued.value = json.queued + status.value = json.status } catch (e) { notification('error', 'Error', `Request error. ${e.message}`) } finally { diff --git a/src/API/Tasks/Index.php b/src/API/Tasks/Index.php index 844c5542..8ba491ec 100644 --- a/src/API/Tasks/Index.php +++ b/src/API/Tasks/Index.php @@ -33,6 +33,7 @@ final class Index $response = [ 'tasks' => [], 'queued' => $queuedTasks, + 'status' => isTaskWorkerRunning(), ]; foreach (TasksCommand::getTasks() as $task) { diff --git a/src/Commands/System/ReportCommand.php b/src/Commands/System/ReportCommand.php index 7f31e2ab..febc3460 100644 --- a/src/Commands/System/ReportCommand.php +++ b/src/Commands/System/ReportCommand.php @@ -13,7 +13,6 @@ use App\Libs\Entity\StateEntity; use App\Libs\Extends\ConsoleOutput; use App\Libs\Extends\Date; use App\Libs\Options; -use App\Libs\Stream; use Cron\CronExpression; use LimitIterator; use RuntimeException; @@ -90,7 +89,6 @@ final class ReportCommand extends Command $output->writeln(r('WatchState version: {answer}', ['answer' => getAppVersion()])); $output->writeln(r('PHP version: {answer}', ['answer' => PHP_VERSION])); $output->writeln(r('Timezone: {answer}', ['answer' => Config::get('tz', 'UTC')])); - $output->writeln(r('Running in container? {answer}', ['answer' => inContainer() ? 'Yes' : 'No'])); $output->writeln(r('Data path: {answer}', ['answer' => Config::get('path')])); $output->writeln(r('Temp path: {answer}', ['answer' => Config::get('tmpDir')])); $output->writeln( @@ -102,32 +100,19 @@ final class ReportCommand extends Command ]) ); - if (inContainer()) { - $output->writeln( - r('Is the tasks runner working? {answer}', [ - 'answer' => (function () { - $pidFile = '/tmp/job-runner.pid'; - if (!file_exists($pidFile)) { - return 'No PID file was found - Likely means job-runner failed to run.'; - } - - try { - $pid = trim((string)(new Stream($pidFile))); - } catch (Throwable $e) { - return $e->getMessage(); - } - - if (file_exists(r('/proc/{pid}/status', ['pid' => $pid]))) { - return 'Yes'; - } - - return r('No. Found PID ({pid}) in file, but it seems the process crashed.', [ - 'pid' => $pid - ]); - })(), - ]) - ); - } + $output->writeln( + r('Is the tasks runner working? {answer}', [ + 'answer' => (function () { + $info = isTaskWorkerRunning(true); + return r("{status} '{container}' - {message}", [ + 'status' => $info['status'] ? 'Yes' : 'No', + 'message' => $info['message'], + 'container' => inContainer() ? 'Container' : 'Unknown', + ]); + })(), + ]) + ); + $output->writeln(r('Running in container? {answer}', ['answer' => inContainer() ? 'Yes' : 'No'])); $output->writeln(r('Report generated at: {answer}', ['answer' => gmdate(Date::ATOM)])); diff --git a/src/Libs/helpers.php b/src/Libs/helpers.php index 1b27923b..e80d92ad 100644 --- a/src/Libs/helpers.php +++ b/src/Libs/helpers.php @@ -1592,3 +1592,46 @@ if (!function_exists('loadEnvFile')) { } } } + + +if (!function_exists('isTaskWorkerRunning')) { + /** + * Check if the task worker is running. This function is only available when running in a container. + * + * @param bool $ignoreContainer (Optional) Whether to ignore the container check. + * + * @return array{ status: bool, message: string } + */ + function isTaskWorkerRunning(bool $ignoreContainer = false): array + { + if (false === $ignoreContainer && !inContainer()) { + return [ + 'status' => true, + 'message' => 'We can only track the task worker status when running in a container.' + ]; + } + + $pidFile = '/tmp/ws-job-runner.pid'; + + if (!file_exists($pidFile)) { + return ['status' => false, 'message' => 'No PID file was found - Likely means task worker failed to run.']; + } + + try { + $pid = trim((string)(new Stream($pidFile))); + } catch (Throwable $e) { + return ['status' => false, 'message' => $e->getMessage()]; + } + + if (file_exists(r('/proc/{pid}/status', ['pid' => $pid]))) { + return ['status' => true, 'message' => 'Task worker is running.']; + } + + return [ + 'status' => false, + 'message' => r("Found PID '{pid}' in file, but it seems the process is not active.", [ + 'pid' => $pid + ]) + ]; + } +}