mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
add time task server
This commit is contained in:
parent
734a8a0fbd
commit
9f867b81cb
42
src/timed_task/open_im_timed_task.go
Normal file
42
src/timed_task/open_im_timed_task.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/src/common/config"
|
||||||
|
"Open_IM/src/common/db"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
for {
|
||||||
|
fmt.Println("start delete mongodb expired record")
|
||||||
|
timeUnixBegin := time.Now().Unix()
|
||||||
|
count, _ := db.DB.MgoUserCount()
|
||||||
|
fmt.Println("mongodb record count: ", count)
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
time.Sleep(1 * time.Millisecond)
|
||||||
|
uid, _ := db.DB.MgoSkipUID(i)
|
||||||
|
fmt.Println("operate uid: ", uid)
|
||||||
|
err := db.DB.DelUserChat(uid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("operate uid failed: ", uid, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
timeUnixEnd := time.Now().Unix()
|
||||||
|
costTime := timeUnixEnd - timeUnixBegin
|
||||||
|
if costTime > int64(config.Config.Mongo.DBRetainChatRecords*24*3600) {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
sleepTime := 0
|
||||||
|
if int64(config.Config.Mongo.DBRetainChatRecords*24*3600)-costTime > 24*3600 {
|
||||||
|
sleepTime = 24 * 3600
|
||||||
|
} else {
|
||||||
|
sleepTime = config.Config.Mongo.DBRetainChatRecords*24*3600 - int(costTime)
|
||||||
|
}
|
||||||
|
fmt.Println("sleep: ", sleepTime)
|
||||||
|
time.Sleep(time.Duration(sleepTime) * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
26
src/timed_task/timed_task/init.go
Normal file
26
src/timed_task/timed_task/init.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package timed_task
|
||||||
|
|
||||||
|
type TimeTask struct {
|
||||||
|
delMgoChatChan chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var timeTask TimeTask
|
||||||
|
|
||||||
|
func GetInstance() *TimeTask {
|
||||||
|
if timeTask.delMgoChatChan == nil {
|
||||||
|
timeTask.delMgoChatChan = make(chan bool)
|
||||||
|
go func() {
|
||||||
|
timeTask.delMgoChatChan <- true
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
return &timeTask
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TimeTask) Run() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-t.delMgoChatChan:
|
||||||
|
t.timedDeleteUserChat()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
src/timed_task/timed_task/timed_task.go
Normal file
26
src/timed_task/timed_task/timed_task.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package timed_task
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/src/common/db"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t *TimeTask) timedDeleteUserChat() {
|
||||||
|
now := time.Now()
|
||||||
|
next := now.Add(time.Hour * 24)
|
||||||
|
next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
|
||||||
|
tm := time.NewTimer(next.Sub(now))
|
||||||
|
|
||||||
|
<-tm.C
|
||||||
|
|
||||||
|
count, _ := db.DB.MgoUserCount()
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
uid, _ := db.DB.MgoSkipUID(i)
|
||||||
|
db.DB.DelUserChat(uid)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
t.delMgoChatChan <- true
|
||||||
|
}()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user