Skip to content

Commit

Permalink
feat: add cause to queued command output
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 11, 2023
1 parent 91990d5 commit ca816ae
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ export default class TaskOutputModal<CustomAttrs extends TaskOutputModalAttrs =
return (
<div className="Modal-body">
<div className="TaskOutputModal-data">
<div className="Form-group">
<label>{app.translator.trans('flarum-package-manager.admin.sections.queue.output_modal.guessed_cause')}</label>
<div className="FormControl TaskOutputModal-data-guessed-cause">
{(this.attrs.task.guessedCause() &&
app.translator.trans('flarum-package-manager.admin.exceptions.guessed_cause.' + this.attrs.task.guessedCause())) ||
app.translator.trans('flarum-package-manager.admin.sections.queue.output_modal.cause_unknown')}
</div>
</div>

<div className="Form-group">
<label>{app.translator.trans('flarum-package-manager.admin.sections.queue.output_modal.command')}</label>
<div className="FormControl TaskOutputModal-data-command">
<code>$ composer {this.attrs.task.command()}</code>
</div>
</div>

<div className="Form-group">
<label>{app.translator.trans('flarum-package-manager.admin.sections.queue.output_modal.output')}</label>
<div className="FormControl TaskOutputModal-data-output">
Expand Down
6 changes: 4 additions & 2 deletions extensions/package-manager/js/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ app.initializers.add('flarum-package-manager', (app) => {

app.packageManager = new PackageManagerState();

if (app.data['flarum-package-manager.using_sync_queue']) {
app.data.settings['flarum-package-manager.queue_jobs'] = '0';
}

app.extensionData
.for('flarum-package-manager')
.registerSetting({
Expand All @@ -33,10 +37,8 @@ app.initializers.add('flarum-package-manager', (app) => {
})
)
),
default: false,
type: 'boolean',
disabled: app.data['flarum-package-manager.using_sync_queue'],
// @todo async to sync while setting is enabled
})
.registerSetting({
setting: 'flarum-package-manager.task_retention_days',
Expand Down
4 changes: 4 additions & 0 deletions extensions/package-manager/js/src/admin/models/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default class Task extends Model {
return Model.attribute<string>('output').call(this);
}

guessedCause() {
return Model.attribute<string>('guessedCause').call(this);
}

createdAt() {
return Model.attribute('createdAt', Model.transformDate).call(this);
}
Expand Down
2 changes: 2 additions & 0 deletions extensions/package-manager/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ flarum-package-manager:
update_minor: Minor update
why_not: Analyze why a package cannot be updated
output_modal:
cause_unknown: Unknown
command: Composer Command
guessed_cause: Cause
output: Output
refresh: Refresh tasks list
statuses:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Flarum\Database\Migration;

return Migration::addColumns('package_manager_tasks', [
'guessed_cause' => ['type' => 'string', 'length' => 255, 'nullable' => true, 'after' => 'output'],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function getDefaultAttributes($model)
'command' => $model->command,
'package' => $model->package,
'output' => $model->output,
'guessedCause' => $model->guessed_cause,
'createdAt' => $model->created_at,
'startedAt' => $model->started_at,
'finishedAt' => $model->finished_at,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function handle(UpdateExtension $command)
]);
}

// @todo: soft update and hard update instead of directly hard updating?
$output = $this->composer->run(
new StringInput("require $extension->name:*"),
$command->task ?? null
Expand Down
7 changes: 6 additions & 1 deletion extensions/package-manager/src/Job/ComposerCommandJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Bus\Dispatcher;
use Flarum\PackageManager\Command\AbstractActionCommand;
use Flarum\PackageManager\Composer\ComposerAdapter;
use Flarum\PackageManager\Exception\ComposerCommandFailedException;
use Flarum\Queue\AbstractJob;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Queue\Middleware\WithoutOverlapping;
Expand Down Expand Up @@ -56,12 +57,16 @@ public function abort(Throwable $exception)
$this->command->task->output = $exception->getMessage();
}

if ($exception instanceof ComposerCommandFailedException) {
$this->command->task->guessed_cause = $exception->guessCause();
}

$this->command->task->end(false);
}

public function failed(Throwable $exception): void
{
$this->command->task->end(false);
$this->abort($exception);
}

public function middleware(): array
Expand Down
3 changes: 2 additions & 1 deletion extensions/package-manager/src/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @property string $command
* @property string $package
* @property string $output
* @property string|null $guessed_cause
* @property Carbon $created_at
* @property Carbon|null $started_at
* @property Carbon|null $finished_at
Expand Down Expand Up @@ -50,7 +51,7 @@ class Task extends AbstractModel

protected $table = 'package_manager_tasks';

protected $fillable = ['command', 'output'];
protected $guarded = ['id'];

public $timestamps = true;

Expand Down

0 comments on commit ca816ae

Please sign in to comment.