2024-02-16 21:31:00 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2024-02-19 09:09:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2024-05-23 16:33:30 +00:00
|
|
|
"log/slog"
|
2024-02-19 09:09:30 +00:00
|
|
|
"net/http"
|
2024-02-21 09:41:20 +00:00
|
|
|
"time"
|
2024-02-19 09:09:30 +00:00
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
"github.com/mentos1386/zdravko/internal/config"
|
|
|
|
"github.com/mentos1386/zdravko/internal/temporal"
|
|
|
|
"github.com/mentos1386/zdravko/internal/worker/activities"
|
|
|
|
"github.com/mentos1386/zdravko/pkg/api"
|
|
|
|
"github.com/mentos1386/zdravko/pkg/retry"
|
2024-02-19 09:09:30 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-05-23 16:33:30 +00:00
|
|
|
"go.temporal.io/sdk/activity"
|
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"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConnectionConfig(token string, apiUrl string) (*ConnectionConfig, error) {
|
2024-02-21 22:15:21 +00:00
|
|
|
req, err := api.NewRequest(http.MethodGet, apiUrl+"/api/v1/workers/connect", token, nil)
|
2024-02-19 09:09:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-21 09:41:20 +00:00
|
|
|
return retry.Retry(10, 3*time.Second, func() (*ConnectionConfig, error) {
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to connect to API")
|
|
|
|
}
|
|
|
|
|
2024-02-23 11:18:02 +00:00
|
|
|
if res.StatusCode == http.StatusUnauthorized {
|
2024-02-24 21:07:49 +00:00
|
|
|
panic("WORKER_GROUP_TOKEN is invalid. Either it expired or the worker was removed!")
|
2024-02-23 11:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.Errorf("unexpected status code: %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
|
2024-02-21 09:41:20 +00:00
|
|
|
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-19 09:09:30 +00:00
|
|
|
}
|
|
|
|
|
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-05-23 16:33:30 +00:00
|
|
|
logger *slog.Logger
|
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{
|
2024-05-23 16:33:30 +00:00
|
|
|
cfg: cfg,
|
2024-05-29 14:39:50 +00:00
|
|
|
logger: slog.Default(),
|
2024-02-16 21:31:00 +00:00
|
|
|
}, 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
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
w.logger.Info("Worker Starting", "group", config.Group)
|
2024-02-19 09:09:30 +00:00
|
|
|
|
2024-02-24 21:07:49 +00:00
|
|
|
temporalClient, err := temporal.ConnectWorkerToTemporal(w.cfg.Token, config.Endpoint)
|
2024-02-16 21:31:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Worker
|
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
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
workerActivities := activities.NewActivities(w.cfg, w.logger)
|
2024-02-16 21:31:00 +00:00
|
|
|
|
|
|
|
// Register Activities
|
2024-05-23 16:33:30 +00:00
|
|
|
w.worker.RegisterActivityWithOptions(workerActivities.Check, activity.RegisterOptions{Name: temporal.ActivityCheckName})
|
2024-02-16 21:31:00 +00:00
|
|
|
|
|
|
|
return w.worker.Run(worker.InterruptCh())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Stop() error {
|
|
|
|
w.worker.Stop()
|
|
|
|
return nil
|
|
|
|
}
|