AFAIRE
sdk
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.

128 lines
2.8 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021-2022 losyme ################################################# MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package client
import (
"encoding/json"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/roundrobin"
"forge.chapril.org/losyme/uuid"
)
type Client struct {
endpoints *roundrobin.AllList
logger Logger
}
func noEndpoint(reqID string) error {
return errors.New("unable to execute this request", "request", reqID) //////////////////////////////////////////////
}
func (c *Client) CreateJob(job *jw.Job) (*jw.Job, error) {
if job.ID == "" {
job.ID = uuid.New()
}
content, err := json.Marshal(job)
if err != nil {
return nil, err
}
reqID := job.ID
for _, item := range c.endpoints.Slice() {
ep := item.(*endpoint)
if !ep.isReady() {
continue
}
if c.logger != nil {
c.logger.Debug( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Create job",
"request", reqID,
"endpoint", ep.URL,
)
}
job, err := ep.createJob(reqID, content)
if err == nil {
return job, nil
}
if c.logger != nil {
c.logger.Warning( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Create job",
"request", reqID,
"endpoint", ep.URL,
nil, err,
)
}
if !errors.Is(err, _errRetry) {
return nil, err
}
}
return nil, noEndpoint(reqID)
}
func (c *Client) NextJob(namespace string) (*jw.Job, error) {
reqID := uuid.New()
for _, item := range c.endpoints.Slice() {
ep := item.(*endpoint)
if !ep.isReady() {
continue
}
if c.logger != nil {
c.logger.Trace( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Next job",
"request", reqID,
"endpoint", ep.URL,
)
}
job, err := ep.nextJob(reqID, namespace)
if err == nil {
return job, nil
}
if c.logger != nil {
c.logger.Warning( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Next job",
"request", reqID,
"endpoint", ep.URL,
nil, err,
)
}
if !errors.Is(err, _errRetry) {
return nil, err
}
}
return nil, noEndpoint(reqID)
}
func (c *Client) UpdateJob(job *jw.Job) (*jw.Job, error) {
return job, nil
}
func (c *Client) CreateWorkflow(wf *jw.Workflow) (*jw.Workflow, error) {
if wf.ID == "" {
wf.ID = uuid.New()
}
return nil, errors.NotImplemented()
}
/*
######################################################################################################## @(°_°)@ #######
*/