Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
task
Manage
Activity
Members
Labels
Plan
Issues
2
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gaia-X
Trust Services API
task
Commits
f46a6add
Commit
f46a6add
authored
1 year ago
by
Yordan Kinkov
Browse files
Options
Downloads
Patches
Plain Diff
Subscribe for NATS cache events
parent
858bfc25
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
cmd/task/main.go
+17
-0
17 additions, 0 deletions
cmd/task/main.go
internal/clients/event/client.go
+60
-0
60 additions, 0 deletions
internal/clients/event/client.go
internal/config/config.go
+6
-0
6 additions, 0 deletions
internal/config/config.go
with
83 additions
and
0 deletions
cmd/task/main.go
+
17
−
0
View file @
f46a6add
...
...
@@ -32,6 +32,7 @@ import (
goatask
"gitlab.eclipse.org/eclipse/xfsc/tsa/task/gen/task"
goatasklist
"gitlab.eclipse.org/eclipse/xfsc/tsa/task/gen/task_list"
"gitlab.eclipse.org/eclipse/xfsc/tsa/task/internal/clients/cache"
"gitlab.eclipse.org/eclipse/xfsc/tsa/task/internal/clients/event"
"gitlab.eclipse.org/eclipse/xfsc/tsa/task/internal/clients/policy"
"gitlab.eclipse.org/eclipse/xfsc/tsa/task/internal/config"
"gitlab.eclipse.org/eclipse/xfsc/tsa/task/internal/executor"
...
...
@@ -91,6 +92,17 @@ func main() {
// create cache client
cache
:=
cache
.
New
(
cfg
.
Cache
.
Addr
,
cache
.
WithHTTPClient
(
oauthClient
))
var
events
*
event
.
Client
if
cfg
.
Nats
.
Addr
!=
""
{
events
,
err
=
event
.
New
(
cfg
.
Nats
.
Addr
,
cfg
.
Nats
.
Subject
)
if
err
!=
nil
{
logger
.
Fatal
(
"failed to create events client"
,
zap
.
Error
(
err
))
}
defer
events
.
Close
(
context
.
Background
())
//nolint:errcheck
}
else
{
logger
.
Info
(
"task service is not able to subscribe for cache events"
)
}
// create task executor
executor
:=
executor
.
New
(
storage
,
...
...
@@ -213,6 +225,11 @@ func main() {
g
.
Go
(
func
()
error
{
return
listExecutor
.
Start
(
ctx
)
})
if
events
!=
nil
{
g
.
Go
(
func
()
error
{
return
events
.
Start
(
ctx
)
})
}
if
err
:=
g
.
Wait
();
err
!=
nil
{
logger
.
Error
(
"run group stopped"
,
zap
.
Error
(
err
))
}
...
...
This diff is collapsed.
Click to expand it.
internal/clients/event/client.go
0 → 100644
+
60
−
0
View file @
f46a6add
package
event
import
(
"context"
"encoding/json"
"fmt"
"github.com/cloudevents/sdk-go/protocol/nats/v2"
cloudevents
"github.com/cloudevents/sdk-go/v2"
)
type
Client
struct
{
consumer
*
nats
.
Consumer
events
cloudevents
.
Client
}
func
New
(
addr
,
subject
string
)
(
*
Client
,
error
)
{
// create cloudevents NATS consumer
// other protocol implementations: https://github.com/cloudevents/sdk-go/tree/main/protocol
c
,
err
:=
nats
.
NewConsumer
(
addr
,
subject
,
nats
.
NatsOptions
())
if
err
!=
nil
{
return
nil
,
err
}
e
,
err
:=
cloudevents
.
NewClient
(
c
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
Client
{
consumer
:
c
,
events
:
e
,
},
nil
}
func
(
c
*
Client
)
Start
(
ctx
context
.
Context
)
error
{
for
{
if
err
:=
c
.
events
.
StartReceiver
(
ctx
,
handler
);
err
!=
nil
{
return
err
}
}
}
func
(
c
*
Client
)
Close
(
ctx
context
.
Context
)
error
{
return
c
.
consumer
.
Close
(
ctx
)
}
// handler is an example implementation.
// Implementation will be done in https://gitlab.eclipse.org/eclipse/xfsc/tsa/task/-/issues/7
func
handler
(
_
context
.
Context
,
event
cloudevents
.
Event
)
error
{
fmt
.
Printf
(
"Got Event Context: %+v
\n
"
,
event
.
Context
)
var
data
map
[
string
]
interface
{}
if
err
:=
json
.
Unmarshal
(
event
.
Data
(),
&
data
);
err
!=
nil
{
fmt
.
Printf
(
"Got Data Error: %s
\n
"
,
err
.
Error
())
}
fmt
.
Printf
(
"Got Data: %+v
\n
"
,
data
)
return
nil
}
This diff is collapsed.
Click to expand it.
internal/config/config.go
+
6
−
0
View file @
f46a6add
...
...
@@ -12,6 +12,7 @@ type Config struct {
Cache
cacheConfig
Metrics
metricsConfig
OAuth
oauthConfig
Nats
natsConfig
LogLevel
string
`envconfig:"LOG_LEVEL" default:"INFO"`
}
...
...
@@ -64,3 +65,8 @@ type oauthConfig struct {
ClientSecret
string
`envconfig:"OAUTH_CLIENT_SECRET"`
TokenURL
string
`envconfig:"OAUTH_TOKEN_URL"`
}
type
natsConfig
struct
{
Addr
string
`envconfig:"NATS_ADDR"`
Subject
string
`envconfig:"NATS_SUBJECT" default:"external"`
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment