Skip to content

Commit

Permalink
Allow users to customize the command's timeout (#70)
Browse files Browse the repository at this point in the history
* Add a way to configure the backup job's timeout

* Add a way to configure the backup job's timeout

* Add a convenience method to disable timeout altogether

* Rename timeout parameter
  • Loading branch information
Voltra authored Jul 7, 2024
1 parent c36a7df commit 92533cb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,60 @@ class AdminPanelProvider extends PanelProvider
}
```

## Customising the timeout

You can customise the timeout for the backup job by following the steps below:

```php
<?php

namespace App\Providers\Filament;

use Filament\Panel;
use Filament\PanelProvider;
use ShuvroRoy\FilamentSpatieLaravelBackup\FilamentSpatieLaravelBackupPlugin;

class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(
FilamentSpatieLaravelBackupPlugin::make()
->timeout(120) // default value is max_execution_time from php.ini, or 30s if it wasn't defined
);
}
}
```

For more details refer to the [set_time_limit](https://www.php.net/manual/en/function.set-time-limit.php) function.

You can also disable the timeout altogether to let the job run as long as needed:

```php
<?php

namespace App\Providers\Filament;

use Filament\Panel;
use Filament\PanelProvider;
use ShuvroRoy\FilamentSpatieLaravelBackup\FilamentSpatieLaravelBackupPlugin;

class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(
FilamentSpatieLaravelBackupPlugin::make()
->noTimeout()
);
}
}
```

## Upgrading

Please see [UPGRADE](UPGRADE.md) for details on how to upgrade 1.X to 2.0.
Expand Down
29 changes: 29 additions & 0 deletions src/FilamentSpatieLaravelBackupPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class FilamentSpatieLaravelBackupPlugin implements Plugin

protected bool $hasStatusListRecordsTable = true;

protected ?int $timeout = null;

public function register(Panel $panel): void
{
$panel->pages([$this->getPage()]);
Expand Down Expand Up @@ -72,6 +74,33 @@ public function getPolingInterval(): string
return $this->interval;
}

/**
* Set the timeout (in seconds) used for the backup job. If set to 0, the job will never timeout.
*
* @see https://www.php.net/manual/en/function.set-time-limit.php
*/
public function timeout(int $seconds): static
{
$this->timeout = $seconds;

return $this;
}

/**
* Make it so that the backup job will never timeout.
*
* @see https://www.php.net/manual/en/function.set-time-limit.php
*/
public function noTimeout(): static
{
return $this->timeout(0);
}

public function getTimeout(): ?int
{
return $this->timeout;
}

public function statusListRecordsTable(bool $condition = true): static
{
$this->hasStatusListRecordsTable = $condition;
Expand Down
11 changes: 7 additions & 4 deletions src/Jobs/CreateBackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class CreateBackupJob implements ShouldQueue
use Queueable;

public function __construct(
protected readonly Option $option = Option::ALL
){}
protected readonly Option $option = Option::ALL,
protected readonly ?int $timeout = null,
) {
}

public function handle(): void
{
Expand All @@ -27,9 +29,10 @@ public function handle(): void
'--only-files' => $this->option === Option::ONLY_FILES,
'--filename' => match ($this->option) {
Option::ALL => null,
default => str_replace('_', '-', $this->option->value).
'-'.date('Y-m-d-H-i-s').'.zip'
default => str_replace('_', '-', $this->option->value) .
'-' . date('Y-m-d-H-i-s') . '.zip'
},
'--timeout' => $this->timeout,
]);
}
}
2 changes: 1 addition & 1 deletion src/Pages/Backups.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function create(string $option = ''): void
/** @var FilamentSpatieLaravelBackupPlugin $plugin */
$plugin = filament()->getPlugin('filament-spatie-backup');

CreateBackupJob::dispatch(Option::from($option))
CreateBackupJob::dispatch(Option::from($option), $plugin->getTimeout())
->onQueue($plugin->getQueue())
->afterResponse();

Expand Down

0 comments on commit 92533cb

Please sign in to comment.