NATS default sending new scheduler

This commit is contained in:
mr 2024-08-13 08:35:01 +02:00
parent 9f26896ab6
commit 0a1a93de9b
2 changed files with 45 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import (
w "cloud.o-forge.io/core/oc-lib/models/workflow"
tools "cloud.o-forge.io/core/oc-lib/tools"
beego "github.com/beego/beego/v2/server/web"
"github.com/nats-io/nats.go"
)
// Operations about workflow
@ -40,11 +41,44 @@ func (o *WorkflowController) Put() {
id := o.Ctx.Input.Param(":id")
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
caller := tools.NewHTTPCaller("", "", "", oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING))+"/oc/booking")
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKFLOW), res, id, caller)
data := oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKFLOW), res, id, caller)
data.Err += o.SetNATSPub(res, data)
o.Data["json"] = data
o.ServeJSON()
}
func (o *WorkflowController) SetNATSPub(res map[string]interface{}, data oclib.LibData) string {
if sched, ok := res["schedule"]; ok && sched != nil && data.Code == 200 {
workflow := data.ToWorkflow()
if workflow != nil && workflow.Schedule != nil {
// HERE NATS
nc, err := nats.Connect(nats.DefaultURL)
if err != nil {
return " -> Could not reach NATS server"
}
start := ""
if workflow.Schedule.Start != nil {
start = workflow.Schedule.Start.String()
}
end := ""
if workflow.Schedule.End != nil {
end = workflow.Schedule.End.String()
}
str_description, _ := json.Marshal(map[string]string{
"workflow_id": workflow.UUID,
"start_date": start,
"stop_date": end,
})
err = nc.Publish("workflowsUpdate", str_description)
time.Sleep(time.Second * 2)
if err != nil {
return " -> " + err.Error()
}
}
}
return ""
}
// @Title Create
// @Description create workflows
// @Param data body json true "body for data content (Json format)"
@ -54,7 +88,9 @@ func (o *WorkflowController) Post() {
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
caller := tools.NewHTTPCaller("", "", "", oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING))+"/oc/booking")
o.Data["json"] = oclib.StoreOne(oclib.LibDataEnum(oclib.WORKFLOW), res, caller)
data := oclib.StoreOne(oclib.LibDataEnum(oclib.WORKFLOW), res, caller)
data.Err += o.SetNATSPub(res, data)
o.Data["json"] = data
o.ServeJSON()
}

6
go.sum
View File

@ -404,6 +404,12 @@ github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nats-io/nats.go v1.36.0 h1:suEUPuWzTSse/XhESwqLxXGuj8vGRuPRoG7MoRN/qyU=
github.com/nats-io/nats.go v1.36.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/ogier/pflag v0.0.1/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=