From 752e6bd5cf4d573d9cdffb0cbe1cdb4bef764b99 Mon Sep 17 00:00:00 2001 From: Matt Kaye Date: Mon, 19 May 2025 12:52:41 -0400 Subject: [PATCH] Feat: Add event key to CEL env for filtering (#1748) * feat: add event key to env * chore: generate a bunch of stuff again * feat: add a test for filtering by event key * refactor: duped code * fix: skip flaky test in ci * Fix: Add filter payloads to context (#1743) * feat: add payload to ctx * chore: ver * fix: add filter payload to go * fix: tests + lint * fix: lint * fix: naming --- .../src/next/lib/docs/generated/blog/_meta.ts | 4 ++ .../lib/docs/generated/snips/go/run/event.ts | 4 +- .../snips/go/z_v0/assignment-affinity/run.ts | 2 +- .../snips/go/z_v0/assignment-sticky/run.ts | 2 +- .../snips/go/z_v0/cancellation/run.ts | 2 +- .../generated/snips/go/z_v0/compute/main.ts | 2 +- .../docs/generated/snips/go/z_v0/dag/main.ts | 2 +- .../snips/go/z_v0/deprecated/requeue/main.ts | 2 +- .../z_v0/deprecated/schedule-timeout/main.ts | 2 +- .../snips/go/z_v0/deprecated/timeout/main.ts | 2 +- .../snips/go/z_v0/deprecated/yaml/main.ts | 2 +- .../snips/go/z_v0/errors-test/main.ts | 2 +- .../cancel-in-progress/main.ts | 2 +- .../group-round-robin/main.ts | 2 +- .../generated/snips/go/z_v0/logging/main.ts | 2 +- .../generated/snips/go/z_v0/middleware/run.ts | 2 +- .../snips/go/z_v0/namespaced/main.ts | 2 +- .../snips/go/z_v0/register-action/main.ts | 2 +- .../generated/snips/go/z_v0/retries/main.ts | 2 +- .../generated/snips/go/z_v0/simple/main.ts | 2 +- .../generated/snips/go/z_v0/timeout/run.ts | 2 +- .../generated/snips/go/z_v0/webhook/run.ts | 2 +- internal/cel/cel.go | 7 +++ pkg/repository/v1/trigger.go | 1 + pkg/worker/context.go | 8 +++ pkg/worker/middleware_test.go | 4 ++ sdks/python/examples/events/test_event.py | 59 +++++++++++++++++-- sdks/python/examples/events/worker.py | 3 +- .../python/examples/priority/test_priority.py | 3 + sdks/typescript/package.json | 2 +- .../src/v1/client/worker/context.ts | 8 +++ 31 files changed, 113 insertions(+), 30 deletions(-) diff --git a/frontend/app/src/next/lib/docs/generated/blog/_meta.ts b/frontend/app/src/next/lib/docs/generated/blog/_meta.ts index a2cab700c..39f592278 100644 --- a/frontend/app/src/next/lib/docs/generated/blog/_meta.ts +++ b/frontend/app/src/next/lib/docs/generated/blog/_meta.ts @@ -1,5 +1,9 @@ // Generated from frontend/docs/pages/blog/_meta.js const meta = { + fastest_postgres_inserts: { + title: 'The fastest Postgres inserts', + href: '/blog/fastest-postgres-inserts', + }, task_queue_modern_python: { title: 'A task queue for modern Python applications', href: '/blog/task-queue-modern-python', diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/run/event.ts b/frontend/app/src/next/lib/docs/generated/snips/go/run/event.ts index cf4cf9ce0..c6e75a58b 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/run/event.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/run/event.ts @@ -3,12 +3,12 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\n\tv1_workflows 'github.com/hatchet-dev/hatchet/examples/go/workflows'\n\tv1 'github.com/hatchet-dev/hatchet/pkg/v1'\n\t'github.com/joho/godotenv'\n)\n\nfunc event() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\thatchet, err := v1.NewHatchetClient()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t// > Pushing an Event\n\terr = hatchet.Events().Push(\n\t\tcontext.Background(),\n\t\t'simple-event:create',\n\t\tv1_workflows.SimpleInput{\n\t\t\tMessage: 'Hello, World!',\n\t\t},\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\n\tv1_workflows 'github.com/hatchet-dev/hatchet/examples/go/workflows'\n\tv1 'github.com/hatchet-dev/hatchet/pkg/v1'\n\t'github.com/joho/godotenv'\n)\n\nfunc event() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\thatchet, err := v1.NewHatchetClient()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t// > Pushing an Event\n\terr = hatchet.Events().Push(\n\t\tcontext.Background(),\n\t\t'simple-event:create',\n\t\tv1_workflows.SimpleInput{\n\t\t\tMessage: 'Hello, World!',\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n", source: 'out/go/run/event.go', blocks: { pushing_an_event: { start: 23, - stop: 31, + stop: 29, }, }, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-affinity/run.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-affinity/run.ts index f77f461b5..1019392b5 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-affinity/run.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-affinity/run.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/types'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run() (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t\tworker.WithLabels(map[string]interface{}{\n\t\t\t'model': 'fancy-ai-model-v2',\n\t\t\t'memory': 1024,\n\t\t}),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:affinity'),\n\t\t\tName: 'affinity',\n\t\t\tDescription: 'affinity',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\n\t\t\t\t\tmodel := ctx.Worker().GetLabels()['model']\n\n\t\t\t\t\tif model != 'fancy-ai-model-v3' {\n\t\t\t\t\t\tctx.Worker().UpsertLabels(map[string]interface{}{\n\t\t\t\t\t\t\t'model': nil,\n\t\t\t\t\t\t})\n\t\t\t\t\t\t// Do something to load the model\n\t\t\t\t\t\tctx.Worker().UpsertLabels(map[string]interface{}{\n\t\t\t\t\t\t\t'model': 'fancy-ai-model-v3',\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).\n\t\t\t\t\tSetName('step-one').\n\t\t\t\t\tSetDesiredLabels(map[string]*types.DesiredWorkerLabel{\n\t\t\t\t\t\t'model': {\n\t\t\t\t\t\t\tValue: 'fancy-ai-model-v3',\n\t\t\t\t\t\t\tWeight: 10,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t'memory': {\n\t\t\t\t\t\t\tValue: 512,\n\t\t\t\t\t\t\tRequired: true,\n\t\t\t\t\t\t\tComparator: types.ComparatorPtr(types.WorkerLabelComparator_GREATER_THAN),\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:affinity',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(10 * time.Second)\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/types'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run() (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t\tworker.WithLabels(map[string]interface{}{\n\t\t\t'model': 'fancy-ai-model-v2',\n\t\t\t'memory': 1024,\n\t\t}),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:affinity'),\n\t\t\tName: 'affinity',\n\t\t\tDescription: 'affinity',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\n\t\t\t\t\tmodel := ctx.Worker().GetLabels()['model']\n\n\t\t\t\t\tif model != 'fancy-ai-model-v3' {\n\t\t\t\t\t\tctx.Worker().UpsertLabels(map[string]interface{}{\n\t\t\t\t\t\t\t'model': nil,\n\t\t\t\t\t\t})\n\t\t\t\t\t\t// Do something to load the model\n\t\t\t\t\t\tctx.Worker().UpsertLabels(map[string]interface{}{\n\t\t\t\t\t\t\t'model': 'fancy-ai-model-v3',\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).\n\t\t\t\t\tSetName('step-one').\n\t\t\t\t\tSetDesiredLabels(map[string]*types.DesiredWorkerLabel{\n\t\t\t\t\t\t'model': {\n\t\t\t\t\t\t\tValue: 'fancy-ai-model-v3',\n\t\t\t\t\t\t\tWeight: 10,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t'memory': {\n\t\t\t\t\t\t\tValue: 512,\n\t\t\t\t\t\t\tRequired: true,\n\t\t\t\t\t\t\tComparator: types.ComparatorPtr(types.WorkerLabelComparator_GREATER_THAN),\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:affinity',\n\t\t\ttestEvent,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(10 * time.Second)\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/assignment-affinity/run.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-sticky/run.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-sticky/run.ts index 17f91a4ba..84cdab5d2 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-sticky/run.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/assignment-sticky/run.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/types'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run() (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\t// > StickyWorker\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:sticky'),\n\t\t\tName: 'sticky',\n\t\t\tDescription: 'sticky',\n\t\t\t// 👀 Specify a sticky strategy when declaring the workflow\n\t\t\tStickyStrategy: types.StickyStrategyPtr(types.StickyStrategy_HARD),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\n\t\t\t\t\tsticky := true\n\n\t\t\t\t\t_, err = ctx.SpawnWorkflow('sticky-child', nil, &worker.SpawnWorkflowOpts{\n\t\t\t\t\t\tSticky: &sticky,\n\t\t\t\t\t})\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, fmt.Errorf('error spawning workflow: %w', err)\n\t\t\t\t\t}\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-three').AddParents('step-two'),\n\t\t\t},\n\t\t},\n\t)\n\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\t// > StickyChild\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tName: 'sticky-child',\n\t\t\tDescription: 'sticky',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:sticky',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(10 * time.Second)\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/types'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run() (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\t// > StickyWorker\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:sticky'),\n\t\t\tName: 'sticky',\n\t\t\tDescription: 'sticky',\n\t\t\t// 👀 Specify a sticky strategy when declaring the workflow\n\t\t\tStickyStrategy: types.StickyStrategyPtr(types.StickyStrategy_HARD),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\n\t\t\t\t\tsticky := true\n\n\t\t\t\t\t_, err = ctx.SpawnWorkflow('sticky-child', nil, &worker.SpawnWorkflowOpts{\n\t\t\t\t\t\tSticky: &sticky,\n\t\t\t\t\t})\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, fmt.Errorf('error spawning workflow: %w', err)\n\t\t\t\t\t}\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-three').AddParents('step-two'),\n\t\t\t},\n\t\t},\n\t)\n\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\t// > StickyChild\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tName: 'sticky-child',\n\t\t\tDescription: 'sticky',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: ctx.Worker().ID(),\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:sticky',\n\t\t\ttestEvent,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(10 * time.Second)\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/assignment-sticky/run.go', blocks: { stickyworker: { diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/cancellation/run.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/cancellation/run.ts index 906e0225f..1326acbe9 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/cancellation/run.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/cancellation/run.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/google/uuid'\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/rest'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:cancellation'),\n\t\t\tName: 'cancellation',\n\t\t\tDescription: 'cancellation',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tselect {\n\t\t\t\t\tcase <-ctx.Done():\n\t\t\t\t\t\tevents <- 'done'\n\t\t\t\t\t\tlog.Printf('context cancelled')\n\t\t\t\t\t\treturn nil, nil\n\t\t\t\t\tcase <-time.After(30 * time.Second):\n\t\t\t\t\t\tlog.Printf('workflow never cancelled')\n\t\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\t\tMessage: 'done',\n\t\t\t\t\t\t}, nil\n\t\t\t\t\t}\n\t\t\t\t}).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:cancellation',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(10 * time.Second)\n\n\t\tworkflowName := 'cancellation'\n\n\t\tworkflows, err := c.API().WorkflowListWithResponse(context.Background(), uuid.MustParse(c.TenantId()), &rest.WorkflowListParams{\n\t\t\tName: &workflowName,\n\t\t})\n\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error listing workflows: %w', err))\n\t\t}\n\n\t\tif workflows.JSON200 == nil {\n\t\t\tpanic(fmt.Errorf('no workflows found'))\n\t\t}\n\n\t\trows := *workflows.JSON200.Rows\n\n\t\tif len(rows) == 0 {\n\t\t\tpanic(fmt.Errorf('no workflows found'))\n\t\t}\n\n\t\tworkflowId := uuid.MustParse(rows[0].Metadata.Id)\n\n\t\tworkflowRuns, err := c.API().WorkflowRunListWithResponse(context.Background(), uuid.MustParse(c.TenantId()), &rest.WorkflowRunListParams{\n\t\t\tWorkflowId: &workflowId,\n\t\t})\n\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error listing workflow runs: %w', err))\n\t\t}\n\n\t\tif workflowRuns.JSON200 == nil {\n\t\t\tpanic(fmt.Errorf('no workflow runs found'))\n\t\t}\n\n\t\tworkflowRunsRows := *workflowRuns.JSON200.Rows\n\n\t\t_, err = c.API().WorkflowRunCancelWithResponse(context.Background(), uuid.MustParse(c.TenantId()), rest.WorkflowRunsCancelRequest{\n\t\t\tWorkflowRunIds: []uuid.UUID{uuid.MustParse(workflowRunsRows[0].Metadata.Id)},\n\t\t})\n\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error cancelling workflow run: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/google/uuid'\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/rest'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:cancellation'),\n\t\t\tName: 'cancellation',\n\t\t\tDescription: 'cancellation',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tselect {\n\t\t\t\t\tcase <-ctx.Done():\n\t\t\t\t\t\tevents <- 'done'\n\t\t\t\t\t\tlog.Printf('context cancelled')\n\t\t\t\t\t\treturn nil, nil\n\t\t\t\t\tcase <-time.After(30 * time.Second):\n\t\t\t\t\t\tlog.Printf('workflow never cancelled')\n\t\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\t\tMessage: 'done',\n\t\t\t\t\t\t}, nil\n\t\t\t\t\t}\n\t\t\t\t}).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:cancellation',\n\t\t\ttestEvent,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(10 * time.Second)\n\n\t\tworkflowName := 'cancellation'\n\n\t\tworkflows, err := c.API().WorkflowListWithResponse(context.Background(), uuid.MustParse(c.TenantId()), &rest.WorkflowListParams{\n\t\t\tName: &workflowName,\n\t\t})\n\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error listing workflows: %w', err))\n\t\t}\n\n\t\tif workflows.JSON200 == nil {\n\t\t\tpanic(fmt.Errorf('no workflows found'))\n\t\t}\n\n\t\trows := *workflows.JSON200.Rows\n\n\t\tif len(rows) == 0 {\n\t\t\tpanic(fmt.Errorf('no workflows found'))\n\t\t}\n\n\t\tworkflowId := uuid.MustParse(rows[0].Metadata.Id)\n\n\t\tworkflowRuns, err := c.API().WorkflowRunListWithResponse(context.Background(), uuid.MustParse(c.TenantId()), &rest.WorkflowRunListParams{\n\t\t\tWorkflowId: &workflowId,\n\t\t})\n\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error listing workflow runs: %w', err))\n\t\t}\n\n\t\tif workflowRuns.JSON200 == nil {\n\t\t\tpanic(fmt.Errorf('no workflow runs found'))\n\t\t}\n\n\t\tworkflowRunsRows := *workflowRuns.JSON200.Rows\n\n\t\t_, err = c.API().WorkflowRunCancelWithResponse(context.Background(), uuid.MustParse(c.TenantId()), rest.WorkflowRunsCancelRequest{\n\t\t\tWorkflowRunIds: []uuid.UUID{uuid.MustParse(workflowRunsRows[0].Metadata.Id)},\n\t\t})\n\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error cancelling workflow run: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/cancellation/run.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/compute/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/compute/main.ts index 3202ba7bc..ccadee28b 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/compute/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/compute/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/compute'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\tpool := 'test-pool'\n\tbasicCompute := compute.Compute{\n\t\tPool: &pool,\n\t\tNumReplicas: 1,\n\t\tCPUs: 1,\n\t\tMemoryMB: 1024,\n\t\tCPUKind: compute.ComputeKindSharedCPU,\n\t\tRegions: []compute.Region{compute.Region('ewr')},\n\t}\n\n\tperformancePool := 'performance-pool'\n\tperformanceCompute := compute.Compute{\n\t\tPool: &performancePool,\n\t\tNumReplicas: 1,\n\t\tCPUs: 2,\n\t\tMemoryMB: 1024,\n\t\tCPUKind: compute.ComputeKindPerformanceCPU,\n\t\tRegions: []compute.Region{compute.Region('ewr')},\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:simple'),\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Expression('input.user_id'),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one').SetCompute(&basicCompute),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one').SetCompute(&performanceCompute),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:simple',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t\tclient.WithEventMetadata(map[string]string{\n\t\t\t\t'hello': 'world',\n\t\t\t}),\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/compute'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\tpool := 'test-pool'\n\tbasicCompute := compute.Compute{\n\t\tPool: &pool,\n\t\tNumReplicas: 1,\n\t\tCPUs: 1,\n\t\tMemoryMB: 1024,\n\t\tCPUKind: compute.ComputeKindSharedCPU,\n\t\tRegions: []compute.Region{compute.Region('ewr')},\n\t}\n\n\tperformancePool := 'performance-pool'\n\tperformanceCompute := compute.Compute{\n\t\tPool: &performancePool,\n\t\tNumReplicas: 1,\n\t\tCPUs: 2,\n\t\tMemoryMB: 1024,\n\t\tCPUKind: compute.ComputeKindPerformanceCPU,\n\t\tRegions: []compute.Region{compute.Region('ewr')},\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:simple'),\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Expression('input.user_id'),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one').SetCompute(&basicCompute),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one').SetCompute(&performanceCompute),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:simple',\n\t\t\ttestEvent,\n\t\t\tclient.WithEventMetadata(map[string]string{\n\t\t\t\t'hello': 'world',\n\t\t\t}),\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/compute/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/dag/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/dag/main.ts index 79c6c50a9..565fe761b 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/dag/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/dag/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t\tworker.WithMaxRuns(1),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:simple'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'post-user-update',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\t\t\t\t\tctx.WorkflowInput(input)\n\n\t\t\t\t\ttime.Sleep(1 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 1 got username: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\t\t\t\t\tctx.WorkflowInput(input)\n\n\t\t\t\t\ttime.Sleep(2 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 2 got username: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\t\t\t\t\tctx.WorkflowInput(input)\n\n\t\t\t\t\tstep1Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-one', step1Out)\n\n\t\t\t\t\tstep2Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-two', step2Out)\n\n\t\t\t\t\ttime.Sleep(3 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Username was: ' + input.Username + ', Step 3: has parents 1 and 2' + step1Out.Message + ', ' + step2Out.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-three').AddParents('step-one', 'step-two'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tstep1Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-one', step1Out)\n\n\t\t\t\t\tstep3Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-three', step3Out)\n\n\t\t\t\t\ttime.Sleep(4 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 4: has parents 1 and 3' + step1Out.Message + ', ' + step3Out.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-four').AddParents('step-one', 'step-three'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tstep4Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-four', step4Out)\n\n\t\t\t\t\ttime.Sleep(5 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 5: has parent 4' + step4Out.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-five').AddParents('step-four'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserID: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\tlog.Printf('pushing event user:create:simple')\n\n\t// push an event\n\terr = c.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create:simple',\n\t\ttestEvent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error pushing event: %w', err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\treturn cleanup()\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t\tworker.WithMaxRuns(1),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:simple'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'post-user-update',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\t\t\t\t\tctx.WorkflowInput(input)\n\n\t\t\t\t\ttime.Sleep(1 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 1 got username: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\t\t\t\t\tctx.WorkflowInput(input)\n\n\t\t\t\t\ttime.Sleep(2 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 2 got username: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\t\t\t\t\tctx.WorkflowInput(input)\n\n\t\t\t\t\tstep1Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-one', step1Out)\n\n\t\t\t\t\tstep2Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-two', step2Out)\n\n\t\t\t\t\ttime.Sleep(3 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Username was: ' + input.Username + ', Step 3: has parents 1 and 2' + step1Out.Message + ', ' + step2Out.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-three').AddParents('step-one', 'step-two'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tstep1Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-one', step1Out)\n\n\t\t\t\t\tstep3Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-three', step3Out)\n\n\t\t\t\t\ttime.Sleep(4 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 4: has parents 1 and 3' + step1Out.Message + ', ' + step3Out.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-four').AddParents('step-one', 'step-three'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {\n\t\t\t\t\tstep4Out := &stepOutput{}\n\t\t\t\t\tctx.StepOutput('step-four', step4Out)\n\n\t\t\t\t\ttime.Sleep(5 * time.Second)\n\n\t\t\t\t\treturn &stepOutput{\n\t\t\t\t\t\tMessage: 'Step 5: has parent 4' + step4Out.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-five').AddParents('step-four'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserID: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\tlog.Printf('pushing event user:create:simple')\n\n\t// push an event\n\terr = c.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create:simple',\n\t\ttestEvent,\n\t)\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error pushing event: %w', err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\treturn cleanup()\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", source: 'out/go/z_v0/dag/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/requeue/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/requeue/main.ts index aecd4847b..3575ca6fe 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/requeue/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/requeue/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype sampleEvent struct{}\n\ntype requeueInput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = worker.RegisterAction('requeue:requeue', func(ctx context.Context, input *requeueInput) (result any, err error) {\n\t\treturn map[string]interface{}{}, nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())\n\tdefer cancel()\n\n\tevent := sampleEvent{}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'example:event',\n\t\tevent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// wait to register the worker for 10 seconds, to let the requeuer kick in\n\ttime.Sleep(10 * time.Second)\n\tcleanup, err := worker.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\t\treturn\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype sampleEvent struct{}\n\ntype requeueInput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = worker.RegisterAction('requeue:requeue', func(ctx context.Context, input *requeueInput) (result any, err error) {\n\t\treturn map[string]interface{}{}, nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())\n\tdefer cancel()\n\n\tevent := sampleEvent{}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'example:event',\n\t\tevent,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// wait to register the worker for 10 seconds, to let the requeuer kick in\n\ttime.Sleep(10 * time.Second)\n\tcleanup, err := worker.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\t\treturn\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", source: 'out/go/z_v0/deprecated/requeue/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/schedule-timeout/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/schedule-timeout/main.ts index 41c364562..42f1450ca 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/schedule-timeout/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/schedule-timeout/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/joho/godotenv'\n)\n\ntype sampleEvent struct{}\n\ntype timeoutInput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevent := sampleEvent{}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\tevent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\ttime.Sleep(35 * time.Second)\n\n\tfmt.Println('step should have timed out')\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/joho/godotenv'\n)\n\ntype sampleEvent struct{}\n\ntype timeoutInput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevent := sampleEvent{}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\tevent,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\ttime.Sleep(35 * time.Second)\n\n\tfmt.Println('step should have timed out')\n}\n", source: 'out/go/z_v0/deprecated/schedule-timeout/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/timeout/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/timeout/main.ts index 99d942159..652bca398 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/timeout/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/timeout/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype sampleEvent struct{}\n\ntype timeoutInput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = worker.RegisterAction('timeout:timeout', func(ctx context.Context, input *timeoutInput) (result any, err error) {\n\t\t// wait for context done signal\n\t\ttimeStart := time.Now().UTC()\n\t\t<-ctx.Done()\n\t\tfmt.Println('context cancelled in ', time.Since(timeStart).Seconds(), ' seconds')\n\n\t\treturn map[string]interface{}{}, nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())\n\tdefer cancel()\n\n\tcleanup, err := worker.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf('error starting worker: %w', err))\n\t}\n\n\tevent := sampleEvent{}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\tevent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\t\treturn\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype sampleEvent struct{}\n\ntype timeoutInput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = worker.RegisterAction('timeout:timeout', func(ctx context.Context, input *timeoutInput) (result any, err error) {\n\t\t// wait for context done signal\n\t\ttimeStart := time.Now().UTC()\n\t\t<-ctx.Done()\n\t\tfmt.Println('context cancelled in ', time.Since(timeStart).Seconds(), ' seconds')\n\n\t\treturn map[string]interface{}{}, nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())\n\tdefer cancel()\n\n\tcleanup, err := worker.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf('error starting worker: %w', err))\n\t}\n\n\tevent := sampleEvent{}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\tevent,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\t\treturn\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", source: 'out/go/z_v0/deprecated/timeout/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/yaml/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/yaml/main.ts index d82118c78..86a65e1d4 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/yaml/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/deprecated/yaml/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserId string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype actionInput struct {\n\tMessage string `json:'message'`\n}\n\ntype actionOut struct {\n\tMessage string `json:'message'`\n}\n\nfunc echo(ctx context.Context, input *actionInput) (result *actionOut, err error) {\n\treturn &actionOut{\n\t\tMessage: input.Message,\n\t}, nil\n}\n\nfunc object(ctx context.Context, input *userCreateEvent) error {\n\treturn nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\techoSvc := worker.NewService('echo')\n\n\terr = echoSvc.RegisterAction(echo)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = echoSvc.RegisterAction(object)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tch := cmdutils.InterruptChan()\n\n\tcleanup, err := worker.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf('error starting worker: %w', err))\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserId: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\ttime.Sleep(1 * time.Second)\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\ttestEvent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up worker: %w', err))\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserId string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype actionInput struct {\n\tMessage string `json:'message'`\n}\n\ntype actionOut struct {\n\tMessage string `json:'message'`\n}\n\nfunc echo(ctx context.Context, input *actionInput) (result *actionOut, err error) {\n\treturn &actionOut{\n\t\tMessage: input.Message,\n\t}, nil\n}\n\nfunc object(ctx context.Context, input *userCreateEvent) error {\n\treturn nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New(\n\t\tclient.InitWorkflows(),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\techoSvc := worker.NewService('echo')\n\n\terr = echoSvc.RegisterAction(echo)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = echoSvc.RegisterAction(object)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tch := cmdutils.InterruptChan()\n\n\tcleanup, err := worker.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf('error starting worker: %w', err))\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserId: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\ttime.Sleep(1 * time.Second)\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\ttestEvent,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up worker: %w', err))\n\t}\n}\n", source: 'out/go/z_v0/deprecated/yaml/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/errors-test/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/errors-test/main.ts index 6aadefd66..9bff5a47f 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/errors-test/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/errors-test/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'os'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/errors/sentry'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserId string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc StepOne(ctx context.Context) (result *stepOneOutput, err error) {\n\treturn nil, fmt.Errorf('this is an error')\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tsentryAlerter, err := sentry.NewSentryAlerter(&sentry.SentryAlerterOpts{\n\t\tDSN: os.Getenv('SENTRY_DSN'),\n\t\tEnvironment: os.Getenv('SENTRY_ENVIRONMENT'),\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t\tworker.WithErrorAlerter(sentryAlerter),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.On(worker.Event('user:create'), &worker.WorkflowJob{\n\t\tName: 'failing-workflow',\n\t\tDescription: 'This is a failing workflow.',\n\t\tSteps: []*worker.WorkflowStep{\n\t\t\t{\n\t\t\t\tFunction: StepOne,\n\t\t\t},\n\t\t},\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// err = worker.RegisterAction('echo:echo', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn map[string]interface{}{\n\t// \t\t'message': input.Message,\n\t// \t}, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\t// err = worker.RegisterAction('echo:object', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn nil, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\tch := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf('error starting worker: %w', err))\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserId: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\ttestEvent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'os'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/errors/sentry'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserId string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc StepOne(ctx context.Context) (result *stepOneOutput, err error) {\n\treturn nil, fmt.Errorf('this is an error')\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tsentryAlerter, err := sentry.NewSentryAlerter(&sentry.SentryAlerterOpts{\n\t\tDSN: os.Getenv('SENTRY_DSN'),\n\t\tEnvironment: os.Getenv('SENTRY_ENVIRONMENT'),\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t\tworker.WithErrorAlerter(sentryAlerter),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.On(worker.Event('user:create'), &worker.WorkflowJob{\n\t\tName: 'failing-workflow',\n\t\tDescription: 'This is a failing workflow.',\n\t\tSteps: []*worker.WorkflowStep{\n\t\t\t{\n\t\t\t\tFunction: StepOne,\n\t\t\t},\n\t\t},\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// err = worker.RegisterAction('echo:echo', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn map[string]interface{}{\n\t// \t\t'message': input.Message,\n\t// \t}, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\t// err = worker.RegisterAction('echo:object', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn nil, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\tch := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf('error starting worker: %w', err))\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserId: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\ttestEvent,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n", source: 'out/go/z_v0/errors-test/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/cancel-in-progress/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/cancel-in-progress/main.ts index 0c9c0b612..4db5d75f5 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/cancel-in-progress/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/cancel-in-progress/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype concurrencyLimitEvent struct {\n\tIndex int `json:'index'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn 'user-create', nil\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('concurrency-test-event'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'concurrency-limit',\n\t\t\tDescription: 'This limits concurrency to 1 run at a time.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey).MaxRuns(1),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\t<-ctx.Done()\n\t\t\t\t\tfmt.Println('context done, returning')\n\t\t\t\t\treturn nil, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\tgo func() {\n\t\t// sleep with interrupt context\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\t\treturn\n\t\tcase <-time.After(2 * time.Second): // timeout\n\t\t}\n\n\t\tfirstEvent := concurrencyLimitEvent{\n\t\t\tIndex: 0,\n\t\t}\n\n\t\t// push an event\n\t\terr = c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'concurrency-test-event',\n\t\t\tfirstEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\treturn\n\t\tcase <-time.After(10 * time.Second): // timeout\n\t\t}\n\n\t\t// push a second event\n\t\terr = c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'concurrency-test-event',\n\t\t\tconcurrencyLimitEvent{\n\t\t\t\tIndex: 1,\n\t\t\t},\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\treturn nil\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype concurrencyLimitEvent struct {\n\tIndex int `json:'index'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn 'user-create', nil\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('concurrency-test-event'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'concurrency-limit',\n\t\t\tDescription: 'This limits concurrency to 1 run at a time.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey).MaxRuns(1),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\t<-ctx.Done()\n\t\t\t\t\tfmt.Println('context done, returning')\n\t\t\t\t\treturn nil, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\tgo func() {\n\t\t// sleep with interrupt context\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\t\treturn\n\t\tcase <-time.After(2 * time.Second): // timeout\n\t\t}\n\n\t\tfirstEvent := concurrencyLimitEvent{\n\t\t\tIndex: 0,\n\t\t}\n\n\t\t// push an event\n\t\terr = c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'concurrency-test-event',\n\t\t\tfirstEvent,\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\treturn\n\t\tcase <-time.After(10 * time.Second): // timeout\n\t\t}\n\n\t\t// push a second event\n\t\terr = c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'concurrency-test-event',\n\t\t\tconcurrencyLimitEvent{\n\t\t\t\tIndex: 1,\n\t\t\t},\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\treturn nil\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", source: 'out/go/z_v0/limit-concurrency/cancel-in-progress/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/group-round-robin/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/group-round-robin/main.ts index d9dd8dc05..e24a32c98 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/group-round-robin/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/limit-concurrency/group-round-robin/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/types'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype concurrencyLimitEvent struct {\n\tUserId int `json:'user_id'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\tinput := &concurrencyLimitEvent{}\n\terr := ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn '', fmt.Errorf('error getting input: %w', err)\n\t}\n\n\treturn fmt.Sprintf('%d', input.UserId), nil\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('concurrency-test-event-rr'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'concurrency-limit-round-robin',\n\t\t\tDescription: 'This limits concurrency to 2 runs at a time.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey).MaxRuns(2).LimitStrategy(types.GroupRoundRobin),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &concurrencyLimitEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, fmt.Errorf('error getting input: %w', err)\n\t\t\t\t\t}\n\n\t\t\t\t\tfmt.Println('received event', input.UserId)\n\n\t\t\t\t\ttime.Sleep(5 * time.Second)\n\n\t\t\t\t\tfmt.Println('processed event', input.UserId)\n\n\t\t\t\t\treturn nil, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\tgo func() {\n\t\t// sleep with interrupt context\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\treturn\n\t\tcase <-time.After(2 * time.Second): // timeout\n\t\t}\n\n\t\tfor i := 0; i < 20; i++ {\n\t\t\tvar event concurrencyLimitEvent\n\n\t\t\tif i < 10 {\n\t\t\t\tevent = concurrencyLimitEvent{0}\n\t\t\t} else {\n\t\t\t\tevent = concurrencyLimitEvent{1}\n\t\t\t}\n\n\t\t\tc.Event().Push(\n\t\t\t\tcontext.Background(),\n\t\t\t\t'concurrency-test-event-rr',\n\t\t\t\tevent,\n\t\t\t\tnil,\n\t\t\t\tnil,\n\t\t\t)\n\t\t}\n\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\treturn\n\t\tcase <-time.After(10 * time.Second): //timeout\n\t\t}\n\t}()\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\treturn fmt.Errorf('error cleaning up: %w', err)\n\t\t\t}\n\t\t\treturn nil\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/types'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype concurrencyLimitEvent struct {\n\tUserId int `json:'user_id'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\tinput := &concurrencyLimitEvent{}\n\terr := ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn '', fmt.Errorf('error getting input: %w', err)\n\t}\n\n\treturn fmt.Sprintf('%d', input.UserId), nil\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('concurrency-test-event-rr'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'concurrency-limit-round-robin',\n\t\t\tDescription: 'This limits concurrency to 2 runs at a time.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey).MaxRuns(2).LimitStrategy(types.GroupRoundRobin),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &concurrencyLimitEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, fmt.Errorf('error getting input: %w', err)\n\t\t\t\t\t}\n\n\t\t\t\t\tfmt.Println('received event', input.UserId)\n\n\t\t\t\t\ttime.Sleep(5 * time.Second)\n\n\t\t\t\t\tfmt.Println('processed event', input.UserId)\n\n\t\t\t\t\treturn nil, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\tgo func() {\n\t\t// sleep with interrupt context\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\treturn\n\t\tcase <-time.After(2 * time.Second): // timeout\n\t\t}\n\n\t\tfor i := 0; i < 20; i++ {\n\t\t\tvar event concurrencyLimitEvent\n\n\t\t\tif i < 10 {\n\t\t\t\tevent = concurrencyLimitEvent{0}\n\t\t\t} else {\n\t\t\t\tevent = concurrencyLimitEvent{1}\n\t\t\t}\n\n\t\t\tc.Event().Push(context.Background(), 'concurrency-test-event-rr', event)\n\t\t}\n\n\t\tselect {\n\t\tcase <-interruptCtx.Done(): // context cancelled\n\t\t\tfmt.Println('interrupted')\n\t\t\treturn\n\t\tcase <-time.After(10 * time.Second): //timeout\n\t\t}\n\t}()\n\n\tfor {\n\t\tselect {\n\t\tcase <-interruptCtx.Done():\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\treturn fmt.Errorf('error cleaning up: %w', err)\n\t\t\t}\n\t\t\treturn nil\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", source: 'out/go/z_v0/limit-concurrency/group-round-robin/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/logging/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/logging/main.ts index b0acbb4dc..7b6403519 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/logging/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/logging/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:log:simple'),\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Expression('input.user_id'),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\tfor i := 0; i < 1000; i++ {\n\t\t\t\t\t\tctx.Log(fmt.Sprintf('step-one: %d', i))\n\t\t\t\t\t}\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:log:simple',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t\tclient.WithEventMetadata(map[string]string{\n\t\t\t\t'hello': 'world',\n\t\t\t}),\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:log:simple'),\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Expression('input.user_id'),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\tfor i := 0; i < 1000; i++ {\n\t\t\t\t\t\tctx.Log(fmt.Sprintf('step-one: %d', i))\n\t\t\t\t\t}\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:log:simple',\n\t\t\ttestEvent,\n\t\t\tclient.WithEventMetadata(map[string]string{\n\t\t\t\t'hello': 'world',\n\t\t\t}),\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/logging/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/middleware/run.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/middleware/run.ts index bc69a40ac..5cdfd8280 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/middleware/run.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/middleware/run.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\tw.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tlog.Printf('1st-middleware')\n\t\tevents <- '1st-middleware'\n\t\tctx.SetContext(context.WithValue(ctx.GetContext(), 'testkey', 'testvalue'))\n\t\treturn next(ctx)\n\t})\n\n\tw.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tlog.Printf('2nd-middleware')\n\t\tevents <- '2nd-middleware'\n\n\t\t// time the function duration\n\t\tstart := time.Now()\n\t\terr := next(ctx)\n\t\tduration := time.Since(start)\n\t\tfmt.Printf('step function took %s\\n', duration)\n\t\treturn err\n\t})\n\n\ttestSvc := w.NewService('test')\n\n\ttestSvc.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tevents <- 'svc-middleware'\n\t\tctx.SetContext(context.WithValue(ctx.GetContext(), 'svckey', 'svcvalue'))\n\t\treturn next(ctx)\n\t})\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:middleware'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'middleware',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\ttestVal := ctx.Value('testkey').(string)\n\t\t\t\t\tevents <- testVal\n\t\t\t\t\tsvcVal := ctx.Value('svckey').(string)\n\t\t\t\t\tevents <- svcVal\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event user:create:middleware')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:middleware',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\tw.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tlog.Printf('1st-middleware')\n\t\tevents <- '1st-middleware'\n\t\tctx.SetContext(context.WithValue(ctx.GetContext(), 'testkey', 'testvalue'))\n\t\treturn next(ctx)\n\t})\n\n\tw.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tlog.Printf('2nd-middleware')\n\t\tevents <- '2nd-middleware'\n\n\t\t// time the function duration\n\t\tstart := time.Now()\n\t\terr := next(ctx)\n\t\tduration := time.Since(start)\n\t\tfmt.Printf('step function took %s\\n', duration)\n\t\treturn err\n\t})\n\n\ttestSvc := w.NewService('test')\n\n\ttestSvc.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tevents <- 'svc-middleware'\n\t\tctx.SetContext(context.WithValue(ctx.GetContext(), 'svckey', 'svcvalue'))\n\t\treturn next(ctx)\n\t})\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:middleware'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'middleware',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\ttestVal := ctx.Value('testkey').(string)\n\t\t\t\t\tevents <- testVal\n\t\t\t\t\tsvcVal := ctx.Value('svckey').(string)\n\t\t\t\t\tevents <- svcVal\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event user:create:middleware')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:middleware',\n\t\t\ttestEvent,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/middleware/run.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/namespaced/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/namespaced/main.ts index 6941cfd6c..f362877f4 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/namespaced/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/namespaced/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn 'user-create', nil\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New(\n\t\tclient.WithNamespace('sample'),\n\t)\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:simple'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:simple',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn 'user-create', nil\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New(\n\t\tclient.WithNamespace('sample'),\n\t)\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:simple'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:simple',\n\t\t\ttestEvent,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/namespaced/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/register-action/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/register-action/main.ts index 15f15d8ca..5dbc49ee0 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/register-action/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/register-action/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserId string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc StepOne(ctx context.Context, input *userCreateEvent) (result *stepOneOutput, err error) {\n\t// could get from context\n\t// testVal := ctx.Value('testkey').(string)\n\t// svcVal := ctx.Value('svckey').(string)\n\n\treturn &stepOneOutput{\n\t\tMessage: 'Username is: ' + input.Username,\n\t}, nil\n}\n\nfunc StepTwo(ctx context.Context, input *stepOneOutput) (result *stepOneOutput, err error) {\n\treturn &stepOneOutput{\n\t\tMessage: 'Above message is: ' + input.Message,\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\ttestSvc.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tctx.SetContext(context.WithValue(ctx.GetContext(), 'testkey', 'testvalue'))\n\t\treturn next(ctx)\n\t})\n\n\terr = testSvc.RegisterAction(StepOne, worker.WithActionName('step-one'))\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = testSvc.RegisterAction(StepTwo, worker.WithActionName('step-two'))\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create', 'user:update'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'post-user-update',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\t// example of calling a registered action from the worker (includes service name)\n\t\t\t\tw.Call('test:step-one'),\n\t\t\t\t// example of calling a registered action from a service\n\t\t\t\ttestSvc.Call('step-two'),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// err = worker.RegisterAction('echo:echo', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn map[string]interface{}{\n\t// \t\t'message': input.Message,\n\t// \t}, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\t// err = worker.RegisterAction('echo:object', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn nil, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserId: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\ttestEvent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interrupt:\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'time'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserId string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc StepOne(ctx context.Context, input *userCreateEvent) (result *stepOneOutput, err error) {\n\t// could get from context\n\t// testVal := ctx.Value('testkey').(string)\n\t// svcVal := ctx.Value('svckey').(string)\n\n\treturn &stepOneOutput{\n\t\tMessage: 'Username is: ' + input.Username,\n\t}, nil\n}\n\nfunc StepTwo(ctx context.Context, input *stepOneOutput) (result *stepOneOutput, err error) {\n\treturn &stepOneOutput{\n\t\tMessage: 'Above message is: ' + input.Message,\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tclient, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tclient,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\ttestSvc.Use(func(ctx worker.HatchetContext, next func(worker.HatchetContext) error) error {\n\t\tctx.SetContext(context.WithValue(ctx.GetContext(), 'testkey', 'testvalue'))\n\t\treturn next(ctx)\n\t})\n\n\terr = testSvc.RegisterAction(StepOne, worker.WithActionName('step-one'))\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = testSvc.RegisterAction(StepTwo, worker.WithActionName('step-two'))\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create', 'user:update'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'post-user-update',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\t// example of calling a registered action from the worker (includes service name)\n\t\t\t\tw.Call('test:step-one'),\n\t\t\t\t// example of calling a registered action from a service\n\t\t\t\ttestSvc.Call('step-two'),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// err = worker.RegisterAction('echo:echo', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn map[string]interface{}{\n\t// \t\t'message': input.Message,\n\t// \t}, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\t// err = worker.RegisterAction('echo:object', func(ctx context.Context, input *actionInput) (result any, err error) {\n\t// \treturn nil, nil\n\t// })\n\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserId: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\t// push an event\n\terr = client.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create',\n\t\ttestEvent,\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor {\n\t\tselect {\n\t\tcase <-interrupt:\n\t\t\tif err := cleanup(); err != nil {\n\t\t\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t\t\t}\n\t\tdefault:\n\t\t\ttime.Sleep(time.Second)\n\t\t}\n\t}\n}\n", source: 'out/go/z_v0/register-action/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/retries/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/retries/main.ts index 2e9c1845e..1f97e94ee 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/retries/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/retries/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn 'user-create', nil\n}\n\ntype retryWorkflow struct {\n\tretries int\n}\n\nfunc (r *retryWorkflow) StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &userCreateEvent{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif r.retries < 2 {\n\t\tr.retries++\n\t\treturn nil, fmt.Errorf('error')\n\t}\n\n\tlog.Printf('finished step-one')\n\treturn &stepOneOutput{\n\t\tMessage: 'Username is: ' + input.Username,\n\t}, nil\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t\tworker.WithMaxRuns(1),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\twk := &retryWorkflow{}\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:simple'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(wk.StepOne).SetName('step-one').SetRetries(4),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserID: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\tlog.Printf('pushing event user:create:simple')\n\n\t// push an event\n\terr = c.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create:simple',\n\t\ttestEvent,\n\t\tnil,\n\t\tnil,\n\t)\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error pushing event: %w', err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\treturn fmt.Errorf('error cleaning up worker: %w', err)\n\t}\n\n\treturn nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tif err := run(cmdutils.InterruptChan(), events); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn 'user-create', nil\n}\n\ntype retryWorkflow struct {\n\tretries int\n}\n\nfunc (r *retryWorkflow) StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &userCreateEvent{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif r.retries < 2 {\n\t\tr.retries++\n\t\treturn nil, fmt.Errorf('error')\n\t}\n\n\tlog.Printf('finished step-one')\n\treturn &stepOneOutput{\n\t\tMessage: 'Username is: ' + input.Username,\n\t}, nil\n}\n\nfunc run(ch <-chan interface{}, events chan<- string) error {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t\tworker.WithMaxRuns(1),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\ttestSvc := w.NewService('test')\n\n\twk := &retryWorkflow{}\n\n\terr = testSvc.On(\n\t\tworker.Events('user:create:simple'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(wk.StepOne).SetName('step-one').SetRetries(4),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserID: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\tlog.Printf('pushing event user:create:simple')\n\n\t// push an event\n\terr = c.Event().Push(\n\t\tcontext.Background(),\n\t\t'user:create:simple',\n\t\ttestEvent,\n\t)\n\n\tif err != nil {\n\t\treturn fmt.Errorf('error pushing event: %w', err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\treturn fmt.Errorf('error cleaning up worker: %w', err)\n\t}\n\n\treturn nil\n}\n", source: 'out/go/z_v0/retries/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/simple/main.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/simple/main.ts index 4358ad8a8..4656fa2a0 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/simple/main.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/simple/main.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:simple'),\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Expression('input.user_id'),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:simple',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t\tclient.WithEventMetadata(map[string]string{\n\t\t\t\t'hello': 'world',\n\t\t\t}),\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\n\t'github.com/joho/godotenv'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/cmdutils'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\ntype userCreateEvent struct {\n\tUsername string `json:'username'`\n\tUserID string `json:'user_id'`\n\tData map[string]string `json:'data'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:'message'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tevents := make(chan string, 50)\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := run(events)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-interrupt\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf('error cleaning up: %w', err))\n\t}\n}\n\nfunc run(events chan<- string) (func() error, error) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events('user:create:simple'),\n\t\t\tName: 'simple',\n\t\t\tDescription: 'This runs after an update to the user model.',\n\t\t\tConcurrency: worker.Expression('input.user_id'),\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-one')\n\t\t\t\t\tevents <- 'step-one'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Username is: ' + input.Username,\n\t\t\t\t\t}, nil\n\t\t\t\t},\n\t\t\t\t).SetName('step-one'),\n\t\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\t\tinput := &stepOneOutput{}\n\t\t\t\t\terr = ctx.StepOutput('step-one', input)\n\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tlog.Printf('step-two')\n\t\t\t\t\tevents <- 'step-two'\n\n\t\t\t\t\treturn &stepOneOutput{\n\t\t\t\t\t\tMessage: 'Above message is: ' + input.Message,\n\t\t\t\t\t}, nil\n\t\t\t\t}).SetName('step-two').AddParents('step-one'),\n\t\t\t},\n\t\t},\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\tlog.Printf('pushing event user:create:simple')\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:simple',\n\t\t\ttestEvent,\n\t\t\tclient.WithEventMetadata(map[string]string{\n\t\t\t\t'hello': 'world',\n\t\t\t}),\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/simple/main.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/timeout/run.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/timeout/run.ts index 755d85cc0..1c58a3cca 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/timeout/run.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/timeout/run.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(done chan<- string, job worker.WorkflowJob) (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.On(\n\t\tworker.Events('user:create:timeout'),\n\t\t&job,\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:timeout',\n\t\t\ttestEvent,\n\t\t\tnil,\n\t\t\tnil,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(20 * time.Second)\n\n\t\tdone <- 'done'\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'fmt'\n\t'log'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(done chan<- string, job worker.WorkflowJob) (func() error, error) {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating client: %w', err)\n\t}\n\n\tw, err := worker.NewWorker(\n\t\tworker.WithClient(\n\t\t\tc,\n\t\t),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error creating worker: %w', err)\n\t}\n\n\terr = w.On(\n\t\tworker.Events('user:create:timeout'),\n\t\t&job,\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error registering workflow: %w', err)\n\t}\n\n\tgo func() {\n\t\tlog.Printf('pushing event')\n\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: 'echo-test',\n\t\t\tUserID: '1234',\n\t\t\tData: map[string]string{\n\t\t\t\t'test': 'test',\n\t\t\t},\n\t\t}\n\n\t\t// push an event\n\t\terr := c.Event().Push(\n\t\t\tcontext.Background(),\n\t\t\t'user:create:timeout',\n\t\t\ttestEvent,\n\t\t)\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf('error pushing event: %w', err))\n\t\t}\n\n\t\ttime.Sleep(20 * time.Second)\n\n\t\tdone <- 'done'\n\t}()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf('error starting worker: %w', err)\n\t}\n\n\treturn cleanup, nil\n}\n", source: 'out/go/z_v0/timeout/run.go', blocks: {}, highlights: {}, diff --git a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/webhook/run.ts b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/webhook/run.ts index 1f166fe44..1c16f57a3 100644 --- a/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/webhook/run.ts +++ b/frontend/app/src/next/lib/docs/generated/snips/go/z_v0/webhook/run.ts @@ -3,7 +3,7 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types'; const snippet: Snippet = { language: 'go', content: - "package main\n\nimport (\n\t'context'\n\t'errors'\n\t'fmt'\n\t'log'\n\t'net/http'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(\n\tname string,\n\tw *worker.Worker,\n\tport string,\n\thandler func(w http.ResponseWriter, r *http.Request), c client.Client, workflow string, event string,\n) error {\n\t// create webserver to handle webhook requests\n\tmux := http.NewServeMux()\n\n\t// Register the HelloHandler to the /hello route\n\tmux.HandleFunc('/webhook', handler)\n\n\t// Create a custom server\n\tserver := &http.Server{\n\t\tAddr: ':' + port,\n\t\tHandler: mux,\n\t\tReadTimeout: 10 * time.Second,\n\t\tWriteTimeout: 10 * time.Second,\n\t\tIdleTimeout: 15 * time.Second,\n\t}\n\n\tdefer func(server *http.Server, ctx context.Context) {\n\t\terr := server.Shutdown(ctx)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}(server, context.Background())\n\n\tgo func() {\n\t\tif err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\tsecret := 'secret'\n\tif err := w.RegisterWebhook(worker.RegisterWebhookWorkerOpts{\n\t\tName: 'test-' + name,\n\t\tURL: fmt.Sprintf('http://localhost:%s/webhook', port),\n\t\tSecret: &secret,\n\t}); err != nil {\n\t\treturn fmt.Errorf('error setting up webhook: %w', err)\n\t}\n\n\ttime.Sleep(30 * time.Second)\n\n\tlog.Printf('pushing event')\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserID: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\t// push an event\n\terr := c.Event().Push(\n\t\tcontext.Background(),\n\t\tevent,\n\t\ttestEvent,\n\t\tnil,\n\t\tnil,\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error pushing event: %w', err)\n\t}\n\n\ttime.Sleep(5 * time.Second)\n\n\treturn nil\n}\n", + "package main\n\nimport (\n\t'context'\n\t'errors'\n\t'fmt'\n\t'log'\n\t'net/http'\n\t'time'\n\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/worker'\n)\n\nfunc run(\n\tname string,\n\tw *worker.Worker,\n\tport string,\n\thandler func(w http.ResponseWriter, r *http.Request), c client.Client, workflow string, event string,\n) error {\n\t// create webserver to handle webhook requests\n\tmux := http.NewServeMux()\n\n\t// Register the HelloHandler to the /hello route\n\tmux.HandleFunc('/webhook', handler)\n\n\t// Create a custom server\n\tserver := &http.Server{\n\t\tAddr: ':' + port,\n\t\tHandler: mux,\n\t\tReadTimeout: 10 * time.Second,\n\t\tWriteTimeout: 10 * time.Second,\n\t\tIdleTimeout: 15 * time.Second,\n\t}\n\n\tdefer func(server *http.Server, ctx context.Context) {\n\t\terr := server.Shutdown(ctx)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}(server, context.Background())\n\n\tgo func() {\n\t\tif err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\tsecret := 'secret'\n\tif err := w.RegisterWebhook(worker.RegisterWebhookWorkerOpts{\n\t\tName: 'test-' + name,\n\t\tURL: fmt.Sprintf('http://localhost:%s/webhook', port),\n\t\tSecret: &secret,\n\t}); err != nil {\n\t\treturn fmt.Errorf('error setting up webhook: %w', err)\n\t}\n\n\ttime.Sleep(30 * time.Second)\n\n\tlog.Printf('pushing event')\n\n\ttestEvent := userCreateEvent{\n\t\tUsername: 'echo-test',\n\t\tUserID: '1234',\n\t\tData: map[string]string{\n\t\t\t'test': 'test',\n\t\t},\n\t}\n\n\t// push an event\n\terr := c.Event().Push(\n\t\tcontext.Background(),\n\t\tevent,\n\t\ttestEvent,\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf('error pushing event: %w', err)\n\t}\n\n\ttime.Sleep(5 * time.Second)\n\n\treturn nil\n}\n", source: 'out/go/z_v0/webhook/run.go', blocks: {}, highlights: {}, diff --git a/internal/cel/cel.go b/internal/cel/cel.go index fce73e71a..ce02be2aa 100644 --- a/internal/cel/cel.go +++ b/internal/cel/cel.go @@ -74,6 +74,7 @@ func NewCELParser() *CELParser { decls.NewVar("additional_metadata", decls.NewMapType(decls.String, decls.Dyn)), decls.NewVar("payload", decls.NewMapType(decls.String, decls.Dyn)), decls.NewVar("event_id", decls.String), + decls.NewVar("event_key", decls.String), checksumDecl, ), ) @@ -125,6 +126,12 @@ func WithEventID(eventID string) InputOpts { } } +func WithEventKey(key string) InputOpts { + return func(w Input) { + w["event_key"] = key + } +} + func NewInput(opts ...InputOpts) Input { res := make(map[string]interface{}) diff --git a/pkg/repository/v1/trigger.go b/pkg/repository/v1/trigger.go index d001cab11..681c461c9 100644 --- a/pkg/repository/v1/trigger.go +++ b/pkg/repository/v1/trigger.go @@ -1610,6 +1610,7 @@ func (r *sharedRepository) processWorkflowExpression(ctx context.Context, expres cel.WithAdditionalMetadata(additionalMetadata), cel.WithPayload(payload), cel.WithEventID(opt.ExternalId), + cel.WithEventKey(opt.Key), ), ) diff --git a/pkg/worker/context.go b/pkg/worker/context.go index 55b9b4e09..9137f3b8a 100644 --- a/pkg/worker/context.go +++ b/pkg/worker/context.go @@ -93,6 +93,8 @@ type HatchetContext interface { IncChildIndex() Priority() int32 + + FilterPayload() map[string]interface{} } type TriggeredBy string @@ -257,6 +259,12 @@ func (h *hatchetContext) UserData(target interface{}) error { return toTarget(h.stepData.UserData, target) } +func (h *hatchetContext) FilterPayload() map[string]interface{} { + payload := h.stepData.Triggers["filter_payload"] + + return payload +} + func (h *hatchetContext) AdditionalMetadata() map[string]string { return h.stepData.AdditionalMetadata } diff --git a/pkg/worker/middleware_test.go b/pkg/worker/middleware_test.go index 3072a4350..015d859f4 100644 --- a/pkg/worker/middleware_test.go +++ b/pkg/worker/middleware_test.go @@ -134,6 +134,10 @@ func (c *testHatchetContext) Worker() HatchetWorkerContext { panic("not implemented") } +func (c *testHatchetContext) FilterPayload() map[string]interface{} { + panic("not implemented") +} + func TestAddMiddleware(t *testing.T) { m := middlewares{} middlewareFunc := func(ctx HatchetContext, next func(HatchetContext) error) error { diff --git a/sdks/python/examples/events/test_event.py b/sdks/python/examples/events/test_event.py index d06313f25..6df5012db 100644 --- a/sdks/python/examples/events/test_event.py +++ b/sdks/python/examples/events/test_event.py @@ -7,7 +7,12 @@ from uuid import uuid4 import pytest from pydantic import BaseModel -from examples.events.worker import EventWorkflowInput, event_workflow +from examples.events.worker import ( + EVENT_KEY, + SECONDARY_KEY, + EventWorkflowInput, + event_workflow, +) from hatchet_sdk.clients.events import ( BulkPushEventOptions, BulkPushEventWithMetadata, @@ -140,7 +145,7 @@ def bpi( test_run_id: str = "", should_skip: bool = False, should_have_runs: bool = True, - key: str = "user:create", + key: str = EVENT_KEY, payload: dict[str, str] = {}, scope: str | None = None, ) -> BulkPushEventWithMetadata: @@ -165,14 +170,14 @@ def cp(should_skip: bool) -> dict[str, bool]: @pytest.mark.asyncio(loop_scope="session") async def test_event_push(hatchet: Hatchet) -> None: - e = hatchet.event.push("user:create", cp(False)) + e = hatchet.event.push(EVENT_KEY, cp(False)) assert e.eventId is not None @pytest.mark.asyncio(loop_scope="session") async def test_async_event_push(hatchet: Hatchet) -> None: - e = await hatchet.event.aio_push("user:create", cp(False)) + e = await hatchet.event.aio_push(EVENT_KEY, cp(False)) assert e.eventId is not None @@ -338,7 +343,7 @@ async def test_event_payload_filtering(hatchet: Hatchet, test_run_id: str) -> No {"foobar": "qux"}, ): event = await hatchet.event.aio_push( - event_key="user:create", + event_key=EVENT_KEY, payload={"message": "This is event 1", "should_skip": False}, options=PushEventOptions( scope=test_run_id, @@ -365,7 +370,7 @@ async def test_event_payload_filtering_with_payload_match( {"foobar": "baz"}, ): event = await hatchet.event.aio_push( - event_key="user:create", + event_key=EVENT_KEY, payload={"message": "This is event 1", "should_skip": False}, options=PushEventOptions( scope=test_run_id, @@ -378,3 +383,45 @@ async def test_event_payload_filtering_with_payload_match( ) runs = await wait_for_result(hatchet, [event]) assert len(runs) == 1 + + +@pytest.mark.asyncio(loop_scope="session") +async def test_filtering_by_event_key(hatchet: Hatchet, test_run_id: str) -> None: + async with event_filter( + hatchet, + test_run_id, + f"event_key == '{SECONDARY_KEY}'", + ): + event_1 = await hatchet.event.aio_push( + event_key=SECONDARY_KEY, + payload={ + "message": "Should run because filter matches", + "should_skip": False, + }, + options=PushEventOptions( + scope=test_run_id, + additional_metadata={ + "should_have_runs": True, + "test_run_id": test_run_id, + }, + ), + ) + event_2 = await hatchet.event.aio_push( + event_key=EVENT_KEY, + payload={ + "message": "Should skip because filter does not match", + "should_skip": False, + }, + options=PushEventOptions( + scope=test_run_id, + additional_metadata={ + "should_have_runs": False, + "test_run_id": test_run_id, + }, + ), + ) + + event_to_runs = await wait_for_result(hatchet, [event_1, event_2]) + + for event, runs in event_to_runs.items(): + await assert_event_runs_processed(event, runs) diff --git a/sdks/python/examples/events/worker.py b/sdks/python/examples/events/worker.py index ff46a0fcc..3cbccec29 100644 --- a/sdks/python/examples/events/worker.py +++ b/sdks/python/examples/events/worker.py @@ -4,6 +4,7 @@ from hatchet_sdk import Context, Hatchet hatchet = Hatchet() EVENT_KEY = "user:create" +SECONDARY_KEY = "foobarbaz" class EventWorkflowInput(BaseModel): @@ -13,7 +14,7 @@ class EventWorkflowInput(BaseModel): # > Event trigger event_workflow = hatchet.workflow( name="EventWorkflow", - on_events=[EVENT_KEY], + on_events=[EVENT_KEY, SECONDARY_KEY], input_validator=EventWorkflowInput, ) # !! diff --git a/sdks/python/examples/priority/test_priority.py b/sdks/python/examples/priority/test_priority.py index 4790dfb30..09c7e54dd 100644 --- a/sdks/python/examples/priority/test_priority.py +++ b/sdks/python/examples/priority/test_priority.py @@ -283,6 +283,9 @@ def time_until_next_minute() -> float: return (next_minute - now).total_seconds() +@pytest.mark.skip( + reason="Test is flaky because the first jobs that are picked up don't necessarily go in priority order" +) @pytest.mark.parametrize( "on_demand_worker", [ diff --git a/sdks/typescript/package.json b/sdks/typescript/package.json index 4ab7850e4..b26c88cd9 100644 --- a/sdks/typescript/package.json +++ b/sdks/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@hatchet-dev/typescript-sdk", - "version": "1.6.0", + "version": "1.6.1", "description": "Background task orchestration & visibility for developers", "types": "dist/index.d.ts", "files": [ diff --git a/sdks/typescript/src/v1/client/worker/context.ts b/sdks/typescript/src/v1/client/worker/context.ts index 9d5d9affc..cec20541a 100644 --- a/sdks/typescript/src/v1/client/worker/context.ts +++ b/sdks/typescript/src/v1/client/worker/context.ts @@ -155,6 +155,14 @@ export class Context { return this.data.triggers; } + /** + * Gets the payload from the filter that matched when triggering the event. + * @returns The payload. + */ + filterPayload(): Record { + return this.data.triggers?.filter_payload || {}; + } + /** * Determines if the workflow was triggered by an event. * @returns True if the workflow was triggered by an event, otherwise false.