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.

106 lines
2.6 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package mongo
import (
"context"
"time"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func (ms *mongoStorage) ValidateJob(_ *jw.Job) error {
return nil
}
func (ms *mongoStorage) txInsertJob(ctx mongo.SessionContext, job *jw.Job) error {
_, err := ms.cJobs().InsertOne(ctx, job)
return err
}
func (ms *mongoStorage) maybeInsertJob(ctx context.Context, job *jw.Job) (bool, error) {
session, err := ms.client.StartSession()
if err != nil {
return false, err
}
defer session.EndSession(ctx)
result, err := session.WithTransaction(
ctx,
func(ctx mongo.SessionContext) (interface{}, error) {
err := ms.cJobs().FindOne(
ctx,
bson.M{
"category": *job.Category,
"namespace": job.Namespace,
"type": job.Type,
"workflow": nil,
"$or": []bson.M{
{"status": jw.StatusTodo},
{"status": jw.StatusPending},
{"status": jw.StatusRunning},
},
},
).Err()
if err == nil || err != mongo.ErrNoDocuments {
return false, err
}
if err := ms.txInsertJob(ctx, job); err != nil {
return false, err
}
return true, nil
},
)
return result.(bool), err
}
func (ms *mongoStorage) InsertJob(job *jw.Job) (bool, error) {
ctx, cancel := util.CtxWithTimeout(5 * time.Second)
defer cancel()
if job.Category == nil {
_, err := ms.cJobs().InsertOne(ctx, job)
if err != nil {
return false, err
}
return true, nil
}
return ms.maybeInsertJob(ctx, job)
}
func (ms *mongoStorage) UpdateJob(job *jw.Job) error {
ctx, cancel := util.CtxWithTimeout(5 * time.Second)
defer cancel()
return ms.cJobs().FindOneAndReplace(ctx, bson.M{"_id": job.ID}, job).Err()
}
func (ms *mongoStorage) NotifyJob(id string, data interface{}) error {
return errors.NotImplemented()
}
func (ms *mongoStorage) SetJobPriority(id string, priority jw.Priority) error {
return errors.NotImplemented()
}
func (ms *mongoStorage) SetJobRunAfter(id string, duration time.Duration) error {
return errors.NotImplemented()
}
/*
######################################################################################################## @(°_°)@ #######
*/