2024-02-27 22:47:20 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-05-23 16:33:30 +00:00
|
|
|
"log/slog"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/mentos1386/zdravko/database"
|
|
|
|
"github.com/mentos1386/zdravko/internal/config"
|
|
|
|
"github.com/mentos1386/zdravko/internal/server/activities"
|
|
|
|
"github.com/mentos1386/zdravko/internal/server/workflows"
|
|
|
|
"github.com/mentos1386/zdravko/internal/temporal"
|
|
|
|
"go.temporal.io/sdk/activity"
|
2024-02-27 22:47:20 +00:00
|
|
|
"go.temporal.io/sdk/client"
|
2024-05-23 16:33:30 +00:00
|
|
|
temporalWorker "go.temporal.io/sdk/worker"
|
|
|
|
"go.temporal.io/sdk/workflow"
|
2024-02-27 22:47:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Worker struct {
|
2024-05-23 16:33:30 +00:00
|
|
|
worker temporalWorker.Worker
|
2024-02-27 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
func NewWorker(temporalClient client.Client, cfg *config.ServerConfig, logger *slog.Logger, db *sqlx.DB, kvStore database.KeyValueStore) *Worker {
|
|
|
|
worker := temporalWorker.New(temporalClient, temporal.TEMPORAL_SERVER_QUEUE, temporalWorker.Options{})
|
2024-02-27 22:47:20 +00:00
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
a := activities.NewActivities(cfg, logger, db, kvStore)
|
2024-02-27 22:47:20 +00:00
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
w := workflows.NewWorkflows()
|
2024-02-27 22:47:20 +00:00
|
|
|
|
|
|
|
// Register Workflows
|
2024-05-23 16:33:30 +00:00
|
|
|
worker.RegisterWorkflowWithOptions(w.CheckWorkflowDefinition, workflow.RegisterOptions{Name: temporal.WorkflowCheckName})
|
|
|
|
|
|
|
|
// Register Activities
|
|
|
|
worker.RegisterActivityWithOptions(a.TargetsFilter, activity.RegisterOptions{Name: temporal.ActivityTargetsFilterName})
|
2024-05-25 11:45:25 +00:00
|
|
|
worker.RegisterActivityWithOptions(a.AddTargetHistory, activity.RegisterOptions{Name: temporal.ActivityAddTargetHistoryName})
|
2024-02-27 22:47:20 +00:00
|
|
|
|
|
|
|
return &Worker{
|
2024-05-23 16:33:30 +00:00
|
|
|
worker: worker,
|
2024-02-27 22:47:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Start() error {
|
2024-05-23 16:33:30 +00:00
|
|
|
return w.worker.Run(temporalWorker.InterruptCh())
|
2024-02-27 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Stop() {
|
|
|
|
w.worker.Stop()
|
|
|
|
}
|