Skip to content

Commit

Permalink
feat: handle readExperiments
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKujawa committed Nov 25, 2022
1 parent 042122d commit aa9382a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions go-chaos/cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
worker "github.com/zeebe-io/zeebe-chaos/go-chaos/worker"
)

const jobType = "zbchaos"
const jobTypeZbChaos = "zbchaos"
const jobTypeReadExperiments = "readExperiments"

func init() {
rootCmd.AddCommand(workerCommand)
Expand Down Expand Up @@ -54,7 +55,8 @@ func start_worker(cmd *cobra.Command, args []string) {
}

// Allow only one job at a time, otherwise job handling might interfere (e.g. override global vars)
jobWorker := client.NewJobWorker().JobType(jobType).Handler(handleZbChaosJob).MaxJobsActive(1).Open()
jobWorker := client.NewJobWorker().JobType(jobTypeZbChaos).Handler(handleZbChaosJob).MaxJobsActive(1).Open()
client.NewJobWorker().JobType(jobTypeReadExperiments).Handler(handleZbChaosJob).MaxJobsActive(1).Open()
jobWorker.AwaitClose()
}

Expand Down
32 changes: 32 additions & 0 deletions go-chaos/worker/chaos_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/camunda/zeebe/clients/go/v8/pkg/entities"
"github.com/camunda/zeebe/clients/go/v8/pkg/worker"
chaos_experiments "github.com/zeebe-io/zeebe-chaos/go-chaos/internal/chaos-experiments"
)

type CommandRunner func([]string, context.Context) error
Expand All @@ -39,6 +40,7 @@ type AuthenticationProvider struct {
}

type ZbChaosVariables struct {
ClusterPlan *string
ClusterId *string
Provider ChaosProvider
AuthenticationDetails AuthenticationProvider
Expand Down Expand Up @@ -74,3 +76,33 @@ func HandleZbChaosJob(client worker.JobClient, job entities.Job, commandRunner C

_, _ = client.NewCompleteJobCommand().JobKey(job.Key).Send(ctx)
}

func HandleReadExperiments(client worker.JobClient, job entities.Job) {
ctx := context.Background()

jobVariables := ZbChaosVariables{
Provider: ChaosProvider{
Timeout: 15 * 60, // 15 minute default Timeout
},
}
err := job.GetVariablesAs(&jobVariables)
if err != nil {
// Can't parse variables, no sense in retrying
_, _ = client.NewFailJobCommand().JobKey(job.Key).Retries(0).Send(ctx)
return
}

experiments, err := chaos_experiments.ReadExperimentsForClusterPlan(*jobVariables.ClusterPlan)
if err != nil {
_, _ = client.NewFailJobCommand().JobKey(job.Key).Retries(0).ErrorMessage(err.Error()).Send(ctx)
return
}

command, err := client.NewCompleteJobCommand().JobKey(job.Key).VariablesFromObject(experiments)
if err != nil {
_, _ = client.NewFailJobCommand().JobKey(job.Key).Retries(0).ErrorMessage(err.Error()).Send(ctx)
return
}

_, _ = command.Send(ctx)
}

0 comments on commit aa9382a

Please sign in to comment.