瀏覽代碼

Update scheduler, add StoryGC command

Daniel Supernault 5 年之前
父節點
當前提交
01dd5db7f5
共有 2 個文件被更改,包括 65 次插入0 次删除
  1. 64 0
      app/Console/Commands/StoryGC.php
  2. 1 0
      app/Console/Kernel.php

+ 64 - 0
app/Console/Commands/StoryGC.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\{
+    DB,
+    Storage
+};
+use App\{
+    Story,
+    StoryView
+};
+
+class StoryGC extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'story:gc';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Clear expired Stories';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $stories = Story::where('expires_at', '<', now())->take(50)->get();
+
+        if($stories->count() == 0) {
+            exit;
+        }
+
+        foreach($stories as $story) {
+            if(Storage::exists($story->path) == true) {
+                Storage::delete($story->path);
+            }
+            DB::transaction(function() use($story) {
+                StoryView::whereStoryId($story->id)->delete();
+                $story->delete();
+            });
+        }
+    }
+}

+ 1 - 0
app/Console/Kernel.php

@@ -30,6 +30,7 @@ class Kernel extends ConsoleKernel
         $schedule->command('media:gc')
                  ->hourly();
         $schedule->command('horizon:snapshot')->everyFiveMinutes();
+        $schedule->command('story:gc')->everyFiveMinutes();
     }
 
     /**