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.

96 lines
2.2 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package mongo
import (
"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) InsertJob(job *jw.Job) (bool, error) {
col := ms.Collection("jobs")
ctx, cancel := util.CtxWithTimeout(5 * time.Second)
defer cancel()
if job.Category == nil {
_, err := col.InsertOne(ctx, job)
if err != nil {
return false, err
}
return true, nil
}
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 := col.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
}
_, err = col.InsertOne(ctx, job)
if err != nil {
return false, err
}
return true, nil
},
)
return result.(bool), err
}
func (ms *mongoStorage) UpdateJob(job *jw.Job) error {
return nil
}
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()
}
/*
######################################################################################################## @(°_°)@ #######
*/