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.
dune/internal/storage/mongo/mongo.go

95 lines
2.1 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package mongo
import (
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/logger"
_mgo "forge.chapril.org/losyme/mongo"
"forge.chapril.org/losyme/uuid"
"go.mongodb.org/mongo-driver/mongo"
"forge.chapril.org/dune/dune/internal/components"
"forge.chapril.org/dune/dune/internal/storage"
)
type mongoStorage struct {
client *mongo.Client
db *mongo.Database
logger *logger.Logger
}
func loadConfig(cs *components.Components) (*_mgo.Config, error) {
cfg := new(_mgo.Config)
if err := cs.Config().Decode(cfg, nil, "storage", "mongo"); err != nil {
return nil, err
}
if cfg.Password != "" {
password, err := cs.Crypto().DecryptString(cfg.Password)
if err != nil {
return nil, err
}
cfg.Password = password
}
return cfg, nil
}
func New(cs *components.Components) (storage.Storage, error) {
logger, err := cs.Logger().NewLogger(uuid.New(), "storage")
if err != nil {
return nil, err
}
cfg, err := loadConfig(cs)
if err != nil {
return nil, err
}
client, err := _mgo.Connect(cfg)
if err != nil {
return nil, err
}
database := client.Database(cs.Application().Name())
ms := &mongoStorage{
client: client,
db: database,
logger: logger,
}
return ms, nil
}
func (ms *mongoStorage) Collection(name string) *mongo.Collection {
return ms.db.Collection(name)
}
func (ms *mongoStorage) cJobs() *mongo.Collection {
return ms.Collection("jobs")
}
func (ms *mongoStorage) cWorkflows() *mongo.Collection {
return ms.Collection("workflows")
}
func (ms *mongoStorage) State() (*storage.State, error) {
return nil, errors.NotImplemented()
}
func (ms *mongoStorage) Close() error {
return _mgo.Disconnect(ms.client)
}
/*
######################################################################################################## @(°_°)@ #######
*/