[Feature] Cleanup "open" payments once a day (#704)

This commit is contained in:
Dennis 2023-02-02 23:33:06 +01:00 committed by GitHub
commit a5e276d50e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 4 deletions

View file

@ -67,7 +67,7 @@ class ChargeCreditsCommand extends Command
$server->suspend();
//add user to notify list
if (! in_array($user, $this->usersToNotify)) {
if (!in_array($user, $this->usersToNotify)) {
array_push($this->usersToNotify, $user);
}
} catch (\Exception $exception) {
@ -85,7 +85,7 @@ class ChargeCreditsCommand extends Command
*/
public function notifyUsers()
{
if (! empty($this->usersToNotify)) {
if (!empty($this->usersToNotify)) {
/** @var User $user */
foreach ($this->usersToNotify as $user) {
$this->line("<fg=yellow>Notified user:</> <fg=blue>{$user->name}</>");

View file

@ -0,0 +1,42 @@
<?php
namespace App\Console\Commands;
use App\Models\Payment;
use Illuminate\Console\Command;
class CleanupPayments extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'payments:open:clear';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clears all payments from the database that have state "open"';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// delete all payments that have state "open" and are older than 1 hour
try {
Payment::where('status', 'open')->where('updated_at', '<', now()->subHour())->delete();
} catch (\Exception $e) {
$this->error('Could not delete payments: ' . $e->getMessage());
return 1;
}
$this->info('Successfully deleted all open payments');
return Command::SUCCESS;
}
}

View file

@ -18,10 +18,11 @@ class Kernel extends ConsoleKernel
{
$schedule->command('credits:charge')->hourly();
$schedule->command('cp:versioncheck:get')->daily();
$schedule->command('payments:open:clear')->daily();
//log cronjob activity
$schedule->call(function () {
Storage::disk('logs')->put('cron.log', 'Last activity from cronjobs - '.now());
Storage::disk('logs')->put('cron.log', 'Last activity from cronjobs - ' . now());
})->everyMinute();
}
@ -32,7 +33,7 @@ class Kernel extends ConsoleKernel
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}

View file

@ -13,6 +13,7 @@ class Payment extends Model
use HasFactory;
public $incrementing = false;
protected $primaryKey = 'id';
/**
* @var string[]