2024-02-16 21:31:00 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2024-02-19 09:09:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/activities"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/config"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/temporal"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/workflows"
|
2024-02-19 09:09:30 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-02-16 21:31:00 +00:00
|
|
|
"go.temporal.io/sdk/worker"
|
|
|
|
)
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
type ConnectionConfig struct {
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConnectionConfig(token string, apiUrl string) (*ConnectionConfig, error) {
|
|
|
|
url := apiUrl + "/api/v1/workers/connect"
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", "Bearer "+token)
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to connect to API")
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := ConnectionConfig{}
|
|
|
|
err = json.Unmarshal(body, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal connection config")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
type Worker struct {
|
|
|
|
worker worker.Worker
|
2024-02-19 09:09:30 +00:00
|
|
|
cfg *config.WorkerConfig
|
2024-02-16 21:31:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
func NewWorker(cfg *config.WorkerConfig) (*Worker, error) {
|
2024-02-16 21:31:00 +00:00
|
|
|
return &Worker{
|
|
|
|
cfg: cfg,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Name() string {
|
|
|
|
return "Temporal Worker"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Start() error {
|
2024-02-19 09:09:30 +00:00
|
|
|
config, err := getConnectionConfig(w.cfg.Token, w.cfg.ApiUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Worker slug:", config.Slug)
|
|
|
|
log.Println("Worker group:", config.Group)
|
|
|
|
|
|
|
|
temporalClient, err := temporal.ConnectWorkerToTemporal(w.cfg.Token, config.Endpoint, config.Slug)
|
2024-02-16 21:31:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Worker
|
|
|
|
// TODO: Maybe identify by region or something?
|
2024-02-19 09:09:30 +00:00
|
|
|
w.worker = worker.New(temporalClient, config.Group, worker.Options{})
|
2024-02-16 21:31:00 +00:00
|
|
|
|
|
|
|
// Register Workflows
|
2024-02-21 09:06:54 +00:00
|
|
|
w.worker.RegisterWorkflow(workflows.HealthcheckWorkflowDefinition)
|
2024-02-16 21:31:00 +00:00
|
|
|
|
|
|
|
// Register Activities
|
2024-02-21 09:06:54 +00:00
|
|
|
w.worker.RegisterActivity(activities.Healthcheck)
|
2024-02-16 21:31:00 +00:00
|
|
|
|
|
|
|
return w.worker.Run(worker.InterruptCh())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Stop() error {
|
|
|
|
w.worker.Stop()
|
|
|
|
return nil
|
|
|
|
}
|