mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-02-15 04:39:26 -06:00
Feat: Event filtering docs (#1753)
* chore: gen yet again * feat: filters client + docs * fix: lint * fix: revert cruft * fix: trigger * fix: lint * fix: gen * Update frontend/docs/pages/home/run-on-event.mdx Co-authored-by: abelanger5 <belanger@sas.upenn.edu> * Update frontend/docs/pages/home/run-on-event.mdx Co-authored-by: abelanger5 <belanger@sas.upenn.edu> --------- Co-authored-by: abelanger5 <belanger@sas.upenn.edu>
This commit is contained in:
@@ -3,12 +3,24 @@ 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)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n",
|
||||
"package main\n\nimport (\n\t'context'\n\n\t'github.com/google/uuid'\n\t'github.com/joho/godotenv'\n\n\tv1_workflows 'github.com/hatchet-dev/hatchet/examples/go/workflows'\n\t'github.com/hatchet-dev/hatchet/pkg/client'\n\t'github.com/hatchet-dev/hatchet/pkg/client/rest'\n\tv1 'github.com/hatchet-dev/hatchet/pkg/v1'\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\t// > Create a filter\n\tpayload := map[string]interface{}{\n\t\t'main_character': 'Anna',\n\t\t'supporting_character': 'Stiva',\n\t\t'location': 'Moscow',\n\t}\n\n\t_, err = hatchet.Filters().Create(\n\t\tcontext.Background(),\n\t\trest.V1CreateFilterRequest{\n\t\t\tWorkflowId: uuid.New(),\n\t\t\tExpression: 'input.shouldSkip == false',\n\t\t\tScope: 'foobarbaz',\n\t\t\tPayload: &payload,\n\t\t},\n\t)\n\n\t// > Skip a run\n\tskipPayload := map[string]interface{}{\n\t\t'shouldSkip': true,\n\t}\n\tskipScope := 'foobarbaz'\n\thatchet.Events().Push(\n\t\tcontext.Background(),\n\t\t'simple-event:create',\n\t\tskipPayload,\n\t\tclient.WithFilterScope(&skipScope),\n\t)\n\n\t// > Trigger a run\n\ttriggerPayload := map[string]interface{}{\n\t\t'shouldSkip': true,\n\t}\n\ttriggerScope := 'foobarbaz'\n\thatchet.Events().Push(\n\t\tcontext.Background(),\n\t\t'simple-event:create',\n\t\ttriggerPayload,\n\t\tclient.WithFilterScope(&triggerScope),\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: 29,
|
||||
start: 27,
|
||||
stop: 33,
|
||||
},
|
||||
create_a_filter: {
|
||||
start: 36,
|
||||
stop: 50,
|
||||
},
|
||||
skip_a_run: {
|
||||
start: 53,
|
||||
stop: 62,
|
||||
},
|
||||
trigger_a_run: {
|
||||
start: 65,
|
||||
stop: 74,
|
||||
},
|
||||
},
|
||||
highlights: {},
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Snippet } from '@/next/lib/docs/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
language: 'python',
|
||||
content:
|
||||
"from examples.events.worker import EVENT_KEY, event_workflow\nfrom hatchet_sdk import Hatchet, PushEventOptions\n\nhatchet = Hatchet()\n\n# > Create a filter\nhatchet.filters.create(\n workflow_id=event_workflow.id,\n expression='input.should_skip == false',\n scope='foobarbaz',\n payload={\n 'main_character': 'Anna',\n 'supporting_character': 'Stiva',\n 'location': 'Moscow',\n },\n)\n\n# > Skip a run\nhatchet.event.push(\n event_key=EVENT_KEY,\n payload={\n 'should_skip': True,\n },\n options=PushEventOptions(\n scope='foobarbaz',\n ),\n)\n\n# > Trigger a run\nhatchet.event.push(\n event_key=EVENT_KEY,\n payload={\n 'should_skip': True,\n },\n options=PushEventOptions(\n scope='foobarbaz',\n ),\n)\n",
|
||||
source: 'out/python/events/filter.py',
|
||||
blocks: {
|
||||
create_a_filter: {
|
||||
start: 7,
|
||||
stop: 16,
|
||||
},
|
||||
skip_a_run: {
|
||||
start: 19,
|
||||
stop: 27,
|
||||
},
|
||||
trigger_a_run: {
|
||||
start: 30,
|
||||
stop: 38,
|
||||
},
|
||||
},
|
||||
highlights: {},
|
||||
}; // Then replace double quotes with single quotes
|
||||
|
||||
export default snippet;
|
||||
@@ -1,7 +1,9 @@
|
||||
import event from './event';
|
||||
import filter from './filter';
|
||||
import test_event from './test_event';
|
||||
import worker from './worker';
|
||||
|
||||
export { event };
|
||||
export { filter };
|
||||
export { test_event };
|
||||
export { worker };
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -3,12 +3,12 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types';
|
||||
const snippet: Snippet = {
|
||||
language: 'python',
|
||||
content:
|
||||
"from pydantic import BaseModel\n\nfrom hatchet_sdk import Context, Hatchet\n\nhatchet = Hatchet()\nEVENT_KEY = 'user:create'\n\n\nclass EventWorkflowInput(BaseModel):\n should_skip: bool\n\n\n# > Event trigger\nevent_workflow = hatchet.workflow(\n name='EventWorkflow',\n on_events=[EVENT_KEY],\n input_validator=EventWorkflowInput,\n)\n\n\n@event_workflow.task()\ndef task(input: EventWorkflowInput, ctx: Context) -> None:\n print('event received')\n\n\ndef main() -> None:\n worker = hatchet.worker(name='EventWorker', workflows=[event_workflow])\n\n worker.start()\n\n\nif __name__ == '__main__':\n main()\n",
|
||||
"from pydantic import BaseModel\n\nfrom hatchet_sdk import Context, Hatchet\n\nhatchet = Hatchet()\nEVENT_KEY = 'user:create'\nSECONDARY_KEY = 'foobarbaz'\n\n\nclass EventWorkflowInput(BaseModel):\n should_skip: bool\n\n\n# > Event trigger\nevent_workflow = hatchet.workflow(\n name='EventWorkflow',\n on_events=[EVENT_KEY, SECONDARY_KEY],\n input_validator=EventWorkflowInput,\n)\n\n\n@event_workflow.task()\ndef task(input: EventWorkflowInput, ctx: Context) -> None:\n print('event received')\n\n\ndef main() -> None:\n worker = hatchet.worker(name='EventWorker', workflows=[event_workflow])\n\n worker.start()\n\n\nif __name__ == '__main__':\n main()\n",
|
||||
source: 'out/python/events/worker.py',
|
||||
blocks: {
|
||||
event_trigger: {
|
||||
start: 14,
|
||||
stop: 18,
|
||||
start: 15,
|
||||
stop: 19,
|
||||
},
|
||||
},
|
||||
highlights: {},
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -2,7 +2,8 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
language: 'python',
|
||||
content: 'from examples.simple.worker import simple\n\nsimple.run()\n',
|
||||
content:
|
||||
'from examples.simple.worker import simple, non_blocking\n\nsimple.run_no_wait()\nnon_blocking.run_no_wait()\n',
|
||||
source: 'out/python/simple/trigger.py',
|
||||
blocks: {},
|
||||
highlights: {},
|
||||
|
||||
@@ -3,14 +3,9 @@ import { Snippet } from '@/next/lib/docs/generated/snips/types';
|
||||
const snippet: Snippet = {
|
||||
language: 'python',
|
||||
content:
|
||||
"# > Simple\n\nfrom hatchet_sdk import Context, EmptyModel, Hatchet\n\nhatchet = Hatchet(debug=True)\n\n\n@hatchet.task()\ndef simple(input: EmptyModel, ctx: Context) -> dict[str, str]:\n return {'result': 'Hello, world!'}\n\n\n@hatchet.durable_task()\ndef simple_durable(input: EmptyModel, ctx: Context) -> dict[str, str]:\n return {'result': 'Hello, world!'}\n\n\ndef main() -> None:\n worker = hatchet.worker('test-worker', workflows=[simple, simple_durable])\n worker.start()\n\n\n\nif __name__ == '__main__':\n main()\n",
|
||||
"from hatchet_sdk import Context, EmptyModel, Hatchet\nimport time\nimport asyncio\n\nhatchet = Hatchet(debug=True)\n\n\n@hatchet.task()\nasync def simple(input: EmptyModel, ctx: Context) -> dict[str, str]:\n for i in range(60):\n print(f'blocking task {i}')\n time.sleep(1)\n\n@hatchet.task()\nasync def non_blocking(input: EmptyModel, ctx: Context) -> dict[str, str]:\n for i in range(60):\n print(f'non-blocking task {i}')\n await asyncio.sleep(1)\n\n\ndef main() -> None:\n worker = hatchet.worker('test-worker', workflows=[simple, non_blocking])\n worker.start()\n\n\nif __name__ == '__main__':\n main()\n",
|
||||
source: 'out/python/simple/worker.py',
|
||||
blocks: {
|
||||
simple: {
|
||||
start: 2,
|
||||
stop: 22,
|
||||
},
|
||||
},
|
||||
blocks: {},
|
||||
highlights: {},
|
||||
}; // Then replace double quotes with single quotes
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Snippet } from '@/next/lib/docs/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
language: 'typescript ',
|
||||
content:
|
||||
"import { hatchet } from '../hatchet-client';\nimport { lower, SIMPLE_EVENT } from './workflow';\n\n// > Create a filter\nhatchet.filters.create({\n workflowId: lower.id,\n expression: 'input.ShouldSkip == false',\n scope: 'foobarbaz',\n payload: {\n main_character: 'Anna',\n supporting_character: 'Stiva',\n location: 'Moscow',\n },\n})\n\n// > Skip a run\nhatchet.events.push(\n SIMPLE_EVENT,\n {\n 'Message': 'hello',\n 'ShouldSkip': true,\n },\n {\n scope: 'foobarbaz',\n }\n)\n\n// > Trigger a run\nhatchet.events.push(\n SIMPLE_EVENT,\n {\n 'Message': 'hello',\n 'ShouldSkip': false,\n },\n {\n scope: 'foobarbaz',\n }\n)",
|
||||
source: 'out/typescript/on_event/filter.ts',
|
||||
blocks: {
|
||||
create_a_filter: {
|
||||
start: 5,
|
||||
stop: 14,
|
||||
},
|
||||
skip_a_run: {
|
||||
start: 17,
|
||||
stop: 26,
|
||||
},
|
||||
trigger_a_run: {
|
||||
start: 29,
|
||||
stop: 38,
|
||||
},
|
||||
},
|
||||
highlights: {},
|
||||
}; // Then replace double quotes with single quotes
|
||||
|
||||
export default snippet;
|
||||
@@ -1,9 +1,11 @@
|
||||
import evente2e from './event.e2e';
|
||||
import event from './event';
|
||||
import filter from './filter';
|
||||
import worker from './worker';
|
||||
import workflow from './workflow';
|
||||
|
||||
export { evente2e };
|
||||
export { event };
|
||||
export { filter };
|
||||
export { worker };
|
||||
export { workflow };
|
||||
|
||||
@@ -2,12 +2,24 @@ import { Snippet } from '@/lib/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',
|
||||
'content': 'package main\n\nimport (\n\t\'context\'\n\n\t\'github.com/google/uuid\'\n\t\'github.com/joho/godotenv\'\n\n\tv1_workflows \'github.com/hatchet-dev/hatchet/examples/go/workflows\'\n\t\'github.com/hatchet-dev/hatchet/pkg/client\'\n\t\'github.com/hatchet-dev/hatchet/pkg/client/rest\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\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\t// > Create a filter\n\tpayload := map[string]interface{}{\n\t\t\'main_character\': \'Anna\',\n\t\t\'supporting_character\': \'Stiva\',\n\t\t\'location\': \'Moscow\',\n\t}\n\n\t_, err = hatchet.Filters().Create(\n\t\tcontext.Background(),\n\t\trest.V1CreateFilterRequest{\n\t\t\tWorkflowId: uuid.New(),\n\t\t\tExpression: \'input.shouldSkip == false\',\n\t\t\tScope: \'foobarbaz\',\n\t\t\tPayload: &payload,\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > Skip a run\n\tskipPayload := map[string]interface{}{\n\t\t\'shouldSkip\': true,\n\t}\n\tskipScope := \'foobarbaz\'\n\terr = hatchet.Events().Push(\n\t\tcontext.Background(),\n\t\t\'simple-event:create\',\n\t\tskipPayload,\n\t\tclient.WithFilterScope(&skipScope),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > Trigger a run\n\ttriggerPayload := map[string]interface{}{\n\t\t\'shouldSkip\': false,\n\t}\n\ttriggerScope := \'foobarbaz\'\n\terr = hatchet.Events().Push(\n\t\tcontext.Background(),\n\t\t\'simple-event:create\',\n\t\ttriggerPayload,\n\t\tclient.WithFilterScope(&triggerScope),\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
|
||||
'start': 27,
|
||||
'stop': 33
|
||||
},
|
||||
'create_a_filter': {
|
||||
'start': 36,
|
||||
'stop': 50
|
||||
},
|
||||
'skip_a_run': {
|
||||
'start': 57,
|
||||
'stop': 66
|
||||
},
|
||||
'trigger_a_run': {
|
||||
'start': 73,
|
||||
'stop': 82
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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(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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Snippet } from '@/lib/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',
|
||||
'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)\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': {}
|
||||
|
||||
24
frontend/docs/lib/generated/snips/python/events/filter.ts
Normal file
24
frontend/docs/lib/generated/snips/python/events/filter.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from examples.events.worker import EVENT_KEY, event_workflow\nfrom hatchet_sdk import Hatchet, PushEventOptions\n\nhatchet = Hatchet()\n\n# > Create a filter\nhatchet.filters.create(\n workflow_id=event_workflow.id,\n expression=\'input.should_skip == false\',\n scope=\'foobarbaz\',\n payload={\n \'main_character\': \'Anna\',\n \'supporting_character\': \'Stiva\',\n \'location\': \'Moscow\',\n },\n)\n\n# > Skip a run\nhatchet.event.push(\n event_key=EVENT_KEY,\n payload={\n \'should_skip\': True,\n },\n options=PushEventOptions(\n scope=\'foobarbaz\',\n ),\n)\n\n# > Trigger a run\nhatchet.event.push(\n event_key=EVENT_KEY,\n payload={\n \'should_skip\': True,\n },\n options=PushEventOptions(\n scope=\'foobarbaz\',\n ),\n)\n',
|
||||
'source': 'out/python/events/filter.py',
|
||||
'blocks': {
|
||||
'create_a_filter': {
|
||||
'start': 7,
|
||||
'stop': 16
|
||||
},
|
||||
'skip_a_run': {
|
||||
'start': 19,
|
||||
'stop': 27
|
||||
},
|
||||
'trigger_a_run': {
|
||||
'start': 30,
|
||||
'stop': 38
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
|
||||
export default snippet;
|
||||
@@ -1,7 +1,9 @@
|
||||
import event from './event';
|
||||
import filter from './filter';
|
||||
import test_event from './test_event';
|
||||
import worker from './worker';
|
||||
|
||||
export { event }
|
||||
export { filter }
|
||||
export { test_event }
|
||||
export { worker }
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -2,12 +2,12 @@ import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from pydantic import BaseModel\n\nfrom hatchet_sdk import Context, Hatchet\n\nhatchet = Hatchet()\nEVENT_KEY = \'user:create\'\n\n\nclass EventWorkflowInput(BaseModel):\n should_skip: bool\n\n\n# > Event trigger\nevent_workflow = hatchet.workflow(\n name=\'EventWorkflow\',\n on_events=[EVENT_KEY],\n input_validator=EventWorkflowInput,\n)\n\n\n@event_workflow.task()\ndef task(input: EventWorkflowInput, ctx: Context) -> None:\n print(\'event received\')\n\n\ndef main() -> None:\n worker = hatchet.worker(name=\'EventWorker\', workflows=[event_workflow])\n\n worker.start()\n\n\nif __name__ == \'__main__\':\n main()\n',
|
||||
'content': 'from pydantic import BaseModel\n\nfrom hatchet_sdk import Context, Hatchet\n\nhatchet = Hatchet()\nEVENT_KEY = \'user:create\'\nSECONDARY_KEY = \'foobarbaz\'\n\n\nclass EventWorkflowInput(BaseModel):\n should_skip: bool\n\n\n# > Event trigger\nevent_workflow = hatchet.workflow(\n name=\'EventWorkflow\',\n on_events=[EVENT_KEY, SECONDARY_KEY],\n input_validator=EventWorkflowInput,\n)\n\n\n@event_workflow.task()\ndef task(input: EventWorkflowInput, ctx: Context) -> None:\n print(\'event received\')\n\n\ndef main() -> None:\n worker = hatchet.worker(name=\'EventWorker\', workflows=[event_workflow])\n\n worker.start()\n\n\nif __name__ == \'__main__\':\n main()\n',
|
||||
'source': 'out/python/events/worker.py',
|
||||
'blocks': {
|
||||
'event_trigger': {
|
||||
'start': 14,
|
||||
'stop': 18
|
||||
'start': 15,
|
||||
'stop': 19
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'typescript ',
|
||||
'content': 'import { hatchet } from \'../hatchet-client\';\nimport { lower, SIMPLE_EVENT } from \'./workflow\';\n\n// > Create a filter\nhatchet.filters.create({\n workflowId: lower.id,\n expression: \'input.ShouldSkip == false\',\n scope: \'foobarbaz\',\n payload: {\n main_character: \'Anna\',\n supporting_character: \'Stiva\',\n location: \'Moscow\',\n },\n});\n\n// > Skip a run\nhatchet.events.push(\n SIMPLE_EVENT,\n {\n Message: \'hello\',\n ShouldSkip: true,\n },\n {\n scope: \'foobarbaz\',\n }\n);\n\n// > Trigger a run\nhatchet.events.push(\n SIMPLE_EVENT,\n {\n Message: \'hello\',\n ShouldSkip: false,\n },\n {\n scope: \'foobarbaz\',\n }\n);\n',
|
||||
'source': 'out/typescript/on_event/filter.ts',
|
||||
'blocks': {
|
||||
'create_a_filter': {
|
||||
'start': 5,
|
||||
'stop': 14
|
||||
},
|
||||
'skip_a_run': {
|
||||
'start': 17,
|
||||
'stop': 26
|
||||
},
|
||||
'trigger_a_run': {
|
||||
'start': 29,
|
||||
'stop': 38
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
|
||||
export default snippet;
|
||||
@@ -1,9 +1,11 @@
|
||||
import evente2e from './event.e2e';
|
||||
import event from './event';
|
||||
import filter from './filter';
|
||||
import worker from './worker';
|
||||
import workflow from './workflow';
|
||||
|
||||
export { evente2e }
|
||||
export { event }
|
||||
export { filter }
|
||||
export { worker }
|
||||
export { workflow }
|
||||
|
||||
3
frontend/docs/next-env.d.ts
vendored
3
frontend/docs/next-env.d.ts
vendored
@@ -1,6 +1,5 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
/// <reference types="next/navigation-types/compat/navigation" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
|
||||
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
|
||||
|
||||
@@ -55,3 +55,72 @@ You can push an event to the event queue by calling the `push` method on the Hat
|
||||
<Snippet src={snips.go.run.event} block="pushing_an_event" />
|
||||
</Tabs.Tab>
|
||||
</UniversalTabs>
|
||||
|
||||
## Event Filtering
|
||||
|
||||
Events can also be _filtered_ in Hatchet, which allows you to push events to Hatchet and only trigger task runs from them in certain cases.
|
||||
|
||||
### Basic Usage
|
||||
|
||||
You can create event filters by using the `filters` clients on the SDKs:
|
||||
|
||||
<UniversalTabs items={["Python", "Typescript", "Go"]}>
|
||||
<Tabs.Tab title="Python">
|
||||
<Snippet src={snips.python.events.filter} block="create_a_filter" />
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab title="Typescript">
|
||||
<Snippet src={snips.typescript.on_event.filter} block="create_a_filter" />
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab title="Go">
|
||||
<Snippet src={snips.go.run.event} block="create_a_filter" />
|
||||
</Tabs.Tab>
|
||||
</UniversalTabs>
|
||||
|
||||
<Callout type="warning">
|
||||
Note the `scope` argument to the filter. When you create a filter, it must be
|
||||
given a `scope` which will be used by Hatchet internally to look it up. When
|
||||
you push events that you want filtered, you **must provide a `scope` with
|
||||
those events that matches the scope sent with the filter**. If you do not, the
|
||||
filter will not apply.
|
||||
</Callout>
|
||||
|
||||
Then, push an event that uses the filter to determine whether or not to run. For instance, this run will be skipped, since the payload does not match the expression:
|
||||
|
||||
<UniversalTabs items={["Python", "Typescript", "Go"]}>
|
||||
<Tabs.Tab title="Python">
|
||||
<Snippet src={snips.python.events.filter} block="skip_a_run" />
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab title="Typescript">
|
||||
<Snippet src={snips.typescript.on_event.filter} block="skip_a_run" />
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab title="Go">
|
||||
<Snippet src={snips.go.run.event} block="skip_a_run" />
|
||||
</Tabs.Tab>
|
||||
</UniversalTabs>
|
||||
|
||||
But this one will be triggered since the payload _does_ match the expression:
|
||||
|
||||
<UniversalTabs items={["Python", "Typescript", "Go"]}>
|
||||
<Tabs.Tab title="Python">
|
||||
<Snippet src={snips.python.events.filter} block="trigger_a_run" />
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab title="Typescript">
|
||||
<Snippet src={snips.typescript.on_event.filter} block="trigger_a_run" />
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab title="Go">
|
||||
<Snippet src={snips.go.run.event} block="trigger_a_run" />
|
||||
</Tabs.Tab>
|
||||
</UniversalTabs>
|
||||
|
||||
<Callout type="info">
|
||||
In Hatchet, filters are "positive", meaning that we look for _matches_ to the
|
||||
filter to determine which tasks to trigger.
|
||||
</Callout>
|
||||
|
||||
### Advanced Usage
|
||||
|
||||
In addition to specifying `input` in the expression (which corresponds to the _event_ payload), you can also provide the following fields:
|
||||
|
||||
1. `payload` corresponds to the _filter_ payload (which was part of the request when the filter was created).
|
||||
2. `additional_metadata` allows for filtering based on `additional_metadata` sent with the event.
|
||||
3. `event_key` allows for filtering based on the key of the event, such as `user:created`.
|
||||
|
||||
@@ -3,9 +3,13 @@ package main
|
||||
import (
|
||||
"context"
|
||||
|
||||
v1_workflows "github.com/hatchet-dev/hatchet/examples/go/workflows"
|
||||
v1 "github.com/hatchet-dev/hatchet/pkg/v1"
|
||||
"github.com/google/uuid"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
v1_workflows "github.com/hatchet-dev/hatchet/examples/go/workflows"
|
||||
"github.com/hatchet-dev/hatchet/pkg/client"
|
||||
"github.com/hatchet-dev/hatchet/pkg/client/rest"
|
||||
v1 "github.com/hatchet-dev/hatchet/pkg/v1"
|
||||
)
|
||||
|
||||
func event() {
|
||||
@@ -29,6 +33,58 @@ func event() {
|
||||
)
|
||||
// !!
|
||||
|
||||
// > Create a filter
|
||||
payload := map[string]interface{}{
|
||||
"main_character": "Anna",
|
||||
"supporting_character": "Stiva",
|
||||
"location": "Moscow",
|
||||
}
|
||||
|
||||
_, err = hatchet.Filters().Create(
|
||||
context.Background(),
|
||||
rest.V1CreateFilterRequest{
|
||||
WorkflowId: uuid.New(),
|
||||
Expression: "input.shouldSkip == false",
|
||||
Scope: "foobarbaz",
|
||||
Payload: &payload,
|
||||
},
|
||||
)
|
||||
// !!
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// > Skip a run
|
||||
skipPayload := map[string]interface{}{
|
||||
"shouldSkip": true,
|
||||
}
|
||||
skipScope := "foobarbaz"
|
||||
err = hatchet.Events().Push(
|
||||
context.Background(),
|
||||
"simple-event:create",
|
||||
skipPayload,
|
||||
client.WithFilterScope(&skipScope),
|
||||
)
|
||||
// !!
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// > Trigger a run
|
||||
triggerPayload := map[string]interface{}{
|
||||
"shouldSkip": false,
|
||||
}
|
||||
triggerScope := "foobarbaz"
|
||||
err = hatchet.Events().Push(
|
||||
context.Background(),
|
||||
"simple-event:create",
|
||||
triggerPayload,
|
||||
client.WithFilterScope(&triggerScope),
|
||||
)
|
||||
// !!
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ type HatchetClient interface {
|
||||
Crons() features.CronsClient
|
||||
Schedules() features.SchedulesClient
|
||||
Events() v0Client.EventClient
|
||||
Filters() features.FiltersClient
|
||||
|
||||
// TODO Run, RunNoWait, bulk
|
||||
}
|
||||
@@ -51,6 +52,7 @@ type v1HatchetClientImpl struct {
|
||||
workflows features.WorkflowsClient
|
||||
crons features.CronsClient
|
||||
schedules features.SchedulesClient
|
||||
filters features.FiltersClient
|
||||
}
|
||||
|
||||
// NewHatchetClient creates a new V1 Hatchet client with the provided configuration.
|
||||
@@ -164,3 +166,12 @@ func (c *v1HatchetClientImpl) Schedules() features.SchedulesClient {
|
||||
}
|
||||
return c.schedules
|
||||
}
|
||||
|
||||
func (c *v1HatchetClientImpl) Filters() features.FiltersClient {
|
||||
if c.filters == nil {
|
||||
api := c.V0().API()
|
||||
tenantID := c.V0().TenantId()
|
||||
c.filters = features.NewFiltersClient(api, &tenantID)
|
||||
}
|
||||
return c.filters
|
||||
}
|
||||
|
||||
90
pkg/v1/features/filters.go
Normal file
90
pkg/v1/features/filters.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package features
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/hatchet-dev/hatchet/pkg/client/rest"
|
||||
)
|
||||
|
||||
type FiltersClient interface {
|
||||
List(ctx context.Context, opts *rest.V1FilterListParams) (*rest.V1FilterList, error)
|
||||
|
||||
Get(ctx context.Context, filterID string) (*rest.V1Filter, error)
|
||||
|
||||
Create(ctx context.Context, opts rest.V1CreateFilterRequest) (*rest.V1Filter, error)
|
||||
|
||||
Delete(ctx context.Context, filterID string) (*rest.V1Filter, error)
|
||||
}
|
||||
|
||||
type filtersClientImpl struct {
|
||||
api *rest.ClientWithResponses
|
||||
tenantID uuid.UUID
|
||||
}
|
||||
|
||||
func NewFiltersClient(
|
||||
api *rest.ClientWithResponses,
|
||||
tenantID *string,
|
||||
) FiltersClient {
|
||||
return &filtersClientImpl{
|
||||
api: api,
|
||||
tenantID: uuid.MustParse(*tenantID),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *filtersClientImpl) List(ctx context.Context, opts *rest.V1FilterListParams) (*rest.V1FilterList, error) {
|
||||
resp, err := c.api.V1FilterListWithResponse(
|
||||
ctx,
|
||||
c.tenantID,
|
||||
opts,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.JSON200, nil
|
||||
}
|
||||
|
||||
func (c *filtersClientImpl) Get(ctx context.Context, filterID string) (*rest.V1Filter, error) {
|
||||
resp, err := c.api.V1FilterGetWithResponse(
|
||||
ctx,
|
||||
c.tenantID,
|
||||
uuid.MustParse(filterID),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.JSON200, nil
|
||||
}
|
||||
|
||||
func (c *filtersClientImpl) Create(ctx context.Context, opts rest.V1CreateFilterRequest) (*rest.V1Filter, error) {
|
||||
resp, err := c.api.V1FilterCreateWithResponse(
|
||||
ctx,
|
||||
c.tenantID,
|
||||
opts,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.JSON200, nil
|
||||
}
|
||||
|
||||
func (c *filtersClientImpl) Delete(ctx context.Context, filterID string) (*rest.V1Filter, error) {
|
||||
resp, err := c.api.V1FilterDeleteWithResponse(
|
||||
ctx,
|
||||
c.tenantID,
|
||||
uuid.MustParse(filterID),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.JSON200, nil
|
||||
}
|
||||
41
sdks/python/examples/events/filter.py
Normal file
41
sdks/python/examples/events/filter.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from examples.events.worker import EVENT_KEY, event_workflow
|
||||
from hatchet_sdk import Hatchet, PushEventOptions
|
||||
|
||||
hatchet = Hatchet()
|
||||
|
||||
# > Create a filter
|
||||
hatchet.filters.create(
|
||||
workflow_id=event_workflow.id,
|
||||
expression="input.should_skip == false",
|
||||
scope="foobarbaz",
|
||||
payload={
|
||||
"main_character": "Anna",
|
||||
"supporting_character": "Stiva",
|
||||
"location": "Moscow",
|
||||
},
|
||||
)
|
||||
# !!
|
||||
|
||||
# > Skip a run
|
||||
hatchet.event.push(
|
||||
event_key=EVENT_KEY,
|
||||
payload={
|
||||
"should_skip": True,
|
||||
},
|
||||
options=PushEventOptions(
|
||||
scope="foobarbaz",
|
||||
),
|
||||
)
|
||||
# !!
|
||||
|
||||
# > Trigger a run
|
||||
hatchet.event.push(
|
||||
event_key=EVENT_KEY,
|
||||
payload={
|
||||
"should_skip": True,
|
||||
},
|
||||
options=PushEventOptions(
|
||||
scope="foobarbaz",
|
||||
),
|
||||
)
|
||||
# !!
|
||||
@@ -160,6 +160,7 @@ class WorkerActionListenerProcess:
|
||||
|
||||
if count > 0:
|
||||
logger.warning(f"{BLOCKED_THREAD_WARNING}: Waiting Steps {count}")
|
||||
print(asyncio.current_task())
|
||||
await asyncio.sleep(1)
|
||||
|
||||
async def send_event(self, event: ActionEvent, retry_attempt: int = 1) -> None:
|
||||
|
||||
41
sdks/typescript/src/v1/examples/on_event/filter.ts
Normal file
41
sdks/typescript/src/v1/examples/on_event/filter.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { hatchet } from '../hatchet-client';
|
||||
import { lower, SIMPLE_EVENT } from './workflow';
|
||||
|
||||
// > Create a filter
|
||||
hatchet.filters.create({
|
||||
workflowId: lower.id,
|
||||
expression: 'input.ShouldSkip == false',
|
||||
scope: 'foobarbaz',
|
||||
payload: {
|
||||
main_character: 'Anna',
|
||||
supporting_character: 'Stiva',
|
||||
location: 'Moscow',
|
||||
},
|
||||
});
|
||||
// !!
|
||||
|
||||
// > Skip a run
|
||||
hatchet.events.push(
|
||||
SIMPLE_EVENT,
|
||||
{
|
||||
Message: 'hello',
|
||||
ShouldSkip: true,
|
||||
},
|
||||
{
|
||||
scope: 'foobarbaz',
|
||||
}
|
||||
);
|
||||
// !!
|
||||
|
||||
// > Trigger a run
|
||||
hatchet.events.push(
|
||||
SIMPLE_EVENT,
|
||||
{
|
||||
Message: 'hello',
|
||||
ShouldSkip: false,
|
||||
},
|
||||
{
|
||||
scope: 'foobarbaz',
|
||||
}
|
||||
);
|
||||
// !!
|
||||
Reference in New Issue
Block a user