Skip to content

Commit 44f7960

Browse files
committedDec 23, 2022
更新:优化定时任务时间
·
v2.7.7v2.1.7
1 parent a941bfe commit 44f7960

File tree

3 files changed

+67
-39
lines changed

3 files changed

+67
-39
lines changed
 

‎app/Plugins/Core/src/Crontab/CleanDatabase.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
311
namespace App\Plugins\Core\src\Crontab;
412

513
use App\Plugins\Core\src\Models\PayOrder;
614
use App\Plugins\Topic\src\Models\Topic;
15+
use App\Plugins\Topic\src\Models\TopicUpdated;
716
use App\Plugins\User\src\Models\UsersNotice;
817
use Hyperf\Crontab\Annotation\Crontab;
18+
use Swoole\Coroutine\System;
919

10-
/**
11-
* @Crontab(name="CleanDatabase", rule="0 * * * *", callback="execute", enable={CleanDatabase::class, "isEnable"}, memo="数据库垃圾清理")
12-
*/
20+
#[Crontab(name: 'CleanDatabase', rule: '0 *\/12 * * *', callback: 'execute', enable: [CleanDatabase::class, 'isEnable'], memo: '数据库垃圾清理')]
1321
class CleanDatabase
1422
{
1523
public function execute()
@@ -22,19 +30,21 @@ public function execute()
2230
$this->order();
2331
// 清理admin_logger日志
2432
$this->admin_logger();
33+
// 清理帖子更新记录
34+
$this->topic_updated();
2535
}
26-
36+
2737
public function isEnable(): bool
2838
{
2939
return true;
3040
}
31-
41+
3242
// 清理已删除的文章
3343
private function topic()
3444
{
3545
Topic::query()->where('status', 'delete')->delete();
3646
}
37-
47+
3848
// 清理已读通知
3949
private function notice()
4050
{
@@ -46,8 +56,8 @@ private function order()
4656
{
4757
$data = [];
4858
foreach (PayOrder::query()->where('status', '待支付')
49-
->orWhere('status', '订单取消')
50-
->orWhere('status', '交易关闭')->get() as $value) {
59+
->orWhere('status', '订单取消')
60+
->orWhere('status', '交易关闭')->get() as $value) {
5161
if (time() - strtotime($value->created_at) > 86400) {
5262
$data[] = $value->id;
5363
}
@@ -59,14 +69,28 @@ private function order()
5969
}
6070

6171
/**
62-
* 清理admin_logger过期日志
63-
* @return void
72+
* 清理admin_logger过期日志.
6473
*/
6574
private function admin_logger(): void
6675
{
67-
foreach (scandir(BASE_PATH."/runtime/logs/admin_logger_database") as $name) {
68-
if (is_dir(BASE_PATH."/runtime/logs/admin_logger_database/".$name) && $name!==(string)date('YmW') && $name!=='.' && $name!=='..') {
69-
System::exec('rm -rf '.BASE_PATH."/runtime/logs/admin_logger_database/".$name);
76+
foreach (scandir(BASE_PATH . '/runtime/logs/admin_logger_database') as $name) {
77+
if (is_dir(BASE_PATH . '/runtime/logs/admin_logger_database/' . $name) && $name !== (string) date('YmW') && $name !== '.' && $name !== '..') {
78+
System::exec('rm -rf ' . BASE_PATH . '/runtime/logs/admin_logger_database/' . $name);
79+
}
80+
}
81+
}
82+
83+
/**
84+
* 清理帖子更新记录.
85+
*/
86+
private function topic_updated(): void
87+
{
88+
foreach (Topic::query()->get('id') as $topic) {
89+
if (TopicUpdated::query()->where('topic_id', $topic->id)->count() > 10) {
90+
$all = TopicUpdated::query()->where('topic_id', $topic->id)->skip(10)->take(100)->get();
91+
foreach ($all as $id) {
92+
TopicUpdated::query()->where('id', $id->id)->delete();
93+
}
7094
}
7195
}
7296
}
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
311
namespace App\Plugins\Core\src\Crontab;
412

513
use App\Model\AdminOption;
6-
use App\Plugins\Topic\src\Models\Topic;
7-
use App\Plugins\User\src\Models\UsersNotice;
814
use App\Plugins\User\src\Models\UsersPm;
915
use Hyperf\Crontab\Annotation\Crontab;
1016

1117
/**
12-
* 用户私信清理
13-
* @Crontab(name="CleanUsersPm", rule="0 * * * *", callback="execute", enable={CleanUsersPm::class, "isEnable"}, memo="用户私信清理")
18+
* 用户私信清理.
19+
* @Crontab(name="CleanUsersPm", rule="0 *\/12 * * *", callback="execute", enable={CleanUsersPm::class, "isEnable"}, memo="用户私信清理")
1420
*/
1521
class CleanUsersPm
1622
{
17-
public function execute()
23+
public function execute(): void
1824
{
1925
// 消息保留时间,单位:秒
20-
$reserve = (int)get_options('pm_msg_reserve', 7)*24*60*60;
21-
26+
$reserve = (int) get_options('pm_msg_reserve', 7) * 24 * 60 * 60;
27+
2228
foreach (UsersPm::query()->get() as $pm) {
23-
if (time()-strtotime($pm->created_at)>=$reserve) {
29+
if (time() - strtotime($pm->created_at) >= $reserve) {
2430
UsersPm::query()->where('id', $pm->id)->delete();
2531
}
2632
}
2733
}
28-
34+
2935
public function isEnable(): bool
3036
{
31-
return !((int)$this->get_options('pm_msg_reserve', 7) === 0);
37+
return ! ((int) $this->get_options('pm_msg_reserve', 7) === 0);
3238
}
33-
34-
private function core_default($string=null, $default=null)
39+
40+
private function core_default($string = null, $default = null)
3541
{
3642
if ($string) {
3743
return $string;
3844
}
3945
return $default;
4046
}
41-
42-
private function get_options($name, $default="")
47+
48+
private function get_options($name, $default = '')
4349
{
44-
if (!cache()->has('admin.options.'.$name)) {
45-
cache()->set("admin.options.".$name, @AdminOption::query()->where("name", $name)->first()->value);
50+
if (! cache()->has('admin.options.' . $name)) {
51+
cache()->set('admin.options.' . $name, @AdminOption::query()->where('name', $name)->first()->value);
4652
}
47-
return $this->core_default(cache()->get("admin.options.".$name), $default);
53+
return $this->core_default(cache()->get('admin.options.' . $name), $default);
4854
}
4955
}

‎app/Plugins/Topic/src/Controllers/ApiController.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,13 @@ public function get_user_ip()
313313
$updateds = request()->input('updateds');
314314
$data = [];
315315
foreach ($updateds as $updated_id) {
316-
Timer::tick(1000, function () use ($updated_id, $data) {
317-
$updated = TopicUpdated::query()->where(['id' => $updated_id])->first();
318-
if ($updated->user_ip) {
319-
$data[] = [
320-
'updated_id' => $updated->id,
321-
'text' => 'IP归属地:' . core_default(get_client_ip_data($updated->user_ip)['pro'], '未知'),
322-
];
323-
}
324-
});
316+
$updated = TopicUpdated::query()->where(['id' => $updated_id])->first();
317+
if ($updated->user_ip) {
318+
$data[] = [
319+
'updated_id' => $updated->id,
320+
'text' => 'IP归属地:' . core_default(get_client_ip_data($updated->user_ip)['pro'], '未知'),
321+
];
322+
}
325323
}
326324
return Json_Api(200, true, $data);
327325
}

0 commit comments

Comments
 (0)
Please sign in to comment.