瀏覽代碼

Update console kernel, add db session garbage collector that runs twice daily

Daniel Supernault 3 年之前
父節點
當前提交
03b0a62a22
共有 2 個文件被更改,包括 57 次插入0 次删除
  1. 56 0
      app/Console/Commands/DatabaseSessionGarbageCollector.php
  2. 1 0
      app/Console/Kernel.php

+ 56 - 0
app/Console/Commands/DatabaseSessionGarbageCollector.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class DatabaseSessionGarbageCollector extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'gc:sessions';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Database sessions garbage collector';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+    	if(config('session.driver') !== 'database') {
+        	return Command::SUCCESS;
+    	}
+
+    	DB::transaction(function() {
+    		DB::table('sessions')->whereNull('user_id')->delete();
+    	});
+
+    	DB::transaction(function() {
+    		$ts = now()->subMonths(3)->timestamp;
+    		DB::table('sessions')->where('last_activity', '<', $ts)->delete();
+    	});
+
+        return Command::SUCCESS;
+    }
+}

+ 1 - 0
app/Console/Kernel.php

@@ -31,6 +31,7 @@ class Kernel extends ConsoleKernel
         $schedule->command('story:gc')->everyFiveMinutes();
         $schedule->command('gc:failedjobs')->dailyAt(3);
         $schedule->command('gc:passwordreset')->dailyAt('09:41');
+        $schedule->command('gc:sessions')->twiceDaily(13, 23);
     }
 
     /**