Skip to content

Commit 1a6734e

Browse files
committed
chore(worker): create temporal namespace at launch
1 parent 59981c6 commit 1a6734e

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

cmd/worker/main.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strconv"
8+
"strings"
9+
"time"
710

811
"go.opentelemetry.io/otel"
12+
"go.temporal.io/api/workflowservice/v1"
913
"go.temporal.io/sdk/client"
1014
"go.temporal.io/sdk/worker"
1115

12-
custom_otel "github.com/instill-ai/mgmt-backend/pkg/logger/otel"
1316
"github.com/instill-ai/x/temporal"
1417
"github.com/instill-ai/x/zapadapter"
1518

1619
"github.com/instill-ai/mgmt-backend/config"
1720
"github.com/instill-ai/mgmt-backend/pkg/logger"
1821

22+
custom_otel "github.com/instill-ai/mgmt-backend/pkg/logger/otel"
1923
mgmtWorker "github.com/instill-ai/mgmt-backend/pkg/worker"
2024
)
2125

@@ -78,6 +82,30 @@ func main() {
7882
}
7983
defer temporalClient.Close()
8084

85+
if _, err := temporalClient.WorkflowService().RegisterNamespace(ctx,
86+
&workflowservice.RegisterNamespaceRequest{
87+
Namespace: "mgmt-backend",
88+
WorkflowExecutionRetentionPeriod: func() *time.Duration {
89+
// Check if the string ends with "d" for day.
90+
s := config.Config.Temporal.Retention
91+
if strings.HasSuffix(s, "d") {
92+
// Parse the number of days.
93+
days, err := strconv.Atoi(s[:len(s)-1])
94+
if err != nil {
95+
logger.Fatal(fmt.Sprintf("Unable to parse retention period in day: %s", err))
96+
}
97+
// Convert days to hours and then to a duration.
98+
t := time.Hour * 24 * time.Duration(days)
99+
return &t
100+
}
101+
logger.Fatal(fmt.Sprintf("Unable to parse retention period in day: %s", err))
102+
return nil
103+
}(),
104+
},
105+
); err != nil {
106+
logger.Fatal(fmt.Sprintf("Unable to register namespace: %s", err))
107+
}
108+
81109
w := worker.New(temporalClient, mgmtWorker.TaskQueue, worker.Options{
82110
MaxConcurrentActivityExecutionSize: 2,
83111
})

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type ServerConfig struct {
5252
type TemporalConfig struct {
5353
HostPort string `koanf:"hostport"`
5454
Namespace string `koanf:"namespace"`
55+
Retention string `koanf:"retention"`
5556
Ca string `koanf:"ca"`
5657
Cert string `koanf:"cert"`
5758
Key string `koanf:"key"`

config/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ openfga:
5454
temporal:
5555
hostport: temporal:7233
5656
namespace: mgmt-backend
57+
retention: 1d
5758
ca:
5859
cert:
5960
key:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
go.opentelemetry.io/otel/sdk v1.16.0
3333
go.opentelemetry.io/otel/sdk/metric v0.39.0
3434
go.opentelemetry.io/otel/trace v1.16.0
35+
go.temporal.io/api v1.16.0
3536
go.temporal.io/sdk v1.21.0
3637
go.uber.org/zap v1.24.0
3738
golang.org/x/crypto v0.14.0
@@ -106,7 +107,6 @@ require (
106107
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
107108
go.opentelemetry.io/otel/metric v1.16.0 // indirect
108109
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
109-
go.temporal.io/api v1.16.0 // indirect
110110
go.uber.org/atomic v1.10.0 // indirect
111111
go.uber.org/multierr v1.9.0 // indirect
112112
golang.org/x/mod v0.8.0 // indirect

0 commit comments

Comments
 (0)