Ver código fonte

WAS日志清除服务

haozi 5 anos atrás
pai
commit
6650e8a29c
3 arquivos alterados com 61 adições e 5 exclusões
  1. 50 0
      app/Console/Commands/LogExpireDelete.php
  2. 3 4
      app/Console/Kernel.php
  3. 8 1
      runServes.sh

+ 50 - 0
app/Console/Commands/LogExpireDelete.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class LogExpireDelete extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'LogExpireDelete';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'LogExpireDelete';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->deleteLog();
+    }
+
+    public function deleteLog(){
+        //前一天
+        $date=Carbon::now()->subDays(150)->format('Y-m-d');
+        DB::table('logs')->where('created_at','like',$date.'%')->delete();
+    }
+}

+ 3 - 4
app/Console/Kernel.php

@@ -14,7 +14,7 @@ class Kernel extends ConsoleKernel
      * @var array
      */
     protected $commands = [
-        //
+        \App\Console\Commands\LogExpireDelete::class,
     ];
 
     /**
@@ -27,9 +27,8 @@ class Kernel extends ConsoleKernel
     {
         // $schedule->command('inspire')
         //          ->hourly();
-        $schedule->call(function (){
-            DB::insert('insert into logs (operation,type) values (?, ?)', ['App', 'aBc']);
-        })->everyMinute();
+        //$schedule->command('LogExpireDelete')->daily();
+        $schedule->command('LogExpireDelete')->everyMinute();
     }
 
     /**

+ 8 - 1
runServes.sh

@@ -10,4 +10,11 @@ nohup php artisan queue:work --tries=2 --delay=2 >/dev/null 2>&1 &
 nohup php artisan queue:work --tries=2 --delay=2 >/dev/null 2>&1 &
 nohup php artisan queue:work --tries=2 --delay=2 >/dev/null 2>&1 &
 
-nohup php artisan schedule:run >> /dev/null 2>&1
+
+
+
+isExistStr=$(cat /etc/crontab|grep '* * * * * /usr/bin/php /var/www_test/was/artisan schedule:run >> /dev/null 2>&1')
+if [ ! -n "$isExistStr" ]; then
+echo -e '\n* * * * * /usr/bin/php /var/www_test/was/artisan schedule:run >> /dev/null 2>&1' >> /etc/crontab
+fi
+systemctl cron restart