Gestionnaire de jobs et workflows
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.2 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package mongo
import (
"time"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"forge.chapril.org/dune/dune/internal/storage"
)
func jobsRunning(ctx mongo.SessionContext, col *mongo.Collection, namespace string) (storage.JobsRunning, error) {
cur, err := col.Find(
ctx,
bson.M{
"namespace": namespace,
"status": jw.StatusRunning,
},
)
if err != nil {
return nil, err
}
defer cur.Close(ctx)
jr := make(storage.JobsRunning)
job := new(jw.Job)
for cur.Next(ctx) {
if err := cur.Decode(job); err != nil {
return nil, err
}
jr[job.Type] += 1
}
if err := cur.Err(); err != nil {
return nil, err
}
return jr, nil
}
type jobsToRun struct {
ctx mongo.SessionContext
cursor *mongo.Cursor
}
func newJobsToRun(ctx mongo.SessionContext, col *mongo.Collection, namespace string) (*jobsToRun, error) {
cur, err := col.Find(
ctx,
bson.M{
"namespace": namespace,
"run_after": bson.M{"$lt": time.Now()},
"$or": []bson.M{
{"status": jw.StatusTodo},
{"status": jw.StatusPending},
},
},
options.Find().SetSort(
bson.D{
primitive.E{Key: "priority", Value: 1},
primitive.E{Key: "weight", Value: -1},
primitive.E{Key: "time_reference", Value: -1},
},
),
)
if err != nil {
return nil, err
}
jtr := &jobsToRun{
ctx: ctx,
cursor: cur,
}
return jtr, nil
}
func (jtr *jobsToRun) Next() (*jw.Job, error) {
cur := jtr.cursor
if cur.Next(jtr.ctx) {
job := new(jw.Job)
err := cur.Decode(job)
if err != nil {
return nil, err
}
return job, nil
}
return nil, cur.Err()
}
func (jtr *jobsToRun) close() {
jtr.cursor.Close(jtr.ctx)
}
func (ms *mongoStorage) NextJob(namespace string, fn storage.SelectNextJob) (*jw.Job, error) {
col := ms.Collection("jobs")
ctx, cancel := util.CtxWithTimeout(10 * time.Second)
defer cancel()
session, err := ms.client.StartSession()
if err != nil {
return nil, err
}
defer session.EndSession(ctx)
result, err := session.WithTransaction(
ctx,
func(ctx mongo.SessionContext) (interface{}, error) {
jr, err := jobsRunning(ctx, col, namespace)
if err != nil {
return nil, err
}
jtr, err := newJobsToRun(ctx, col, namespace)
if err != nil {
return nil, err
}
defer jtr.close()
job, err := fn(jr, jtr)
if err != nil || job == nil {
return nil, err
}
_, err = col.UpdateByID(ctx, job.ID, bson.M{"$set": bson.M{"status": jw.StatusRunning}})
if err != nil {
return nil, err
}
return job, nil
},
)
if err != nil || result == nil {
return nil, err
}
return result.(*jw.Job), nil
}
/*
######################################################################################################## @(°_°)@ #######
*/