Skip to content

Commit

Permalink
feat: queue improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 9, 2023
1 parent 82b7fa1 commit e48a1ac
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 42 deletions.
3 changes: 2 additions & 1 deletion extensions/package-manager/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface IConfigureComposer extends ComponentAttrs {

export default class ConfigureComposer<CustomAttrs extends IConfigureComposer = IConfigureComposer> extends Component<CustomAttrs> {
protected settings: Record<string, Stream<any>> = {};
protected initialSettings: Record<string, any> | null = null;
protected loading: boolean = false;

oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
Expand All @@ -39,7 +40,7 @@ export default class ConfigureComposer<CustomAttrs extends IConfigureComposer =
})}

<div className="Form-group">
<Button className="Button Button--primary" loading={this.loading} onclick={this.submit.bind(this)}>
<Button className="Button Button--primary" loading={this.loading} onclick={this.submit.bind(this)} disabled={!this.isDirty()}>
{app.translator.trans('core.admin.settings.submit_button')}
</Button>
</div>
Expand Down Expand Up @@ -74,10 +75,18 @@ export default class ConfigureComposer<CustomAttrs extends IConfigureComposer =
Object.keys(data).forEach((key) => {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class SettingsPage extends ExtensionPage {
items.add('queue', <QueueSection />, 5);
}

items.setPriority('permissions', 0);
items.remove('permissions');

return items;
}
Expand Down
6 changes: 6 additions & 0 deletions extensions/package-manager/js/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion extensions/package-manager/js/src/admin/states/QueueState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task[]> {
this.tasks = null;
params = {
page: {
Expand All @@ -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;
});
}
Expand Down Expand Up @@ -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);
}
}
26 changes: 1 addition & 25 deletions extensions/package-manager/js/src/admin/utils/jumpToQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion extensions/package-manager/less/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@import "admin/QueueSection";
@import "admin/ControlSection";

.PackageManager-controlSection, .PackageManager-queueSection {
.PackageManager-controlSection {
> .container {
padding-bottom: 0;
}
Expand Down
4 changes: 4 additions & 0 deletions extensions/package-manager/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ flarum-package-manager:
queue_jobs_help: >
You can read about a <a href='{basic_impl_link}'>basic queue</a> implementation or a <a href='{adv_impl_link}'>more advanced</a> one.
Make sure the PHP version used for the queue is {php_version}. Make sure <a href='{folder_perms_link}'>folder permissions</a> 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!
Expand Down
16 changes: 16 additions & 0 deletions extensions/package-manager/src/Job/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
12 changes: 0 additions & 12 deletions extensions/package-manager/src/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e48a1ac

Please sign in to comment.