2017-09-13 15:09:13 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora Task System
|
2019-05-01 23:14:07 +00:00
|
|
|
* Copyright Azareal 2017 - 2020
|
2017-09-13 15:09:13 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-11-11 04:06:16 +00:00
|
|
|
package common
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-10-16 07:32:58 +00:00
|
|
|
import (
|
2017-11-11 23:34:27 +00:00
|
|
|
"database/sql"
|
2017-10-16 07:32:58 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
2021-04-07 14:23:11 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2017-10-16 07:32:58 +00:00
|
|
|
)
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-11-11 23:34:27 +00:00
|
|
|
type TaskStmts struct {
|
|
|
|
getExpiredScheduledGroups *sql.Stmt
|
|
|
|
getSync *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2018-01-11 08:03:17 +00:00
|
|
|
var ScheduledHalfSecondTasks []func() error
|
2017-12-10 03:43:30 +00:00
|
|
|
var ScheduledSecondTasks []func() error
|
|
|
|
var ScheduledFifteenMinuteTasks []func() error
|
2018-05-31 06:51:31 +00:00
|
|
|
var ScheduledHourTasks []func() error
|
2021-04-29 12:59:48 +00:00
|
|
|
var ScheduledDayTasks []func() error
|
2017-12-24 22:08:35 +00:00
|
|
|
var ShutdownTasks []func() error
|
2017-11-11 23:34:27 +00:00
|
|
|
var taskStmts TaskStmts
|
2017-09-10 16:57:22 +00:00
|
|
|
var lastSync time.Time
|
|
|
|
|
2017-12-27 23:38:37 +00:00
|
|
|
// TODO: Add a TaskInits.Add
|
2017-09-10 16:57:22 +00:00
|
|
|
func init() {
|
|
|
|
lastSync = time.Now()
|
2017-11-12 03:29:05 +00:00
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
2017-11-11 23:34:27 +00:00
|
|
|
taskStmts = TaskStmts{
|
2017-11-12 03:29:05 +00:00
|
|
|
getExpiredScheduledGroups: acc.Select("users_groups_scheduler").Columns("uid").Where("UTC_TIMESTAMP() > revert_at AND temporary = 1").Prepare(),
|
|
|
|
getSync: acc.Select("sync").Columns("last_update").Prepare(),
|
2017-11-11 23:34:27 +00:00
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
2017-09-10 16:57:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 08:03:17 +00:00
|
|
|
// AddScheduledHalfSecondTask is not concurrency safe
|
|
|
|
func AddScheduledHalfSecondTask(task func() error) {
|
|
|
|
ScheduledHalfSecondTasks = append(ScheduledHalfSecondTasks, task)
|
|
|
|
}
|
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
// AddScheduledSecondTask is not concurrency safe
|
|
|
|
func AddScheduledSecondTask(task func() error) {
|
|
|
|
ScheduledSecondTasks = append(ScheduledSecondTasks, task)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddScheduledFifteenMinuteTask is not concurrency safe
|
|
|
|
func AddScheduledFifteenMinuteTask(task func() error) {
|
|
|
|
ScheduledFifteenMinuteTasks = append(ScheduledFifteenMinuteTasks, task)
|
|
|
|
}
|
|
|
|
|
2018-05-31 06:51:31 +00:00
|
|
|
// AddScheduledHourTask is not concurrency safe
|
|
|
|
func AddScheduledHourTask(task func() error) {
|
|
|
|
ScheduledHourTasks = append(ScheduledHourTasks, task)
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:59:48 +00:00
|
|
|
// AddScheduledDayTask is not concurrency safe
|
|
|
|
func AddScheduledDayTask(task func() error) {
|
|
|
|
ScheduledDayTasks = append(ScheduledDayTasks, task)
|
|
|
|
}
|
|
|
|
|
2017-12-24 22:08:35 +00:00
|
|
|
// AddShutdownTask is not concurrency safe
|
|
|
|
func AddShutdownTask(task func() error) {
|
|
|
|
ShutdownTasks = append(ShutdownTasks, task)
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledHalfSecondTaskCount is not concurrency safe
|
|
|
|
func ScheduledHalfSecondTaskCount() int {
|
|
|
|
return len(ScheduledHalfSecondTasks)
|
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledSecondTaskCount is not concurrency safe
|
|
|
|
func ScheduledSecondTaskCount() int {
|
|
|
|
return len(ScheduledSecondTasks)
|
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledFifteenMinuteTaskCount is not concurrency safe
|
|
|
|
func ScheduledFifteenMinuteTaskCount() int {
|
|
|
|
return len(ScheduledFifteenMinuteTasks)
|
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledHourTaskCount is not concurrency safe
|
|
|
|
func ScheduledHourTaskCount() int {
|
|
|
|
return len(ScheduledHourTasks)
|
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2021-04-29 12:59:48 +00:00
|
|
|
// ScheduledDayTaskCount is not concurrency safe
|
|
|
|
func ScheduledDayTaskCount() int {
|
|
|
|
return len(ScheduledDayTasks)
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ShutdownTaskCount is not concurrency safe
|
|
|
|
func ShutdownTaskCount() int {
|
|
|
|
return len(ShutdownTasks)
|
|
|
|
}
|
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
// TODO: Use AddScheduledSecondTask
|
2017-11-11 04:06:16 +00:00
|
|
|
func HandleExpiredScheduledGroups() error {
|
2021-04-07 14:23:11 +00:00
|
|
|
rows, e := taskStmts.getExpiredScheduledGroups.Query()
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var uid int
|
|
|
|
for rows.Next() {
|
2021-04-07 14:23:11 +00:00
|
|
|
if e := rows.Scan(&uid); e != nil {
|
|
|
|
return e
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-10-21 00:27:47 +00:00
|
|
|
// Sneaky way of initialising a *User, please use the methods on the UserStore instead
|
2017-11-11 04:06:16 +00:00
|
|
|
user := BlankUser()
|
2017-10-21 00:27:47 +00:00
|
|
|
user.ID = uid
|
2021-04-29 12:59:48 +00:00
|
|
|
if e = user.RevertGroupUpdate(); e != nil {
|
2021-04-07 14:23:11 +00:00
|
|
|
return e
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows.Err()
|
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
// TODO: Use AddScheduledSecondTask
|
2018-02-15 13:15:27 +00:00
|
|
|
// TODO: Be a little more granular with the synchronisation
|
2018-05-31 06:51:31 +00:00
|
|
|
// TODO: Synchronise more things
|
2018-08-04 11:46:36 +00:00
|
|
|
// TODO: Does this even work?
|
2017-11-11 04:06:16 +00:00
|
|
|
func HandleServerSync() error {
|
2018-02-15 13:15:27 +00:00
|
|
|
// We don't want to run any unnecessary queries when there is nothing to synchronise
|
2018-11-22 07:21:43 +00:00
|
|
|
if Config.ServerCount == 1 {
|
2018-02-15 13:15:27 +00:00
|
|
|
return nil
|
2018-11-22 07:21:43 +00:00
|
|
|
}
|
2018-02-15 13:15:27 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
var lastUpdate time.Time
|
2021-04-07 14:23:11 +00:00
|
|
|
e := taskStmts.getSync.QueryRow().Scan(&lastUpdate)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2017-09-10 16:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if lastUpdate.After(lastSync) {
|
2021-04-29 12:59:48 +00:00
|
|
|
if e = Forums.LoadForums(); e != nil {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("Unable to reload the forums")
|
2021-04-07 14:23:11 +00:00
|
|
|
return e
|
2017-09-10 16:57:22 +00:00
|
|
|
}
|
|
|
|
// TODO: Resync the groups
|
|
|
|
// TODO: Resync the permissions
|
2021-04-29 12:59:48 +00:00
|
|
|
if e = LoadSettings(); e != nil {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("Unable to reload the settings")
|
2021-04-07 14:23:11 +00:00
|
|
|
return e
|
2017-09-10 16:57:22 +00:00
|
|
|
}
|
2021-04-29 12:59:48 +00:00
|
|
|
if e = WordFilters.ReloadAll(); e != nil {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("Unable to reload the word filters")
|
2021-04-07 14:23:11 +00:00
|
|
|
return e
|
2017-09-10 16:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|