2024-02-16 21:31:00 +00:00
|
|
|
package temporal
|
2024-02-16 12:07:29 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-18 21:37:17 +00:00
|
|
|
"context"
|
2024-02-16 13:50:35 +00:00
|
|
|
"time"
|
|
|
|
|
2024-02-16 12:07:29 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/config"
|
2024-02-18 21:37:17 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/jwt"
|
2024-02-16 13:50:35 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/pkg/retry"
|
2024-02-19 09:09:30 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-02-16 12:07:29 +00:00
|
|
|
"go.temporal.io/sdk/client"
|
|
|
|
)
|
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
type AuthHeadersProvider struct {
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AuthHeadersProvider) GetHeaders(ctx context.Context) (map[string]string, error) {
|
|
|
|
return map[string]string{
|
|
|
|
"authorization": "Bearer " + p.Token,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
func ConnectServerToTemporal(cfg *config.ServerConfig) (client.Client, error) {
|
2024-02-18 21:37:17 +00:00
|
|
|
// For server we generate new token with admin permissions
|
2024-02-19 09:09:30 +00:00
|
|
|
token, err := jwt.NewTokenForServer(cfg.Jwt.PrivateKey, cfg.Jwt.PublicKey)
|
2024-02-18 21:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := &AuthHeadersProvider{token}
|
|
|
|
|
|
|
|
// Try to connect to the Temporal Server
|
2024-02-21 22:15:21 +00:00
|
|
|
return retry.Retry(10, 3*time.Second, func() (client.Client, error) {
|
2024-02-18 21:37:17 +00:00
|
|
|
return client.Dial(client.Options{
|
|
|
|
HostPort: cfg.Temporal.ServerHost,
|
|
|
|
HeadersProvider: provider,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
func ConnectWorkerToTemporal(token string, temporalHost string, identity string) (client.Client, error) {
|
|
|
|
provider := &AuthHeadersProvider{token}
|
2024-02-18 21:37:17 +00:00
|
|
|
|
2024-02-16 13:50:35 +00:00
|
|
|
// Try to connect to the Temporal Server
|
|
|
|
return retry.Retry(5, 6*time.Second, func() (client.Client, error) {
|
2024-02-19 09:09:30 +00:00
|
|
|
client, err := client.Dial(client.Options{
|
|
|
|
HostPort: temporalHost,
|
2024-02-18 21:37:17 +00:00
|
|
|
HeadersProvider: provider,
|
|
|
|
Namespace: "default",
|
2024-02-19 09:09:30 +00:00
|
|
|
Identity: identity,
|
2024-02-16 13:50:35 +00:00
|
|
|
})
|
2024-02-19 09:09:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to connect to Temporal Server: "+temporalHost)
|
|
|
|
}
|
|
|
|
return client, nil
|
2024-02-16 13:50:35 +00:00
|
|
|
})
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|