添加定期删除message的任务

This commit is contained in:
Easy 2022-03-22 14:31:54 +08:00
parent 967e1d4bcb
commit a570beaf3e
6 changed files with 91 additions and 1 deletions

View File

@ -66,3 +66,6 @@ WECHAT_APPSECRET=
MAX_PUSH_EVERY_USER_PER_MINUTE=60
MAX_PUSH_KEY_PER_TIME=100
# 清理N天前的推送消息0为不清理
MAX_PUSH_EXISTS_DAYS=0

View File

@ -0,0 +1,50 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\PushDeerMessage;
use Carbon\Carbon;
class CleanOldPush extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:cleanpush';
/**
* The console command description.
*
* @var string
*/
protected $description = '清除过时的推送内容';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$items_count = 10000; // 每次最多删除条数
$days = intval(env('MAX_PUSH_EXISTS_DAYS'));
if ($days > 0) {
// echo "进入清理模式";
PushDeerMessage::whereDate('created_at', '<', Carbon::now()->subDays($days))->take($items_count)->delete();
}
return 0;
}
}

View File

@ -15,7 +15,8 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
// $schedule->command('app:cleanpush')->everyMinute();
$schedule->command('app:cleanpush')->hourly();
}
/**

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddIndexToMessages extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('push_deer_messages', function (Blueprint $table) {
$table->index(['created_at'], 'created');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('push_deer_messages', function (Blueprint $table) {
$table->dropIndex('created');
});
}
}

View File

@ -20,5 +20,8 @@ RUN chmod +x /data/gorush
ADD supervisord-ios.conf /opt/docker/etc/supervisor.d/push-ios.conf
ADD supervisord-clip.conf /opt/docker/etc/supervisor.d/push-clip.conf
ADD larave-cron /etc/cron.d
RUN chmod +x /etc/cron.d/larave-cron
EXPOSE 80

1
docker/web/larave-cron Normal file
View File

@ -0,0 +1 @@
* * * * * cd /app/api && php artisan schedule:run >> /dev/null 2>&1