Skip to content

Commit

Permalink
更新:优化定时任务时间
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Dec 23, 2022
1 parent a941bfe commit 44f7960
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 39 deletions.
50 changes: 37 additions & 13 deletions app/Plugins/Core/src/Crontab/CleanDatabase.php
@@ -1,15 +1,23 @@
<?php

declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/super-forum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Plugins\Core\src\Crontab;

use App\Plugins\Core\src\Models\PayOrder;
use App\Plugins\Topic\src\Models\Topic;
use App\Plugins\Topic\src\Models\TopicUpdated;
use App\Plugins\User\src\Models\UsersNotice;
use Hyperf\Crontab\Annotation\Crontab;
use Swoole\Coroutine\System;

/**
* @Crontab(name="CleanDatabase", rule="0 * * * *", callback="execute", enable={CleanDatabase::class, "isEnable"}, memo="数据库垃圾清理")
*/
#[Crontab(name: 'CleanDatabase', rule: '0 *\/12 * * *', callback: 'execute', enable: [CleanDatabase::class, 'isEnable'], memo: '数据库垃圾清理')]
class CleanDatabase
{
public function execute()
Expand All @@ -22,19 +30,21 @@ public function execute()
$this->order();
// 清理admin_logger日志
$this->admin_logger();
// 清理帖子更新记录
$this->topic_updated();
}

public function isEnable(): bool
{
return true;
}

// 清理已删除的文章
private function topic()
{
Topic::query()->where('status', 'delete')->delete();
}

// 清理已读通知
private function notice()
{
Expand All @@ -46,8 +56,8 @@ private function order()
{
$data = [];
foreach (PayOrder::query()->where('status', '待支付')
->orWhere('status', '订单取消')
->orWhere('status', '交易关闭')->get() as $value) {
->orWhere('status', '订单取消')
->orWhere('status', '交易关闭')->get() as $value) {
if (time() - strtotime($value->created_at) > 86400) {
$data[] = $value->id;
}
Expand All @@ -59,14 +69,28 @@ private function order()
}

/**
* 清理admin_logger过期日志
* @return void
* 清理admin_logger过期日志.
*/
private function admin_logger(): void
{
foreach (scandir(BASE_PATH."/runtime/logs/admin_logger_database") as $name) {
if (is_dir(BASE_PATH."/runtime/logs/admin_logger_database/".$name) && $name!==(string)date('YmW') && $name!=='.' && $name!=='..') {
System::exec('rm -rf '.BASE_PATH."/runtime/logs/admin_logger_database/".$name);
foreach (scandir(BASE_PATH . '/runtime/logs/admin_logger_database') as $name) {
if (is_dir(BASE_PATH . '/runtime/logs/admin_logger_database/' . $name) && $name !== (string) date('YmW') && $name !== '.' && $name !== '..') {
System::exec('rm -rf ' . BASE_PATH . '/runtime/logs/admin_logger_database/' . $name);
}
}
}

/**
* 清理帖子更新记录.
*/
private function topic_updated(): void
{
foreach (Topic::query()->get('id') as $topic) {
if (TopicUpdated::query()->where('topic_id', $topic->id)->count() > 10) {
$all = TopicUpdated::query()->where('topic_id', $topic->id)->skip(10)->take(100)->get();
foreach ($all as $id) {
TopicUpdated::query()->where('id', $id->id)->delete();
}
}
}
}
Expand Down
40 changes: 23 additions & 17 deletions app/Plugins/Core/src/Crontab/CleanUsersPm.php
@@ -1,49 +1,55 @@
<?php

declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/super-forum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Plugins\Core\src\Crontab;

use App\Model\AdminOption;
use App\Plugins\Topic\src\Models\Topic;
use App\Plugins\User\src\Models\UsersNotice;
use App\Plugins\User\src\Models\UsersPm;
use Hyperf\Crontab\Annotation\Crontab;

/**
* 用户私信清理
* @Crontab(name="CleanUsersPm", rule="0 * * * *", callback="execute", enable={CleanUsersPm::class, "isEnable"}, memo="用户私信清理")
* 用户私信清理.
* @Crontab(name="CleanUsersPm", rule="0 *\/12 * * *", callback="execute", enable={CleanUsersPm::class, "isEnable"}, memo="用户私信清理")
*/
class CleanUsersPm
{
public function execute()
public function execute(): void
{
// 消息保留时间,单位:秒
$reserve = (int)get_options('pm_msg_reserve', 7)*24*60*60;
$reserve = (int) get_options('pm_msg_reserve', 7) * 24 * 60 * 60;

foreach (UsersPm::query()->get() as $pm) {
if (time()-strtotime($pm->created_at)>=$reserve) {
if (time() - strtotime($pm->created_at) >= $reserve) {
UsersPm::query()->where('id', $pm->id)->delete();
}
}
}

public function isEnable(): bool
{
return !((int)$this->get_options('pm_msg_reserve', 7) === 0);
return ! ((int) $this->get_options('pm_msg_reserve', 7) === 0);
}
private function core_default($string=null, $default=null)

private function core_default($string = null, $default = null)
{
if ($string) {
return $string;
}
return $default;
}
private function get_options($name, $default="")

private function get_options($name, $default = '')
{
if (!cache()->has('admin.options.'.$name)) {
cache()->set("admin.options.".$name, @AdminOption::query()->where("name", $name)->first()->value);
if (! cache()->has('admin.options.' . $name)) {
cache()->set('admin.options.' . $name, @AdminOption::query()->where('name', $name)->first()->value);
}
return $this->core_default(cache()->get("admin.options.".$name), $default);
return $this->core_default(cache()->get('admin.options.' . $name), $default);
}
}
16 changes: 7 additions & 9 deletions app/Plugins/Topic/src/Controllers/ApiController.php
Expand Up @@ -313,15 +313,13 @@ public function get_user_ip()
$updateds = request()->input('updateds');
$data = [];
foreach ($updateds as $updated_id) {
Timer::tick(1000, function () use ($updated_id, $data) {
$updated = TopicUpdated::query()->where(['id' => $updated_id])->first();
if ($updated->user_ip) {
$data[] = [
'updated_id' => $updated->id,
'text' => 'IP归属地:' . core_default(get_client_ip_data($updated->user_ip)['pro'], '未知'),
];
}
});
$updated = TopicUpdated::query()->where(['id' => $updated_id])->first();
if ($updated->user_ip) {
$data[] = [
'updated_id' => $updated->id,
'text' => 'IP归属地:' . core_default(get_client_ip_data($updated->user_ip)['pro'], '未知'),
];
}
}
return Json_Api(200, true, $data);
}
Expand Down

0 comments on commit 44f7960

Please sign in to comment.