Explorar el Código

feat: ✨ Add open payments cleanup to schedule

IceToast hace 2 años
padre
commit
62358aec4e

+ 2 - 2
app/Console/Commands/ChargeCreditsCommand.php

@@ -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}</>");

+ 42 - 0
app/Console/Commands/CleanupPayments.php

@@ -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: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;
+    }
+}

+ 3 - 2
app/Console/Kernel.php

@@ -18,10 +18,11 @@ class Kernel extends ConsoleKernel
     {
         $schedule->command('credits:charge')->hourly();
         $schedule->command('cp:versioncheck:get')->daily();
+        $schedule->command('payments: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');
     }