2017-09-13 15:09:13 +00:00
|
|
|
/*
|
|
|
|
*
|
2022-02-21 03:32:53 +00:00
|
|
|
* Gosora Task System
|
|
|
|
* 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 (
|
2022-02-21 03:32:53 +00:00
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
"time"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
2022-02-21 03:32:53 +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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
getExpiredScheduledGroups *sql.Stmt
|
|
|
|
getSync *sql.Stmt
|
2017-11-11 23:34:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 00:36:29 +00:00
|
|
|
var Tasks *ScheduledTasks
|
|
|
|
|
|
|
|
type TaskSet interface {
|
2022-02-21 03:32:53 +00:00
|
|
|
Add(func() error)
|
|
|
|
GetList() []func() error
|
|
|
|
Run() error
|
|
|
|
Count() int
|
2021-05-03 00:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultTaskSet struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
Tasks []func() error
|
2021-05-03 00:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultTaskSet) Add(task func() error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
s.Tasks = append(s.Tasks, task)
|
2021-05-03 00:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultTaskSet) GetList() []func() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
return s.Tasks
|
2021-05-03 00:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultTaskSet) Run() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
for _, task := range s.Tasks {
|
|
|
|
if e := task(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-03 00:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultTaskSet) Count() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
return len(s.Tasks)
|
2021-05-03 00:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ScheduledTasks struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
HalfSec TaskSet
|
|
|
|
Sec TaskSet
|
|
|
|
FifteenMin TaskSet
|
|
|
|
Hour TaskSet
|
|
|
|
Day TaskSet
|
|
|
|
Shutdown TaskSet
|
2021-05-03 00:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewScheduledTasks() *ScheduledTasks {
|
2022-02-21 03:32:53 +00:00
|
|
|
return &ScheduledTasks{
|
|
|
|
HalfSec: &DefaultTaskSet{},
|
|
|
|
Sec: &DefaultTaskSet{},
|
|
|
|
FifteenMin: &DefaultTaskSet{},
|
|
|
|
Hour: &DefaultTaskSet{},
|
|
|
|
Day: &DefaultTaskSet{},
|
|
|
|
Shutdown: &DefaultTaskSet{},
|
|
|
|
}
|
2021-05-03 00:36:29 +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
|
2021-05-03 00:36:29 +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() {
|
2022-02-21 03:32:53 +00:00
|
|
|
lastSync = time.Now()
|
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
|
|
|
taskStmts = TaskStmts{
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
2017-09-10 16:57:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 08:03:17 +00:00
|
|
|
// AddScheduledHalfSecondTask is not concurrency safe
|
2021-05-03 00:36:29 +00:00
|
|
|
/*func AddScheduledHalfSecondTask(task func() error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
ScheduledHalfSecondTasks = append(ScheduledHalfSecondTasks, task)
|
2018-01-11 08:03:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
// AddScheduledSecondTask is not concurrency safe
|
|
|
|
func AddScheduledSecondTask(task func() error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
ScheduledSecondTasks = append(ScheduledSecondTasks, task)
|
2017-12-10 03:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddScheduledFifteenMinuteTask is not concurrency safe
|
|
|
|
func AddScheduledFifteenMinuteTask(task func() error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
ScheduledFifteenMinuteTasks = append(ScheduledFifteenMinuteTasks, task)
|
2017-12-10 03:43:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 06:51:31 +00:00
|
|
|
// AddScheduledHourTask is not concurrency safe
|
|
|
|
func AddScheduledHourTask(task func() error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
ScheduledHourTasks = append(ScheduledHourTasks, task)
|
2018-05-31 06:51:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 12:59:48 +00:00
|
|
|
// AddScheduledDayTask is not concurrency safe
|
|
|
|
func AddScheduledDayTask(task func() error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
ScheduledDayTasks = append(ScheduledDayTasks, task)
|
2021-04-29 12:59:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 22:08:35 +00:00
|
|
|
// AddShutdownTask is not concurrency safe
|
|
|
|
func AddShutdownTask(task func() error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
ShutdownTasks = append(ShutdownTasks, task)
|
2017-12-24 22:08:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledHalfSecondTaskCount is not concurrency safe
|
|
|
|
func ScheduledHalfSecondTaskCount() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
return len(ScheduledHalfSecondTasks)
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledSecondTaskCount is not concurrency safe
|
|
|
|
func ScheduledSecondTaskCount() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
return len(ScheduledSecondTasks)
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledFifteenMinuteTaskCount is not concurrency safe
|
|
|
|
func ScheduledFifteenMinuteTaskCount() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
return len(ScheduledFifteenMinuteTasks)
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ScheduledHourTaskCount is not concurrency safe
|
|
|
|
func ScheduledHourTaskCount() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
return len(ScheduledHourTasks)
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2021-04-29 12:59:48 +00:00
|
|
|
// ScheduledDayTaskCount is not concurrency safe
|
|
|
|
func ScheduledDayTaskCount() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
return len(ScheduledDayTasks)
|
2021-04-29 12:59:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// ShutdownTaskCount is not concurrency safe
|
|
|
|
func ShutdownTaskCount() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
return len(ShutdownTasks)
|
2021-05-03 00:36:29 +00:00
|
|
|
}*/
|
2020-02-23 09:08:47 +00:00
|
|
|
|
2017-12-10 03:43:30 +00:00
|
|
|
// TODO: Use AddScheduledSecondTask
|
2017-11-11 04:06:16 +00:00
|
|
|
func HandleExpiredScheduledGroups() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
rows, e := taskStmts.getExpiredScheduledGroups.Query()
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var uid int
|
|
|
|
for rows.Next() {
|
|
|
|
if e := rows.Scan(&uid); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
// Sneaky way of initialising a *User, please use the methods on the UserStore instead
|
|
|
|
user := BlankUser()
|
|
|
|
user.ID = uid
|
|
|
|
if e = user.RevertGroupUpdate(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows.Err()
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
// We don't want to run any unnecessary queries when there is nothing to synchronise
|
|
|
|
if Config.ServerCount == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastUpdate time.Time
|
|
|
|
e := taskStmts.getSync.QueryRow().Scan(&lastUpdate)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastUpdate.After(lastSync) {
|
|
|
|
if e = Forums.LoadForums(); e != nil {
|
|
|
|
log.Print("Unable to reload the forums")
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
// TODO: Resync the groups
|
|
|
|
// TODO: Resync the permissions
|
|
|
|
if e = LoadSettings(); e != nil {
|
|
|
|
log.Print("Unable to reload the settings")
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if e = WordFilters.ReloadAll(); e != nil {
|
|
|
|
log.Print("Unable to reload the word filters")
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-09-10 16:57:22 +00:00
|
|
|
}
|