From e48a1ac9dc7604fb1d0be5042432275783ff0949 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 9 Dec 2023 11:07:07 +0100 Subject: [PATCH] feat: queue improvements --- extensions/package-manager/extend.php | 3 ++- .../admin/components/ConfigureComposer.tsx | 11 +++++++- .../js/src/admin/components/SettingsPage.tsx | 2 +- .../package-manager/js/src/admin/index.tsx | 6 +++++ .../js/src/admin/states/QueueState.ts | 23 +++++++++++++++- .../js/src/admin/utils/jumpToQueue.ts | 26 +------------------ extensions/package-manager/less/admin.less | 2 +- extensions/package-manager/locale/en.yml | 4 +++ .../package-manager/src/Job/Dispatcher.php | 16 ++++++++++++ extensions/package-manager/src/Task/Task.php | 12 --------- 10 files changed, 63 insertions(+), 42 deletions(-) diff --git a/extensions/package-manager/extend.php b/extensions/package-manager/extend.php index c506da8fd7..114034d28e 100755 --- a/extensions/package-manager/extend.php +++ b/extensions/package-manager/extend.php @@ -49,7 +49,8 @@ ->default(Settings\LastUpdateCheck::key(), json_encode(Settings\LastUpdateCheck::default())) ->default(Settings\LastUpdateRun::key(), json_encode(Settings\LastUpdateRun::default())) ->default('flarum-package-manager.queue_jobs', false) - ->default('flarum-package-manager.minimum_stability', 'stable'), + ->default('flarum-package-manager.minimum_stability', 'stable') + ->default('flarum-package-manager.task_retention_days', 30), (new Extend\ServiceProvider) ->register(PackageManagerServiceProvider::class), diff --git a/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx b/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx index 30d180fd93..dbf9e64347 100644 --- a/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx +++ b/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx @@ -13,6 +13,7 @@ export interface IConfigureComposer extends ComponentAttrs { export default class ConfigureComposer extends Component { protected settings: Record> = {}; + protected initialSettings: Record | null = null; protected loading: boolean = false; oninit(vnode: Mithril.Vnode) { @@ -39,7 +40,7 @@ export default class ConfigureComposer - @@ -74,10 +75,18 @@ export default class ConfigureComposer { this.settings[key] = Stream(data[key]); }); + + if (this.initialSettings === null) { + this.initialSettings = data; + } }) .finally(() => { this.loading = false; m.redraw(); }); } + + isDirty() { + return JSON.stringify(this.initialSettings) !== JSON.stringify(this.settings); + } } diff --git a/extensions/package-manager/js/src/admin/components/SettingsPage.tsx b/extensions/package-manager/js/src/admin/components/SettingsPage.tsx index 954dd31edd..6be39cb504 100644 --- a/extensions/package-manager/js/src/admin/components/SettingsPage.tsx +++ b/extensions/package-manager/js/src/admin/components/SettingsPage.tsx @@ -52,7 +52,7 @@ export default class SettingsPage extends ExtensionPage { items.add('queue', , 5); } - items.setPriority('permissions', 0); + items.remove('permissions'); return items; } diff --git a/extensions/package-manager/js/src/admin/index.tsx b/extensions/package-manager/js/src/admin/index.tsx index 45d8b0c1fe..f7b4aad557 100755 --- a/extensions/package-manager/js/src/admin/index.tsx +++ b/extensions/package-manager/js/src/admin/index.tsx @@ -37,6 +37,12 @@ app.initializers.add('flarum-package-manager', (app) => { type: 'boolean', disabled: app.data['flarum-package-manager.using_sync_queue'], }) + .registerSetting({ + setting: 'flarum-package-manager.task_retention_days', + label: app.translator.trans('flarum-package-manager.admin.settings.task_retention_days'), + help: app.translator.trans('flarum-package-manager.admin.settings.task_retention_days_help'), + type: 'number', + }) .registerPage(SettingsPage); extend(ExtensionPage.prototype, 'topItems', function (items) { diff --git a/extensions/package-manager/js/src/admin/states/QueueState.ts b/extensions/package-manager/js/src/admin/states/QueueState.ts index 492e415a7a..80598bee7c 100644 --- a/extensions/package-manager/js/src/admin/states/QueueState.ts +++ b/extensions/package-manager/js/src/admin/states/QueueState.ts @@ -3,12 +3,13 @@ import Task from '../models/Task'; import { ApiQueryParamsPlural } from 'flarum/common/Store'; export default class QueueState { + private polling: any = null; private tasks: Task[] | null = null; private limit = 20; private offset = 0; private total = 0; - load(params?: ApiQueryParamsPlural) { + load(params?: ApiQueryParamsPlural, actionTaken = false): Promise { this.tasks = null; params = { page: { @@ -25,6 +26,16 @@ export default class QueueState { m.redraw(); + // Check if there is a pending or running task + const task = data?.find((task) => task.status() === 'pending' || task.status() === 'running'); + + if (task) { + this.pollQueue(actionTaken); + } else if (actionTaken) { + // Refresh the page + window.location.reload(); + } + return data; }); } @@ -62,4 +73,14 @@ export default class QueueState { this.load(); } } + + pollQueue(actionTaken = false): void { + if (this.polling) { + clearTimeout(this.polling); + } + + this.polling = setTimeout(() => { + this.load({}, actionTaken); + }, 6000); + } } diff --git a/extensions/package-manager/js/src/admin/utils/jumpToQueue.ts b/extensions/package-manager/js/src/admin/utils/jumpToQueue.ts index d8e9ea3418..0045f59ad3 100644 --- a/extensions/package-manager/js/src/admin/utils/jumpToQueue.ts +++ b/extensions/package-manager/js/src/admin/utils/jumpToQueue.ts @@ -8,33 +8,9 @@ export default function jumpToQueue(): void { m.route.set(app.route('extension', { id: 'flarum-package-manager' })); - app.packageManager.queue.load(); + app.packageManager.queue.load({}, true); setTimeout(() => { document.getElementById('PackageManager-queueSection')?.scrollIntoView({ block: 'nearest' }); }, 200); - - pollQueue(); -} - -let queuePolling: any = null; - -export function pollQueue(): void { - if (queuePolling) { - clearInterval(queuePolling); - } - - queuePolling = setTimeout(() => { - app.packageManager.queue.load(); - - // Check if there is a pending or running task - const task = app.packageManager.queue.getItems()?.find((task) => task.status() === 'pending' || task.status() === 'running'); - - if (task) { - pollQueue(); - } else { - // Refresh the page - window.location.reload(); - } - }, 6000); } diff --git a/extensions/package-manager/less/admin.less b/extensions/package-manager/less/admin.less index 3b52522153..ec55d3a66f 100755 --- a/extensions/package-manager/less/admin.less +++ b/extensions/package-manager/less/admin.less @@ -3,7 +3,7 @@ @import "admin/QueueSection"; @import "admin/ControlSection"; -.PackageManager-controlSection, .PackageManager-queueSection { +.PackageManager-controlSection { > .container { padding-bottom: 0; } diff --git a/extensions/package-manager/locale/en.yml b/extensions/package-manager/locale/en.yml index 86e69182cc..295d5775a3 100755 --- a/extensions/package-manager/locale/en.yml +++ b/extensions/package-manager/locale/en.yml @@ -92,6 +92,10 @@ flarum-package-manager: queue_jobs_help: > You can read about a basic queue implementation or a more advanced one. Make sure the PHP version used for the queue is {php_version}. Make sure folder permissions are correctly configured. + task_retention_days: Task retention days + task_retention_days_help: > + The number of days to keep completed tasks in the database. Tasks older than this will be deleted. + Set to 0 to keep all tasks. updater: up_to_date: Everything is up to date! diff --git a/extensions/package-manager/src/Job/Dispatcher.php b/extensions/package-manager/src/Job/Dispatcher.php index 20026dc078..3f1722d1f8 100644 --- a/extensions/package-manager/src/Job/Dispatcher.php +++ b/extensions/package-manager/src/Job/Dispatcher.php @@ -9,6 +9,7 @@ namespace Flarum\PackageManager\Job; +use Carbon\Carbon; use Flarum\Bus\Dispatcher as Bus; use Flarum\PackageManager\Command\AbstractActionCommand; use Flarum\PackageManager\Task\Task; @@ -79,6 +80,21 @@ public function dispatch(AbstractActionCommand $command): DispatcherResponse $data = $this->bus->dispatch($command); } + $this->clearOldTasks(); + return new DispatcherResponse($queueJobs, $data ?? null); } + + protected function clearOldTasks(): void + { + $days = $this->settings->get('flarum-package-manager.task_retention_days'); + + if ($days === null || ((int) $days) === 0) { + return; + } + + Task::query() + ->where('created_at', '<', Carbon::now()->subDays($days)) + ->delete(); + } } diff --git a/extensions/package-manager/src/Task/Task.php b/extensions/package-manager/src/Task/Task.php index 479d780de2..9ce2f16d70 100644 --- a/extensions/package-manager/src/Task/Task.php +++ b/extensions/package-manager/src/Task/Task.php @@ -60,18 +60,6 @@ class Task extends AbstractModel 'finished_at' => 'datetime', ]; - public static function boot() - { - parent::boot(); - - static::created(function (self $task) { - // auto clear tasks older than 2 weeks. - static::query() - ->where('created_at', '<', Carbon::now()->subWeeks(2)) - ->delete(); - }); - } - public static function build(string $operation, ?string $package): self { $task = new static;