-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
1,609 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tasks | ||
|
||
import ( | ||
"github.com/cufee/aftermath/cmds/core" | ||
"github.com/cufee/aftermath/internal/database" | ||
) | ||
|
||
type TaskHandler struct { | ||
process func(client core.Client, task database.Task) (string, error) | ||
shouldRetry func(task *database.Task) bool | ||
} | ||
|
||
var defaultHandlers = make(map[database.TaskType]TaskHandler) | ||
|
||
func DefaultHandlers() map[database.TaskType]TaskHandler { | ||
return defaultHandlers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package tasks | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cufee/aftermath/cmds/core" | ||
"github.com/cufee/aftermath/internal/database" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type Queue struct { | ||
limiter chan struct{} | ||
concurrencyLimit int | ||
lastTaskRun time.Time | ||
|
||
handlers map[database.TaskType]TaskHandler | ||
core core.Client | ||
} | ||
|
||
func (q *Queue) ConcurrencyLimit() int { | ||
return q.concurrencyLimit | ||
} | ||
|
||
func (q *Queue) ActiveWorkers() int { | ||
return len(q.limiter) | ||
} | ||
|
||
func (q *Queue) LastTaskRun() time.Time { | ||
return q.lastTaskRun | ||
} | ||
|
||
func NewQueue(client core.Client, handlers map[database.TaskType]TaskHandler, concurrencyLimit int) *Queue { | ||
return &Queue{ | ||
core: client, | ||
handlers: handlers, | ||
concurrencyLimit: concurrencyLimit, | ||
limiter: make(chan struct{}, concurrencyLimit), | ||
} | ||
} | ||
|
||
func (q *Queue) Process(callback func(error), tasks ...database.Task) { | ||
var err error | ||
if callback != nil { | ||
defer callback(err) | ||
} | ||
if len(tasks) == 0 { | ||
log.Debug().Msg("no tasks to process") | ||
return | ||
} | ||
|
||
log.Debug().Msgf("processing %d tasks", len(tasks)) | ||
|
||
var wg sync.WaitGroup | ||
q.lastTaskRun = time.Now() | ||
processedTasks := make(chan database.Task, len(tasks)) | ||
for _, task := range tasks { | ||
wg.Add(1) | ||
go func(t database.Task) { | ||
q.limiter <- struct{}{} | ||
defer func() { | ||
processedTasks <- t | ||
wg.Done() | ||
<-q.limiter | ||
log.Debug().Msgf("finished processing task %s", t.ID) | ||
}() | ||
log.Debug().Msgf("processing task %s", t.ID) | ||
|
||
handler, ok := q.handlers[t.Type] | ||
if !ok { | ||
t.Status = database.TaskStatusFailed | ||
t.LogAttempt(database.TaskLog{ | ||
Targets: t.Targets, | ||
Timestamp: time.Now(), | ||
Error: "missing task type handler", | ||
}) | ||
return | ||
} | ||
|
||
attempt := database.TaskLog{ | ||
Targets: t.Targets, | ||
Timestamp: time.Now(), | ||
} | ||
|
||
message, err := handler.process(nil, t) | ||
attempt.Comment = message | ||
if err != nil { | ||
attempt.Error = err.Error() | ||
t.Status = database.TaskStatusFailed | ||
} else { | ||
t.Status = database.TaskStatusComplete | ||
} | ||
t.LogAttempt(attempt) | ||
}(task) | ||
} | ||
|
||
wg.Wait() | ||
close(processedTasks) | ||
|
||
rescheduledCount := 0 | ||
processedSlice := make([]database.Task, 0, len(processedTasks)) | ||
for task := range processedTasks { | ||
handler, ok := q.handlers[task.Type] | ||
if !ok { | ||
continue | ||
} | ||
|
||
if task.Status == database.TaskStatusFailed && handler.shouldRetry(&task) { | ||
rescheduledCount++ | ||
task.Status = database.TaskStatusScheduled | ||
} | ||
processedSlice = append(processedSlice, task) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
|
||
err = q.core.Database().UpdateTasks(ctx, processedSlice...) | ||
if err != nil { | ||
return | ||
} | ||
|
||
log.Debug().Msgf("processed %d tasks, %d rescheduled", len(processedSlice), rescheduledCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package tasks | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cufee/aftermath/cmds/core" | ||
"github.com/cufee/aftermath/internal/database" | ||
) | ||
|
||
func init() { | ||
defaultHandlers[database.TaskTypeRecordSessions] = TaskHandler{ | ||
process: func(client core.Client, task database.Task) (string, error) { | ||
if task.Data == nil { | ||
return "no data provided", errors.New("no data provided") | ||
} | ||
realm, ok := task.Data["realm"].(string) | ||
if !ok { | ||
return "invalid realm", errors.New("invalid realm") | ||
} | ||
|
||
return "did nothing for a task on realm " + realm, nil | ||
|
||
// accountErrs, err := cache.RefreshSessionsAndAccounts(models.SessionTypeDaily, nil, realm, task.Targets...) | ||
// if err != nil { | ||
// return "failed to refresh sessions on all account", err | ||
// } | ||
|
||
// var failedAccounts []int | ||
// for accountId, err := range accountErrs { | ||
// if err != nil && accountId != 0 { | ||
// failedAccounts = append(failedAccounts, accountId) | ||
// } | ||
// } | ||
// if len(failedAccounts) == 0 { | ||
// return "finished session update on all accounts", nil | ||
// } | ||
|
||
// // Retry failed accounts | ||
// task.Targets = failedAccounts | ||
// return "retrying failed accounts", errors.New("some accounts failed") | ||
}, | ||
shouldRetry: func(task *database.Task) bool { | ||
triesLeft, ok := task.Data["triesLeft"].(int32) | ||
if !ok { | ||
return false | ||
} | ||
if triesLeft <= 0 { | ||
return false | ||
} | ||
|
||
triesLeft -= 1 | ||
task.Data["triesLeft"] = triesLeft | ||
task.ScheduledAfter = time.Now().Add(5 * time.Minute) // Backoff for 5 minutes to avoid spamming | ||
return true | ||
}, | ||
} | ||
} | ||
|
||
func CreateSessionUpdateTasks(client core.Client) func(realm string) error { | ||
return func(realm string) error { | ||
realm = strings.ToUpper(realm) | ||
task := database.Task{ | ||
Type: database.TaskTypeRecordSessions, | ||
ReferenceID: "realm_" + realm, | ||
Data: map[string]any{ | ||
"realm": realm, | ||
"triesLeft": int32(3), | ||
}, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
|
||
accounts, err := client.Database().GetRealmAccounts(ctx, realm) | ||
if err != nil { | ||
return err | ||
} | ||
if len(accounts) < 1 { | ||
return nil | ||
} | ||
|
||
// This update requires (2 + n) requests per n players | ||
return client.Database().CreateTasks(ctx, splitTaskByTargets(task, 50)...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tasks | ||
|
||
import "github.com/cufee/aftermath/internal/database" | ||
|
||
func splitTaskByTargets(task database.Task, batchSize int) []database.Task { | ||
if len(task.Targets) <= batchSize { | ||
return []database.Task{task} | ||
} | ||
|
||
var tasks []database.Task | ||
subTasks := len(task.Targets) / batchSize | ||
|
||
for i := 0; i <= subTasks; i++ { | ||
subTask := task | ||
if len(task.Targets) > batchSize*(i+1) { | ||
subTask.Targets = (task.Targets[batchSize*i : batchSize*(i+1)]) | ||
} else { | ||
subTask.Targets = (task.Targets[batchSize*i:]) | ||
} | ||
tasks = append(tasks, subTask) | ||
} | ||
|
||
return tasks | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/cufee/aftermath/cmds/discord/commands/builder" | ||
"github.com/cufee/aftermath/cmds/discord/common" | ||
) | ||
|
||
func init() { | ||
Loaded.add( | ||
builder.NewCommand("ping"). | ||
Params(builder.SetDescKey("Pong!")). | ||
Handler(func(ctx *common.Context) error { | ||
return ctx.Reply("Pong!") | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.