2017-09-13 15:09:13 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora Task System
|
|
|
|
* Copyright Azareal 2017 - 2018
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
"../query_gen/lib"
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
var ScheduledSecondTasks []func() error
|
|
|
|
var ScheduledFifteenMinuteTasks []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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-12-24 22:08:35 +00:00
|
|
|
// AddShutdownTask is not concurrency safe
|
|
|
|
func AddShutdownTask(task func() error) {
|
|
|
|
ShutdownTasks = append(ShutdownTasks, task)
|
|
|
|
}
|
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
// TODO: Use AddScheduledSecondTask
|
2017-11-11 04:06:16 +00:00
|
|
|
func HandleExpiredScheduledGroups() error {
|
2017-11-11 23:34:27 +00:00
|
|
|
rows, err := taskStmts.getExpiredScheduledGroups.Query()
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var uid int
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&uid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
err = user.RevertGroupUpdate()
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows.Err()
|
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
// TODO: Use AddScheduledSecondTask
|
2017-11-11 04:06:16 +00:00
|
|
|
func HandleServerSync() error {
|
2017-09-10 16:57:22 +00:00
|
|
|
var lastUpdate time.Time
|
2017-11-11 23:34:27 +00:00
|
|
|
err := taskStmts.getSync.QueryRow().Scan(&lastUpdate)
|
2017-09-10 16:57:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastUpdate.After(lastSync) {
|
|
|
|
// TODO: A more granular sync
|
2017-11-23 05:37:08 +00:00
|
|
|
err = Forums.LoadForums()
|
2017-09-10 16:57:22 +00:00
|
|
|
if err != nil {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("Unable to reload the forums")
|
2017-09-10 16:57:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Resync the groups
|
|
|
|
// TODO: Resync the permissions
|
|
|
|
err = LoadSettings()
|
|
|
|
if err != nil {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("Unable to reload the settings")
|
2017-09-10 16:57:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = LoadWordFilters()
|
|
|
|
if err != nil {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("Unable to reload the word filters")
|
2017-09-10 16:57:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|