1
1
<?php
2
2
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
+ */
3
11
namespace App \Plugins \Core \src \Crontab ;
4
12
5
13
use App \Plugins \Core \src \Models \PayOrder ;
6
14
use App \Plugins \Topic \src \Models \Topic ;
15
+ use App \Plugins \Topic \src \Models \TopicUpdated ;
7
16
use App \Plugins \User \src \Models \UsersNotice ;
8
17
use Hyperf \Crontab \Annotation \Crontab ;
18
+ use Swoole \Coroutine \System ;
9
19
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: '数据库垃圾清理 ' )]
13
21
class CleanDatabase
14
22
{
15
23
public function execute ()
@@ -22,19 +30,21 @@ public function execute()
22
30
$ this ->order ();
23
31
// 清理admin_logger日志
24
32
$ this ->admin_logger ();
33
+ // 清理帖子更新记录
34
+ $ this ->topic_updated ();
25
35
}
26
-
36
+
27
37
public function isEnable (): bool
28
38
{
29
39
return true ;
30
40
}
31
-
41
+
32
42
// 清理已删除的文章
33
43
private function topic ()
34
44
{
35
45
Topic::query ()->where ('status ' , 'delete ' )->delete ();
36
46
}
37
-
47
+
38
48
// 清理已读通知
39
49
private function notice ()
40
50
{
@@ -46,8 +56,8 @@ private function order()
46
56
{
47
57
$ data = [];
48
58
foreach (PayOrder::query ()->where ('status ' , '待支付 ' )
49
- ->orWhere ('status ' , '订单取消 ' )
50
- ->orWhere ('status ' , '交易关闭 ' )->get () as $ value ) {
59
+ ->orWhere ('status ' , '订单取消 ' )
60
+ ->orWhere ('status ' , '交易关闭 ' )->get () as $ value ) {
51
61
if (time () - strtotime ($ value ->created_at ) > 86400 ) {
52
62
$ data [] = $ value ->id ;
53
63
}
@@ -59,14 +69,28 @@ private function order()
59
69
}
60
70
61
71
/**
62
- * 清理admin_logger过期日志
63
- * @return void
72
+ * 清理admin_logger过期日志.
64
73
*/
65
74
private function admin_logger (): void
66
75
{
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
+ }
70
94
}
71
95
}
72
96
}
0 commit comments