mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-02-04 23:28:47 -06:00
Fix: Quote replacement in generated doc snippets (#1755)
* fix: get rid of quote replacing * chore: gen * drive by: event doc wording * fix: dedenting
This commit is contained in:
@@ -19,7 +19,8 @@ const CodeStyleRender = ({ parsed, language }: CodeStyleRenderProps) => {
|
||||
|
||||
useEffect(() => {
|
||||
const asyncHighlight = async () => {
|
||||
const highlightedHtml = await codeToHtml(parsed, {
|
||||
const dedentedCode = dedent(parsed);
|
||||
const highlightedHtml = await codeToHtml(dedentedCode, {
|
||||
lang: language.toLowerCase(),
|
||||
theme: themeName,
|
||||
});
|
||||
@@ -38,3 +39,33 @@ const CodeStyleRender = ({ parsed, language }: CodeStyleRenderProps) => {
|
||||
};
|
||||
|
||||
export default CodeStyleRender;
|
||||
|
||||
function dedent(code: string) {
|
||||
const lines = code.split("\n");
|
||||
const nonEmptyLines = lines.filter((line) => line.trim().length > 0);
|
||||
|
||||
if (nonEmptyLines.length === 0) {
|
||||
return code;
|
||||
}
|
||||
|
||||
const minIndent = Math.min(
|
||||
...nonEmptyLines.map((line) => {
|
||||
const match = line.match(/^(\s*)/);
|
||||
return match ? match[1].length : 0;
|
||||
})
|
||||
);
|
||||
|
||||
if (minIndent > 0) {
|
||||
return lines
|
||||
.map((line) => {
|
||||
if (line.trim().length > 0) {
|
||||
return line.slice(minIndent);
|
||||
}
|
||||
|
||||
return line;
|
||||
})
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
@@ -84,16 +84,5 @@ export const parseDocComments = (
|
||||
return `🚨 No snippet found for ${target} \n\n${source}`;
|
||||
}
|
||||
|
||||
// Shift indentation to the least indented level
|
||||
const nonEmptyLines = resultLines.filter((line) => line.trim() !== "");
|
||||
const indents = nonEmptyLines.map(
|
||||
(line) => line.match(/^(\s*)/)?.[1].length || 0
|
||||
);
|
||||
const minIndent = Math.min(...indents);
|
||||
|
||||
const shiftedLines = resultLines.map((line) =>
|
||||
line.length >= minIndent ? line.slice(minIndent) : line.trimStart()
|
||||
);
|
||||
|
||||
return shiftedLines.join("\n");
|
||||
return resultLines.join("\n");
|
||||
};
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package migration_guides\n\nimport (\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n)\n\nfunc HatchetClient() (v1.HatchetClient, error) {\n\thatchet, err := v1.NewHatchetClient()\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn hatchet, nil\n}\n',
|
||||
'source': 'out/go/migration-guides/hatchet-client.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package migration_guides\n\nimport (\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n)\n\nfunc HatchetClient() (v1.HatchetClient, error) {\n\thatchet, err := v1.NewHatchetClient()\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn hatchet, nil\n}\n",
|
||||
"source": "out/go/migration-guides/hatchet-client.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'context\'\n\t\'fmt\'\n\n\thatchet_client \'github.com/hatchet-dev/hatchet/pkg/examples/quickstart/hatchet_client\'\n\tworkflows \'github.com/hatchet-dev/hatchet/pkg/examples/quickstart/workflows\'\n)\n\nfunc main() {\n\thatchet, err := hatchet_client.HatchetClient()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tsimple := workflows.FirstTask(hatchet)\n\n\tresult, err := simple.Run(context.Background(), workflows.SimpleInput{\n\t\tMessage: \'Hello, World!\',\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(\n\t\t\'Finished running task, and got the transformed message! The transformed message is:\',\n\t\tresult.ToLower.TransformedMessage,\n\t)\n}\n',
|
||||
'source': 'out/go/quickstart/cmd/run/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\thatchet_client \"github.com/hatchet-dev/hatchet/pkg/examples/quickstart/hatchet_client\"\n\tworkflows \"github.com/hatchet-dev/hatchet/pkg/examples/quickstart/workflows\"\n)\n\nfunc main() {\n\thatchet, err := hatchet_client.HatchetClient()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tsimple := workflows.FirstTask(hatchet)\n\n\tresult, err := simple.Run(context.Background(), workflows.SimpleInput{\n\t\tMessage: \"Hello, World!\",\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(\n\t\t\"Finished running task, and got the transformed message! The transformed message is:\",\n\t\tresult.ToLower.TransformedMessage,\n\t)\n}\n",
|
||||
"source": "out/go/quickstart/cmd/run/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\thatchet_client \'github.com/hatchet-dev/hatchet/pkg/examples/quickstart/hatchet_client\'\n\tworkflows \'github.com/hatchet-dev/hatchet/pkg/examples/quickstart/workflows\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/cmdutils\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/worker\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n)\n\nfunc main() {\n\n\thatchet, err := hatchet_client.HatchetClient()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := hatchet.Worker(\n\t\tworker.WorkerOpts{\n\t\t\tName: \'first-worker\',\n\t\t\tWorkflows: []workflow.WorkflowBase{\n\t\t\t\tworkflows.FirstTask(hatchet),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// we construct an interrupt context to handle Ctrl+C\n\t// you can pass in your own context.Context here to the worker\n\tinterruptCtx, cancel := cmdutils.NewInterruptContext()\n\n\tdefer cancel()\n\n\terr = worker.StartBlocking(interruptCtx)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n',
|
||||
'source': 'out/go/quickstart/cmd/worker/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\thatchet_client \"github.com/hatchet-dev/hatchet/pkg/examples/quickstart/hatchet_client\"\n\tworkflows \"github.com/hatchet-dev/hatchet/pkg/examples/quickstart/workflows\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/cmdutils\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/worker\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n)\n\nfunc main() {\n\n\thatchet, err := hatchet_client.HatchetClient()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tworker, err := hatchet.Worker(\n\t\tworker.WorkerOpts{\n\t\t\tName: \"first-worker\",\n\t\t\tWorkflows: []workflow.WorkflowBase{\n\t\t\t\tworkflows.FirstTask(hatchet),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// we construct an interrupt context to handle Ctrl+C\n\t// you can pass in your own context.Context here to the worker\n\tinterruptCtx, cancel := cmdutils.NewInterruptContext()\n\n\tdefer cancel()\n\n\terr = worker.StartBlocking(interruptCtx)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n",
|
||||
"source": "out/go/quickstart/cmd/worker/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package hatchet_client\n\nimport (\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/joho/godotenv\'\n)\n\nfunc HatchetClient() (v1.HatchetClient, error) {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn v1.NewHatchetClient()\n}\n',
|
||||
'source': 'out/go/quickstart/hatchet_client/hatchet_client.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package hatchet_client\n\nimport (\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/joho/godotenv\"\n)\n\nfunc HatchetClient() (v1.HatchetClient, error) {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn v1.NewHatchetClient()\n}\n",
|
||||
"source": "out/go/quickstart/hatchet_client/hatchet_client.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package workflows\n\nimport (\n\t\'fmt\'\n\t\'strings\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype SimpleInput struct {\n\tMessage string `json:\'message\'`\n}\n\ntype LowerOutput struct {\n\tTransformedMessage string `json:\'transformed_message\'`\n}\n\ntype SimpleResult struct {\n\tToLower LowerOutput\n}\n\nfunc FirstTask(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[SimpleInput, SimpleResult] {\n\tsimple := factory.NewWorkflow[SimpleInput, SimpleResult](\n\t\tcreate.WorkflowCreateOpts[SimpleInput]{\n\t\t\tName: \'first-task\',\n\t\t},\n\t\thatchet,\n\t)\n\n\tsimple.Task(\n\t\tcreate.WorkflowTask[SimpleInput, SimpleResult]{\n\t\t\tName: \'first-task\',\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input SimpleInput) (any, error) {\n\t\t\tfmt.Println(\'first-task task called\')\n\t\t\treturn &LowerOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n',
|
||||
'source': 'out/go/quickstart/workflows/first_task.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package workflows\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype SimpleInput struct {\n\tMessage string `json:\"message\"`\n}\n\ntype LowerOutput struct {\n\tTransformedMessage string `json:\"transformed_message\"`\n}\n\ntype SimpleResult struct {\n\tToLower LowerOutput\n}\n\nfunc FirstTask(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[SimpleInput, SimpleResult] {\n\tsimple := factory.NewWorkflow[SimpleInput, SimpleResult](\n\t\tcreate.WorkflowCreateOpts[SimpleInput]{\n\t\t\tName: \"first-task\",\n\t\t},\n\t\thatchet,\n\t)\n\n\tsimple.Task(\n\t\tcreate.WorkflowTask[SimpleInput, SimpleResult]{\n\t\t\tName: \"first-task\",\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input SimpleInput) (any, error) {\n\t\t\tfmt.Println(\"first-task task called\")\n\t\t\treturn &LowerOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n",
|
||||
"source": "out/go/quickstart/workflows/first_task.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'context\'\n\t\'fmt\'\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 bulk() {\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\n\tctx := context.Background()\n\t// > Bulk Run Tasks\n\tsimple := v1_workflows.Simple(hatchet)\n\tbulkRunIds, err := simple.RunBulkNoWait(ctx, []v1_workflows.SimpleInput{\n\t\t{\n\t\t\tMessage: \'Hello, World!\',\n\t\t},\n\t\t{\n\t\t\tMessage: \'Hello, Moon!\',\n\t\t},\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(bulkRunIds)\n}\n',
|
||||
'source': 'out/go/run/bulk.go',
|
||||
'blocks': {
|
||||
'bulk_run_tasks': {
|
||||
'start': 26,
|
||||
'stop': 40
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\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 bulk() {\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\n\tctx := context.Background()\n\t// > Bulk Run Tasks\n\tsimple := v1_workflows.Simple(hatchet)\n\tbulkRunIds, err := simple.RunBulkNoWait(ctx, []v1_workflows.SimpleInput{\n\t\t{\n\t\t\tMessage: \"Hello, World!\",\n\t\t},\n\t\t{\n\t\t\tMessage: \"Hello, Moon!\",\n\t\t},\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(bulkRunIds)\n}\n",
|
||||
"source": "out/go/run/bulk.go",
|
||||
"blocks": {
|
||||
"bulk_run_tasks": {
|
||||
"start": 26,
|
||||
"stop": 40
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'context\'\n\t\'fmt\'\n\n\tv1_workflows \'github.com/hatchet-dev/hatchet/examples/go/workflows\'\n\t\'github.com/hatchet-dev/hatchet/pkg/client/rest\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/joho/godotenv\'\n)\n\nfunc cron() {\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// > Create\n\tsimple := v1_workflows.Simple(hatchet)\n\n\tctx := context.Background()\n\n\tresult, err := simple.Cron(\n\t\tctx,\n\t\t\'daily-run\',\n\t\t\'0 0 * * *\',\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\t// it may be useful to save the cron id for later\n\tfmt.Println(result.Metadata.Id)\n\n\t// > Delete\n\thatchet.Crons().Delete(ctx, result.Metadata.Id)\n\n\t// > List\n\tcrons, err := hatchet.Crons().List(ctx, rest.CronWorkflowListParams{\n\t\tAdditionalMetadata: &[]string{\'user:daily-run\'},\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(crons)\n}\n',
|
||||
'source': 'out/go/run/cron.go',
|
||||
'blocks': {
|
||||
'create': {
|
||||
'start': 25,
|
||||
'stop': 43
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\tv1_workflows \"github.com/hatchet-dev/hatchet/examples/go/workflows\"\n\t\"github.com/hatchet-dev/hatchet/pkg/client/rest\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/joho/godotenv\"\n)\n\nfunc cron() {\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// > Create\n\tsimple := v1_workflows.Simple(hatchet)\n\n\tctx := context.Background()\n\n\tresult, err := simple.Cron(\n\t\tctx,\n\t\t\"daily-run\",\n\t\t\"0 0 * * *\",\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\t// it may be useful to save the cron id for later\n\tfmt.Println(result.Metadata.Id)\n\n\t// > Delete\n\thatchet.Crons().Delete(ctx, result.Metadata.Id)\n\n\t// > List\n\tcrons, err := hatchet.Crons().List(ctx, rest.CronWorkflowListParams{\n\t\tAdditionalMetadata: &[]string{\"user:daily-run\"},\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(crons)\n}\n",
|
||||
"source": "out/go/run/cron.go",
|
||||
"blocks": {
|
||||
"create": {
|
||||
"start": 25,
|
||||
"stop": 43
|
||||
},
|
||||
'delete': {
|
||||
'start': 46,
|
||||
'stop': 46
|
||||
"delete": {
|
||||
"start": 46,
|
||||
"stop": 46
|
||||
},
|
||||
'list': {
|
||||
'start': 49,
|
||||
'stop': 51
|
||||
"list": {
|
||||
"start": 49,
|
||||
"stop": 51
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'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': 27,
|
||||
'stop': 33
|
||||
"language": "go",
|
||||
"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": 27,
|
||||
"stop": 33
|
||||
},
|
||||
'create_a_filter': {
|
||||
'start': 36,
|
||||
'stop': 50
|
||||
"create_a_filter": {
|
||||
"start": 36,
|
||||
"stop": 50
|
||||
},
|
||||
'skip_a_run': {
|
||||
'start': 57,
|
||||
'stop': 66
|
||||
"skip_a_run": {
|
||||
"start": 57,
|
||||
"stop": 66
|
||||
},
|
||||
'trigger_a_run': {
|
||||
'start': 73,
|
||||
'stop': 82
|
||||
"trigger_a_run": {
|
||||
"start": 73,
|
||||
"stop": 82
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
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\tv1_workflows \'github.com/hatchet-dev/hatchet/examples/go/workflows\'\n\t\'github.com/hatchet-dev/hatchet/pkg/client\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/joho/godotenv\'\n)\n\nfunc priority() {\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\n\tctx := context.Background()\n\n\tpriorityWorkflow := v1_workflows.Priority(hatchet)\n\n\t// > Running a Task with Priority\n\tpriority := int32(3)\n\n\trunId, err := priorityWorkflow.RunNoWait(ctx, v1_workflows.PriorityInput{\n\t\tUserId: \'1234\',\n\t}, client.WithPriority(priority))\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(runId)\n\n\t// > Schedule and cron\n\tschedulePriority := int32(3)\n\trunAt := time.Now().Add(time.Minute)\n\n\tscheduledRunId, _ := priorityWorkflow.Schedule(ctx, runAt, v1_workflows.PriorityInput{\n\t\tUserId: \'1234\',\n\t}, client.WithPriority(schedulePriority))\n\n\tcronId, _ := priorityWorkflow.Cron(ctx, \'my-cron\', \'* * * * *\', v1_workflows.PriorityInput{\n\t\tUserId: \'1234\',\n\t}, client.WithPriority(schedulePriority))\n\n\tfmt.Println(scheduledRunId)\n\tfmt.Println(cronId)\n\n}\n',
|
||||
'source': 'out/go/run/priority.go',
|
||||
'blocks': {
|
||||
'running_a_task_with_priority': {
|
||||
'start': 31,
|
||||
'stop': 35
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"time\"\n\n\tv1_workflows \"github.com/hatchet-dev/hatchet/examples/go/workflows\"\n\t\"github.com/hatchet-dev/hatchet/pkg/client\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/joho/godotenv\"\n)\n\nfunc priority() {\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\n\tctx := context.Background()\n\n\tpriorityWorkflow := v1_workflows.Priority(hatchet)\n\n\t// > Running a Task with Priority\n\tpriority := int32(3)\n\n\trunId, err := priorityWorkflow.RunNoWait(ctx, v1_workflows.PriorityInput{\n\t\tUserId: \"1234\",\n\t}, client.WithPriority(priority))\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(runId)\n\n\t// > Schedule and cron\n\tschedulePriority := int32(3)\n\trunAt := time.Now().Add(time.Minute)\n\n\tscheduledRunId, _ := priorityWorkflow.Schedule(ctx, runAt, v1_workflows.PriorityInput{\n\t\tUserId: \"1234\",\n\t}, client.WithPriority(schedulePriority))\n\n\tcronId, _ := priorityWorkflow.Cron(ctx, \"my-cron\", \"* * * * *\", v1_workflows.PriorityInput{\n\t\tUserId: \"1234\",\n\t}, client.WithPriority(schedulePriority))\n\n\tfmt.Println(scheduledRunId)\n\tfmt.Println(cronId)\n\n}\n",
|
||||
"source": "out/go/run/priority.go",
|
||||
"blocks": {
|
||||
"running_a_task_with_priority": {
|
||||
"start": 31,
|
||||
"stop": 35
|
||||
},
|
||||
'schedule_and_cron': {
|
||||
'start': 44,
|
||||
'stop': 53
|
||||
"schedule_and_cron": {
|
||||
"start": 44,
|
||||
"stop": 53
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
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\'sync\'\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 simple() {\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\n\tctx := context.Background()\n\t// > Running a Task\n\tsimple := v1_workflows.Simple(hatchet)\n\tresult, err := simple.Run(ctx, v1_workflows.SimpleInput{\n\t\tMessage: \'Hello, World!\',\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(result.TransformedMessage)\n\n\t// > Running Multiple Tasks\n\tvar results []string\n\tvar resultsMutex sync.Mutex\n\tvar errs []error\n\tvar errsMutex sync.Mutex\n\n\twg := sync.WaitGroup{}\n\twg.Add(2)\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tresult, err := simple.Run(ctx, v1_workflows.SimpleInput{\n\t\t\tMessage: \'Hello, World!\',\n\t\t})\n\n\t\tif err != nil {\n\t\t\terrsMutex.Lock()\n\t\t\terrs = append(errs, err)\n\t\t\terrsMutex.Unlock()\n\t\t\treturn\n\t\t}\n\n\t\tresultsMutex.Lock()\n\t\tresults = append(results, result.TransformedMessage)\n\t\tresultsMutex.Unlock()\n\t}()\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tresult, err := simple.Run(ctx, v1_workflows.SimpleInput{\n\t\t\tMessage: \'Hello, Moon!\',\n\t\t})\n\n\t\tif err != nil {\n\t\t\terrsMutex.Lock()\n\t\t\terrs = append(errs, err)\n\t\t\terrsMutex.Unlock()\n\t\t\treturn\n\t\t}\n\n\t\tresultsMutex.Lock()\n\t\tresults = append(results, result.TransformedMessage)\n\t\tresultsMutex.Unlock()\n\t}()\n\n\twg.Wait()\n\n\t// > Running a Task Without Waiting\n\tsimple = v1_workflows.Simple(hatchet)\n\trunRef, err := simple.RunNoWait(ctx, v1_workflows.SimpleInput{\n\t\tMessage: \'Hello, World!\',\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// The Run Ref Exposes an ID that can be used to wait for the task to complete\n\t// or check on the status of the task\n\trunId := runRef.RunId()\n\tfmt.Println(runId)\n\n\t// > Subscribing to results\n\t// finally, we can wait for the task to complete and get the result\n\tfinalResult, err := runRef.Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(finalResult)\n}\n',
|
||||
'source': 'out/go/run/simple.go',
|
||||
'blocks': {
|
||||
'running_a_task': {
|
||||
'start': 27,
|
||||
'stop': 36
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"sync\"\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 simple() {\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\n\tctx := context.Background()\n\t// > Running a Task\n\tsimple := v1_workflows.Simple(hatchet)\n\tresult, err := simple.Run(ctx, v1_workflows.SimpleInput{\n\t\tMessage: \"Hello, World!\",\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(result.TransformedMessage)\n\n\t// > Running Multiple Tasks\n\tvar results []string\n\tvar resultsMutex sync.Mutex\n\tvar errs []error\n\tvar errsMutex sync.Mutex\n\n\twg := sync.WaitGroup{}\n\twg.Add(2)\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tresult, err := simple.Run(ctx, v1_workflows.SimpleInput{\n\t\t\tMessage: \"Hello, World!\",\n\t\t})\n\n\t\tif err != nil {\n\t\t\terrsMutex.Lock()\n\t\t\terrs = append(errs, err)\n\t\t\terrsMutex.Unlock()\n\t\t\treturn\n\t\t}\n\n\t\tresultsMutex.Lock()\n\t\tresults = append(results, result.TransformedMessage)\n\t\tresultsMutex.Unlock()\n\t}()\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tresult, err := simple.Run(ctx, v1_workflows.SimpleInput{\n\t\t\tMessage: \"Hello, Moon!\",\n\t\t})\n\n\t\tif err != nil {\n\t\t\terrsMutex.Lock()\n\t\t\terrs = append(errs, err)\n\t\t\terrsMutex.Unlock()\n\t\t\treturn\n\t\t}\n\n\t\tresultsMutex.Lock()\n\t\tresults = append(results, result.TransformedMessage)\n\t\tresultsMutex.Unlock()\n\t}()\n\n\twg.Wait()\n\n\t// > Running a Task Without Waiting\n\tsimple = v1_workflows.Simple(hatchet)\n\trunRef, err := simple.RunNoWait(ctx, v1_workflows.SimpleInput{\n\t\tMessage: \"Hello, World!\",\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// The Run Ref Exposes an ID that can be used to wait for the task to complete\n\t// or check on the status of the task\n\trunId := runRef.RunId()\n\tfmt.Println(runId)\n\n\t// > Subscribing to results\n\t// finally, we can wait for the task to complete and get the result\n\tfinalResult, err := runRef.Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(finalResult)\n}\n",
|
||||
"source": "out/go/run/simple.go",
|
||||
"blocks": {
|
||||
"running_a_task": {
|
||||
"start": 27,
|
||||
"stop": 36
|
||||
},
|
||||
'running_multiple_tasks': {
|
||||
'start': 39,
|
||||
'stop': 83
|
||||
"running_multiple_tasks": {
|
||||
"start": 39,
|
||||
"stop": 83
|
||||
},
|
||||
'running_a_task_without_waiting': {
|
||||
'start': 86,
|
||||
'stop': 98
|
||||
"running_a_task_without_waiting": {
|
||||
"start": 86,
|
||||
"stop": 98
|
||||
},
|
||||
'subscribing_to_results': {
|
||||
'start': 101,
|
||||
'stop': 108
|
||||
"subscribing_to_results": {
|
||||
"start": 101,
|
||||
"stop": 108
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\t\'os\'\n\t\'time\'\n\n\tv1_workflows \'github.com/hatchet-dev/hatchet/examples/go/workflows\'\n\t\'github.com/hatchet-dev/hatchet/pkg/cmdutils\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/worker\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/joho/godotenv\'\n)\n\nfunc main() {\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\n\t// Get workflow name from command line arguments\n\tvar workflowName string\n\tif len(os.Args) > 1 {\n\t\tworkflowName = os.Args[1]\n\t\tfmt.Println(\'workflow name provided:\', workflowName)\n\t}\n\n\t// Define workflows map\n\tworkflowMap := map[string][]workflow.WorkflowBase{\n\t\t\'dag\': {v1_workflows.DagWorkflow(hatchet)},\n\t\t\'on-failure\': {v1_workflows.OnFailure(hatchet)},\n\t\t\'simple\': {v1_workflows.Simple(hatchet)},\n\t\t\'sleep\': {v1_workflows.DurableSleep(hatchet)},\n\t\t\'child\': {v1_workflows.Parent(hatchet), v1_workflows.Child(hatchet)},\n\t\t\'cancellation\': {v1_workflows.Cancellation(hatchet)},\n\t\t\'timeout\': {v1_workflows.Timeout(hatchet)},\n\t\t\'sticky\': {v1_workflows.Sticky(hatchet), v1_workflows.StickyDag(hatchet), v1_workflows.Child(hatchet)},\n\t\t\'retries\': {v1_workflows.Retries(hatchet), v1_workflows.RetriesWithCount(hatchet), v1_workflows.WithBackoff(hatchet)},\n\t\t\'on-cron\': {v1_workflows.OnCron(hatchet)},\n\t\t\'non-retryable\': {v1_workflows.NonRetryableError(hatchet)},\n\t\t\'priority\': {v1_workflows.Priority(hatchet)},\n\t}\n\n\t// Add an \'all\' option that registers all workflows\n\tallWorkflows := []workflow.WorkflowBase{}\n\tfor _, wfs := range workflowMap {\n\t\tallWorkflows = append(allWorkflows, wfs...)\n\t}\n\tworkflowMap[\'all\'] = allWorkflows\n\n\t// Lookup workflow from map\n\tworkflow, ok := workflowMap[workflowName]\n\tif !ok {\n\t\tfmt.Println(\'Invalid workflow name provided. Usage: go run examples/v1/worker/start.go [workflow-name]\')\n\t\tfmt.Println(\'Available workflows:\', getAvailableWorkflows(workflowMap))\n\t\tos.Exit(1)\n\t}\n\n\tvar slots int\n\tif workflowName == \'priority\' {\n\t\tslots = 1\n\t} else {\n\t\tslots = 100\n\t}\n\n\tworker, err := hatchet.Worker(\n\t\tworker.WorkerOpts{\n\t\t\tName: fmt.Sprintf(\'%s-worker\', workflowName),\n\t\t\tWorkflows: workflow,\n\t\t\tSlots: slots,\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.NewInterruptContext()\n\n\terr = worker.StartBlocking(interruptCtx)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tgo func() {\n\t\ttime.Sleep(10 * time.Second)\n\t\tcancel()\n\t}()\n}\n\n// Helper function to get available workflows as a formatted string\nfunc getAvailableWorkflows(workflowMap map[string][]workflow.WorkflowBase) string {\n\tvar workflows string\n\tcount := 0\n\tfor name := range workflowMap {\n\t\tif count > 0 {\n\t\t\tworkflows += \', \'\n\t\t}\n\t\tworkflows += fmt.Sprintf(\'\'%s\'\', name)\n\t\tcount++\n\t}\n\treturn workflows\n}\n',
|
||||
'source': 'out/go/worker/start.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"time\"\n\n\tv1_workflows \"github.com/hatchet-dev/hatchet/examples/go/workflows\"\n\t\"github.com/hatchet-dev/hatchet/pkg/cmdutils\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/worker\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/joho/godotenv\"\n)\n\nfunc main() {\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\n\t// Get workflow name from command line arguments\n\tvar workflowName string\n\tif len(os.Args) > 1 {\n\t\tworkflowName = os.Args[1]\n\t\tfmt.Println(\"workflow name provided:\", workflowName)\n\t}\n\n\t// Define workflows map\n\tworkflowMap := map[string][]workflow.WorkflowBase{\n\t\t\"dag\": {v1_workflows.DagWorkflow(hatchet)},\n\t\t\"on-failure\": {v1_workflows.OnFailure(hatchet)},\n\t\t\"simple\": {v1_workflows.Simple(hatchet)},\n\t\t\"sleep\": {v1_workflows.DurableSleep(hatchet)},\n\t\t\"child\": {v1_workflows.Parent(hatchet), v1_workflows.Child(hatchet)},\n\t\t\"cancellation\": {v1_workflows.Cancellation(hatchet)},\n\t\t\"timeout\": {v1_workflows.Timeout(hatchet)},\n\t\t\"sticky\": {v1_workflows.Sticky(hatchet), v1_workflows.StickyDag(hatchet), v1_workflows.Child(hatchet)},\n\t\t\"retries\": {v1_workflows.Retries(hatchet), v1_workflows.RetriesWithCount(hatchet), v1_workflows.WithBackoff(hatchet)},\n\t\t\"on-cron\": {v1_workflows.OnCron(hatchet)},\n\t\t\"non-retryable\": {v1_workflows.NonRetryableError(hatchet)},\n\t\t\"priority\": {v1_workflows.Priority(hatchet)},\n\t}\n\n\t// Add an \"all\" option that registers all workflows\n\tallWorkflows := []workflow.WorkflowBase{}\n\tfor _, wfs := range workflowMap {\n\t\tallWorkflows = append(allWorkflows, wfs...)\n\t}\n\tworkflowMap[\"all\"] = allWorkflows\n\n\t// Lookup workflow from map\n\tworkflow, ok := workflowMap[workflowName]\n\tif !ok {\n\t\tfmt.Println(\"Invalid workflow name provided. Usage: go run examples/v1/worker/start.go [workflow-name]\")\n\t\tfmt.Println(\"Available workflows:\", getAvailableWorkflows(workflowMap))\n\t\tos.Exit(1)\n\t}\n\n\tvar slots int\n\tif workflowName == \"priority\" {\n\t\tslots = 1\n\t} else {\n\t\tslots = 100\n\t}\n\n\tworker, err := hatchet.Worker(\n\t\tworker.WorkerOpts{\n\t\t\tName: fmt.Sprintf(\"%s-worker\", workflowName),\n\t\t\tWorkflows: workflow,\n\t\t\tSlots: slots,\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterruptCtx, cancel := cmdutils.NewInterruptContext()\n\n\terr = worker.StartBlocking(interruptCtx)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tgo func() {\n\t\ttime.Sleep(10 * time.Second)\n\t\tcancel()\n\t}()\n}\n\n// Helper function to get available workflows as a formatted string\nfunc getAvailableWorkflows(workflowMap map[string][]workflow.WorkflowBase) string {\n\tvar workflows string\n\tcount := 0\n\tfor name := range workflowMap {\n\t\tif count > 0 {\n\t\t\tworkflows += \", \"\n\t\t}\n\t\tworkflows += fmt.Sprintf(\"'%s'\", name)\n\t\tcount++\n\t}\n\treturn workflows\n}\n",
|
||||
"source": "out/go/worker/start.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'errors\'\n\t\'time\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype CancellationInput struct{}\ntype CancellationResult struct {\n\tCompleted bool\n}\n\nfunc Cancellation(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[CancellationInput, CancellationResult] {\n\n\t// > Cancelled task\n\t// Create a task that sleeps for 10 seconds and checks if it was cancelled\n\tcancellation := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'cancellation-task\',\n\t\t}, func(ctx worker.HatchetContext, input CancellationInput) (*CancellationResult, error) {\n\t\t\t// Sleep for 10 seconds\n\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t// Check if the context was cancelled\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn nil, errors.New(\'Task was cancelled\')\n\t\t\tdefault:\n\t\t\t\t// Continue execution\n\t\t\t}\n\n\t\t\treturn &CancellationResult{\n\t\t\t\tCompleted: true,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn cancellation\n}\n',
|
||||
'source': 'out/go/workflows/cancellations.go',
|
||||
'blocks': {
|
||||
'cancelled_task': {
|
||||
'start': 22,
|
||||
'stop': 43
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype CancellationInput struct{}\ntype CancellationResult struct {\n\tCompleted bool\n}\n\nfunc Cancellation(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[CancellationInput, CancellationResult] {\n\n\t// > Cancelled task\n\t// Create a task that sleeps for 10 seconds and checks if it was cancelled\n\tcancellation := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"cancellation-task\",\n\t\t}, func(ctx worker.HatchetContext, input CancellationInput) (*CancellationResult, error) {\n\t\t\t// Sleep for 10 seconds\n\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t// Check if the context was cancelled\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn nil, errors.New(\"Task was cancelled\")\n\t\t\tdefault:\n\t\t\t\t// Continue execution\n\t\t\t}\n\n\t\t\treturn &CancellationResult{\n\t\t\t\tCompleted: true,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn cancellation\n}\n",
|
||||
"source": "out/go/workflows/cancellations.go",
|
||||
"blocks": {
|
||||
"cancelled_task": {
|
||||
"start": 22,
|
||||
"stop": 43
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype ChildInput struct {\n\tN int `json:\'n\'`\n}\n\ntype ValueOutput struct {\n\tValue int `json:\'value\'`\n}\n\ntype ParentInput struct {\n\tN int `json:\'n\'`\n}\n\ntype SumOutput struct {\n\tResult int `json:\'result\'`\n}\n\nfunc Child(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ChildInput, ValueOutput] {\n\tchild := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'child\',\n\t\t}, func(ctx worker.HatchetContext, input ChildInput) (*ValueOutput, error) {\n\t\t\treturn &ValueOutput{\n\t\t\t\tValue: input.N,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn child\n}\n\nfunc Parent(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ParentInput, SumOutput] {\n\n\tchild := Child(hatchet)\n\tparent := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'parent\',\n\t\t}, func(ctx worker.HatchetContext, input ParentInput) (*SumOutput, error) {\n\n\t\t\tsum := 0\n\n\t\t\t// Launch child workflows in parallel\n\t\t\tresults := make([]*ValueOutput, 0, input.N)\n\t\t\tfor j := 0; j < input.N; j++ {\n\t\t\t\tresult, err := child.RunAsChild(ctx, ChildInput{N: j}, workflow.RunAsChildOpts{})\n\n\t\t\t\tif err != nil {\n\t\t\t\t\t// firstErr = err\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\tresults = append(results, result)\n\n\t\t\t}\n\n\t\t\t// Sum results from all children\n\t\t\tfor _, result := range results {\n\t\t\t\tsum += result.Value\n\t\t\t}\n\n\t\t\treturn &SumOutput{\n\t\t\t\tResult: sum,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn parent\n}\n',
|
||||
'source': 'out/go/workflows/child-workflows.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype ChildInput struct {\n\tN int `json:\"n\"`\n}\n\ntype ValueOutput struct {\n\tValue int `json:\"value\"`\n}\n\ntype ParentInput struct {\n\tN int `json:\"n\"`\n}\n\ntype SumOutput struct {\n\tResult int `json:\"result\"`\n}\n\nfunc Child(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ChildInput, ValueOutput] {\n\tchild := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"child\",\n\t\t}, func(ctx worker.HatchetContext, input ChildInput) (*ValueOutput, error) {\n\t\t\treturn &ValueOutput{\n\t\t\t\tValue: input.N,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn child\n}\n\nfunc Parent(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ParentInput, SumOutput] {\n\n\tchild := Child(hatchet)\n\tparent := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"parent\",\n\t\t}, func(ctx worker.HatchetContext, input ParentInput) (*SumOutput, error) {\n\n\t\t\tsum := 0\n\n\t\t\t// Launch child workflows in parallel\n\t\t\tresults := make([]*ValueOutput, 0, input.N)\n\t\t\tfor j := 0; j < input.N; j++ {\n\t\t\t\tresult, err := child.RunAsChild(ctx, ChildInput{N: j}, workflow.RunAsChildOpts{})\n\n\t\t\t\tif err != nil {\n\t\t\t\t\t// firstErr = err\n\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\tresults = append(results, result)\n\n\t\t\t}\n\n\t\t\t// Sum results from all children\n\t\t\tfor _, result := range results {\n\t\t\t\tsum += result.Value\n\t\t\t}\n\n\t\t\treturn &SumOutput{\n\t\t\t\tResult: sum,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn parent\n}\n",
|
||||
"source": "out/go/workflows/child-workflows.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,20 +1,20 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'math/rand\'\n\t\'time\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\t\'github.com/hatchet-dev/hatchet/pkg/client/types\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype ConcurrencyInput struct {\n\tMessage string\n\tTier string\n\tAccount string\n}\n\ntype TransformedOutput struct {\n\tTransformedMessage string\n}\n\nfunc ConcurrencyRoundRobin(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ConcurrencyInput, TransformedOutput] {\n\t// > Concurrency Strategy With Key\n\tvar maxRuns int32 = 1\n\tstrategy := types.GroupRoundRobin\n\n\tconcurrency := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'simple-concurrency\',\n\t\t\tConcurrency: []*types.Concurrency{\n\t\t\t\t{\n\t\t\t\t\tExpression: \'input.GroupKey\',\n\t\t\t\t\tMaxRuns: &maxRuns,\n\t\t\t\t\tLimitStrategy: &strategy,\n\t\t\t\t},\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input ConcurrencyInput) (*TransformedOutput, error) {\n\t\t\t// Random sleep between 200ms and 1000ms\n\t\t\ttime.Sleep(time.Duration(200+rand.Intn(800)) * time.Millisecond)\n\n\t\t\treturn &TransformedOutput{\n\t\t\t\tTransformedMessage: input.Message,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn concurrency\n}\n\nfunc MultipleConcurrencyKeys(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ConcurrencyInput, TransformedOutput] {\n\t// > Multiple Concurrency Keys\n\tstrategy := types.GroupRoundRobin\n\tvar maxRuns int32 = 20\n\n\tconcurrency := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'simple-concurrency\',\n\t\t\tConcurrency: []*types.Concurrency{\n\t\t\t\t{\n\t\t\t\t\tExpression: \'input.Tier\',\n\t\t\t\t\tMaxRuns: &maxRuns,\n\t\t\t\t\tLimitStrategy: &strategy,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tExpression: \'input.Account\',\n\t\t\t\t\tMaxRuns: &maxRuns,\n\t\t\t\t\tLimitStrategy: &strategy,\n\t\t\t\t},\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input ConcurrencyInput) (*TransformedOutput, error) {\n\t\t\t// Random sleep between 200ms and 1000ms\n\t\t\ttime.Sleep(time.Duration(200+rand.Intn(800)) * time.Millisecond)\n\n\t\t\treturn &TransformedOutput{\n\t\t\t\tTransformedMessage: input.Message,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn concurrency\n}\n',
|
||||
'source': 'out/go/workflows/concurrency-rr.go',
|
||||
'blocks': {
|
||||
'concurrency_strategy_with_key': {
|
||||
'start': 27,
|
||||
'stop': 49
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"math/rand\"\n\t\"time\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\t\"github.com/hatchet-dev/hatchet/pkg/client/types\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype ConcurrencyInput struct {\n\tMessage string\n\tTier string\n\tAccount string\n}\n\ntype TransformedOutput struct {\n\tTransformedMessage string\n}\n\nfunc ConcurrencyRoundRobin(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ConcurrencyInput, TransformedOutput] {\n\t// > Concurrency Strategy With Key\n\tvar maxRuns int32 = 1\n\tstrategy := types.GroupRoundRobin\n\n\tconcurrency := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"simple-concurrency\",\n\t\t\tConcurrency: []*types.Concurrency{\n\t\t\t\t{\n\t\t\t\t\tExpression: \"input.GroupKey\",\n\t\t\t\t\tMaxRuns: &maxRuns,\n\t\t\t\t\tLimitStrategy: &strategy,\n\t\t\t\t},\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input ConcurrencyInput) (*TransformedOutput, error) {\n\t\t\t// Random sleep between 200ms and 1000ms\n\t\t\ttime.Sleep(time.Duration(200+rand.Intn(800)) * time.Millisecond)\n\n\t\t\treturn &TransformedOutput{\n\t\t\t\tTransformedMessage: input.Message,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn concurrency\n}\n\nfunc MultipleConcurrencyKeys(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[ConcurrencyInput, TransformedOutput] {\n\t// > Multiple Concurrency Keys\n\tstrategy := types.GroupRoundRobin\n\tvar maxRuns int32 = 20\n\n\tconcurrency := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"simple-concurrency\",\n\t\t\tConcurrency: []*types.Concurrency{\n\t\t\t\t{\n\t\t\t\t\tExpression: \"input.Tier\",\n\t\t\t\t\tMaxRuns: &maxRuns,\n\t\t\t\t\tLimitStrategy: &strategy,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tExpression: \"input.Account\",\n\t\t\t\t\tMaxRuns: &maxRuns,\n\t\t\t\t\tLimitStrategy: &strategy,\n\t\t\t\t},\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input ConcurrencyInput) (*TransformedOutput, error) {\n\t\t\t// Random sleep between 200ms and 1000ms\n\t\t\ttime.Sleep(time.Duration(200+rand.Intn(800)) * time.Millisecond)\n\n\t\t\treturn &TransformedOutput{\n\t\t\t\tTransformedMessage: input.Message,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn concurrency\n}\n",
|
||||
"source": "out/go/workflows/concurrency-rr.go",
|
||||
"blocks": {
|
||||
"concurrency_strategy_with_key": {
|
||||
"start": 27,
|
||||
"stop": 49
|
||||
},
|
||||
'multiple_concurrency_keys': {
|
||||
'start': 56,
|
||||
'stop': 83
|
||||
"multiple_concurrency_keys": {
|
||||
"start": 56,
|
||||
"stop": 83
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'fmt\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype DagWithConditionsInput struct {\n\tMessage string\n}\n\ntype DagWithConditionsResult struct {\n\tStep1 SimpleOutput\n\tStep2 SimpleOutput\n}\n\ntype conditionOpts = create.WorkflowTask[DagWithConditionsInput, DagWithConditionsResult]\n\nfunc DagWithConditionsWorkflow(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DagWithConditionsInput, DagWithConditionsResult] {\n\n\tsimple := factory.NewWorkflow[DagWithConditionsInput, DagWithConditionsResult](\n\t\tcreate.WorkflowCreateOpts[DagWithConditionsInput]{\n\t\t\tName: \'simple-dag\',\n\t\t},\n\t\thatchet,\n\t)\n\n\tstep1 := simple.Task(\n\t\tconditionOpts{\n\t\t\tName: \'Step1\',\n\t\t}, func(ctx worker.HatchetContext, input DagWithConditionsInput) (interface{}, error) {\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 1,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\tsimple.Task(\n\t\tconditionOpts{\n\t\t\tName: \'Step2\',\n\t\t\tParents: []create.NamedTask{\n\t\t\t\tstep1,\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input DagWithConditionsInput) (interface{}, error) {\n\n\t\t\tvar step1Output SimpleOutput\n\t\t\terr := ctx.ParentOutput(step1, &step1Output)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tfmt.Println(step1Output.Step)\n\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 2,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n',
|
||||
'source': 'out/go/workflows/dag-with-conditions.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype DagWithConditionsInput struct {\n\tMessage string\n}\n\ntype DagWithConditionsResult struct {\n\tStep1 SimpleOutput\n\tStep2 SimpleOutput\n}\n\ntype conditionOpts = create.WorkflowTask[DagWithConditionsInput, DagWithConditionsResult]\n\nfunc DagWithConditionsWorkflow(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DagWithConditionsInput, DagWithConditionsResult] {\n\n\tsimple := factory.NewWorkflow[DagWithConditionsInput, DagWithConditionsResult](\n\t\tcreate.WorkflowCreateOpts[DagWithConditionsInput]{\n\t\t\tName: \"simple-dag\",\n\t\t},\n\t\thatchet,\n\t)\n\n\tstep1 := simple.Task(\n\t\tconditionOpts{\n\t\t\tName: \"Step1\",\n\t\t}, func(ctx worker.HatchetContext, input DagWithConditionsInput) (interface{}, error) {\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 1,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\tsimple.Task(\n\t\tconditionOpts{\n\t\t\tName: \"Step2\",\n\t\t\tParents: []create.NamedTask{\n\t\t\t\tstep1,\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input DagWithConditionsInput) (interface{}, error) {\n\n\t\t\tvar step1Output SimpleOutput\n\t\t\terr := ctx.ParentOutput(step1, &step1Output)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tfmt.Println(step1Output.Step)\n\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 2,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n",
|
||||
"source": "out/go/workflows/dag-with-conditions.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype DagInput struct {\n\tMessage string\n}\n\ntype SimpleOutput struct {\n\tStep int\n}\n\ntype DagResult struct {\n\tStep1 SimpleOutput\n\tStep2 SimpleOutput\n}\n\nfunc DagWorkflow(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DagInput, DagResult] {\n\t// > Declaring a Workflow\n\tsimple := factory.NewWorkflow[DagInput, DagResult](\n\t\tcreate.WorkflowCreateOpts[DagInput]{\n\t\t\tName: \'simple-dag\',\n\t\t},\n\t\thatchet,\n\t)\n\n\t// > Defining a Task\n\tsimple.Task(\n\t\tcreate.WorkflowTask[DagInput, DagResult]{\n\t\t\tName: \'step\',\n\t\t}, func(ctx worker.HatchetContext, input DagInput) (interface{}, error) {\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 1,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\t// > Adding a Task with a parent\n\tstep1 := simple.Task(\n\t\tcreate.WorkflowTask[DagInput, DagResult]{\n\t\t\tName: \'step-1\',\n\t\t}, func(ctx worker.HatchetContext, input DagInput) (interface{}, error) {\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 1,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\tsimple.Task(\n\t\tcreate.WorkflowTask[DagInput, DagResult]{\n\t\t\tName: \'step-2\',\n\t\t\tParents: []create.NamedTask{\n\t\t\t\tstep1,\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input DagInput) (interface{}, error) {\n\t\t\t// Get the output of the parent task\n\t\t\tvar step1Output SimpleOutput\n\t\t\terr := ctx.ParentOutput(step1, &step1Output)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 2,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n',
|
||||
'source': 'out/go/workflows/dag.go',
|
||||
'blocks': {
|
||||
'declaring_a_workflow': {
|
||||
'start': 26,
|
||||
'stop': 31
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype DagInput struct {\n\tMessage string\n}\n\ntype SimpleOutput struct {\n\tStep int\n}\n\ntype DagResult struct {\n\tStep1 SimpleOutput\n\tStep2 SimpleOutput\n}\n\nfunc DagWorkflow(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DagInput, DagResult] {\n\t// > Declaring a Workflow\n\tsimple := factory.NewWorkflow[DagInput, DagResult](\n\t\tcreate.WorkflowCreateOpts[DagInput]{\n\t\t\tName: \"simple-dag\",\n\t\t},\n\t\thatchet,\n\t)\n\n\t// > Defining a Task\n\tsimple.Task(\n\t\tcreate.WorkflowTask[DagInput, DagResult]{\n\t\t\tName: \"step\",\n\t\t}, func(ctx worker.HatchetContext, input DagInput) (interface{}, error) {\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 1,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\t// > Adding a Task with a parent\n\tstep1 := simple.Task(\n\t\tcreate.WorkflowTask[DagInput, DagResult]{\n\t\t\tName: \"step-1\",\n\t\t}, func(ctx worker.HatchetContext, input DagInput) (interface{}, error) {\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 1,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\tsimple.Task(\n\t\tcreate.WorkflowTask[DagInput, DagResult]{\n\t\t\tName: \"step-2\",\n\t\t\tParents: []create.NamedTask{\n\t\t\t\tstep1,\n\t\t\t},\n\t\t}, func(ctx worker.HatchetContext, input DagInput) (interface{}, error) {\n\t\t\t// Get the output of the parent task\n\t\t\tvar step1Output SimpleOutput\n\t\t\terr := ctx.ParentOutput(step1, &step1Output)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &SimpleOutput{\n\t\t\t\tStep: 2,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n",
|
||||
"source": "out/go/workflows/dag.go",
|
||||
"blocks": {
|
||||
"declaring_a_workflow": {
|
||||
"start": 26,
|
||||
"stop": 31
|
||||
},
|
||||
'defining_a_task': {
|
||||
'start': 34,
|
||||
'stop': 42
|
||||
"defining_a_task": {
|
||||
"start": 34,
|
||||
"stop": 42
|
||||
},
|
||||
'adding_a_task_with_a_parent': {
|
||||
'start': 45,
|
||||
'stop': 73
|
||||
"adding_a_task_with_a_parent": {
|
||||
"start": 45,
|
||||
"stop": 73
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype DurableEventInput struct {\n\tMessage string\n}\n\ntype EventData struct {\n\tMessage string\n}\n\ntype DurableEventOutput struct {\n\tData EventData\n}\n\nfunc DurableEvent(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DurableEventInput, DurableEventOutput] {\n\t// > Durable Event\n\tdurableEventTask := factory.NewDurableTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'durable-event\',\n\t\t},\n\t\tfunc(ctx worker.DurableHatchetContext, input DurableEventInput) (*DurableEventOutput, error) {\n\t\t\teventData, err := ctx.WaitForEvent(\'user:update\', \'\')\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tv := EventData{}\n\t\t\terr = eventData.Unmarshal(&v)\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &DurableEventOutput{\n\t\t\t\tData: v,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\tfactory.NewDurableTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'durable-event\',\n\t\t},\n\t\tfunc(ctx worker.DurableHatchetContext, input DurableEventInput) (*DurableEventOutput, error) {\n\t\t\t// > Durable Event With Filter\n\t\t\teventData, err := ctx.WaitForEvent(\'user:update\', \'input.user_id == \'1234\'\')\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tv := EventData{}\n\t\t\terr = eventData.Unmarshal(&v)\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &DurableEventOutput{\n\t\t\t\tData: v,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn durableEventTask\n}\n',
|
||||
'source': 'out/go/workflows/durable-event.go',
|
||||
'blocks': {
|
||||
'durable_event': {
|
||||
'start': 25,
|
||||
'stop': 48
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype DurableEventInput struct {\n\tMessage string\n}\n\ntype EventData struct {\n\tMessage string\n}\n\ntype DurableEventOutput struct {\n\tData EventData\n}\n\nfunc DurableEvent(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DurableEventInput, DurableEventOutput] {\n\t// > Durable Event\n\tdurableEventTask := factory.NewDurableTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"durable-event\",\n\t\t},\n\t\tfunc(ctx worker.DurableHatchetContext, input DurableEventInput) (*DurableEventOutput, error) {\n\t\t\teventData, err := ctx.WaitForEvent(\"user:update\", \"\")\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tv := EventData{}\n\t\t\terr = eventData.Unmarshal(&v)\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &DurableEventOutput{\n\t\t\t\tData: v,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\tfactory.NewDurableTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"durable-event\",\n\t\t},\n\t\tfunc(ctx worker.DurableHatchetContext, input DurableEventInput) (*DurableEventOutput, error) {\n\t\t\t// > Durable Event With Filter\n\t\t\teventData, err := ctx.WaitForEvent(\"user:update\", \"input.user_id == '1234'\")\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tv := EventData{}\n\t\t\terr = eventData.Unmarshal(&v)\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &DurableEventOutput{\n\t\t\t\tData: v,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn durableEventTask\n}\n",
|
||||
"source": "out/go/workflows/durable-event.go",
|
||||
"blocks": {
|
||||
"durable_event": {
|
||||
"start": 25,
|
||||
"stop": 48
|
||||
},
|
||||
'durable_event_with_filter': {
|
||||
'start': 56,
|
||||
'stop': 56
|
||||
"durable_event_with_filter": {
|
||||
"start": 56,
|
||||
"stop": 56
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'strings\'\n\t\'time\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype DurableSleepInput struct {\n\tMessage string\n}\n\ntype DurableSleepOutput struct {\n\tTransformedMessage string\n}\n\nfunc DurableSleep(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DurableSleepInput, DurableSleepOutput] {\n\t// > Durable Sleep\n\tsimple := factory.NewDurableTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'durable-sleep\',\n\t\t},\n\t\tfunc(ctx worker.DurableHatchetContext, input DurableSleepInput) (*DurableSleepOutput, error) {\n\t\t\t_, err := ctx.SleepFor(10 * time.Second)\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &DurableSleepOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn simple\n}\n',
|
||||
'source': 'out/go/workflows/durable-sleep.go',
|
||||
'blocks': {
|
||||
'durable_sleep': {
|
||||
'start': 24,
|
||||
'stop': 40
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype DurableSleepInput struct {\n\tMessage string\n}\n\ntype DurableSleepOutput struct {\n\tTransformedMessage string\n}\n\nfunc DurableSleep(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[DurableSleepInput, DurableSleepOutput] {\n\t// > Durable Sleep\n\tsimple := factory.NewDurableTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"durable-sleep\",\n\t\t},\n\t\tfunc(ctx worker.DurableHatchetContext, input DurableSleepInput) (*DurableSleepOutput, error) {\n\t\t\t_, err := ctx.SleepFor(10 * time.Second)\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &DurableSleepOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn simple\n}\n",
|
||||
"source": "out/go/workflows/durable-sleep.go",
|
||||
"blocks": {
|
||||
"durable_sleep": {
|
||||
"start": 24,
|
||||
"stop": 40
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'errors\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype NonRetryableInput struct{}\ntype NonRetryableResult struct{}\n\n// NonRetryableError returns a workflow which throws a non-retryable error\nfunc NonRetryableError(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[NonRetryableInput, NonRetryableResult] {\n\t// > Non Retryable Error\n\tretries := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'non-retryable-task\',\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input NonRetryableInput) (*NonRetryableResult, error) {\n\t\t\treturn nil, worker.NewNonRetryableError(errors.New(\'intentional failure\'))\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn retries\n}\n',
|
||||
'source': 'out/go/workflows/non-retryable-error.go',
|
||||
'blocks': {
|
||||
'non_retryable_error': {
|
||||
'start': 19,
|
||||
'stop': 27
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"errors\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype NonRetryableInput struct{}\ntype NonRetryableResult struct{}\n\n// NonRetryableError returns a workflow which throws a non-retryable error\nfunc NonRetryableError(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[NonRetryableInput, NonRetryableResult] {\n\t// > Non Retryable Error\n\tretries := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"non-retryable-task\",\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input NonRetryableInput) (*NonRetryableResult, error) {\n\t\t\treturn nil, worker.NewNonRetryableError(errors.New(\"intentional failure\"))\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn retries\n}\n",
|
||||
"source": "out/go/workflows/non-retryable-error.go",
|
||||
"blocks": {
|
||||
"non_retryable_error": {
|
||||
"start": 19,
|
||||
"stop": 27
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'strings\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype OnCronInput struct {\n\tMessage string `json:\'Message\'`\n}\n\ntype JobResult struct {\n\tTransformedMessage string `json:\'TransformedMessage\'`\n}\n\ntype OnCronOutput struct {\n\tJob JobResult `json:\'job\'`\n}\n\n// > Workflow Definition Cron Trigger\nfunc OnCron(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[OnCronInput, OnCronOutput] {\n\t// Create a standalone task that transforms a message\n\tcronTask := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'on-cron-task\',\n\t\t\t// 👀 add a cron expression\n\t\t\tOnCron: []string{\'0 0 * * *\'}, // Run every day at midnight\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input OnCronInput) (*OnCronOutput, error) {\n\t\t\treturn &OnCronOutput{\n\t\t\t\tJob: JobResult{\n\t\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t\t},\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn cronTask\n}\n\n',
|
||||
'source': 'out/go/workflows/on-cron.go',
|
||||
'blocks': {
|
||||
'workflow_definition_cron_trigger': {
|
||||
'start': 26,
|
||||
'stop': 46
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"strings\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype OnCronInput struct {\n\tMessage string `json:\"Message\"`\n}\n\ntype JobResult struct {\n\tTransformedMessage string `json:\"TransformedMessage\"`\n}\n\ntype OnCronOutput struct {\n\tJob JobResult `json:\"job\"`\n}\n\n// > Workflow Definition Cron Trigger\nfunc OnCron(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[OnCronInput, OnCronOutput] {\n\t// Create a standalone task that transforms a message\n\tcronTask := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"on-cron-task\",\n\t\t\t// 👀 add a cron expression\n\t\t\tOnCron: []string{\"0 0 * * *\"}, // Run every day at midnight\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input OnCronInput) (*OnCronOutput, error) {\n\t\t\treturn &OnCronOutput{\n\t\t\t\tJob: JobResult{\n\t\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t\t},\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn cronTask\n}\n\n",
|
||||
"source": "out/go/workflows/on-cron.go",
|
||||
"blocks": {
|
||||
"workflow_definition_cron_trigger": {
|
||||
"start": 26,
|
||||
"stop": 46
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'strings\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype EventInput struct {\n\tMessage string\n}\n\ntype LowerTaskOutput struct {\n\tTransformedMessage string\n}\n\ntype UpperTaskOutput struct {\n\tTransformedMessage string\n}\n\n// > Run workflow on event\nconst SimpleEvent = \'simple-event:create\'\n\nfunc Lower(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[EventInput, LowerTaskOutput] {\n\treturn factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'lower\',\n\t\t\t// 👀 Declare the event that will trigger the workflow\n\t\t\tOnEvents: []string{SimpleEvent},\n\t\t}, func(ctx worker.HatchetContext, input EventInput) (*LowerTaskOutput, error) {\n\t\t\t// Transform the input message to lowercase\n\t\t\treturn &LowerTaskOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n}\n\n\nfunc Upper(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[EventInput, UpperTaskOutput] {\n\treturn factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'upper\',\n\t\t\tOnEvents: []string{SimpleEvent},\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input EventInput) (*UpperTaskOutput, error) {\n\t\t\treturn &UpperTaskOutput{\n\t\t\t\tTransformedMessage: strings.ToUpper(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n}\n',
|
||||
'source': 'out/go/workflows/on-event.go',
|
||||
'blocks': {
|
||||
'run_workflow_on_event': {
|
||||
'start': 26,
|
||||
'stop': 43
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"strings\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype EventInput struct {\n\tMessage string\n}\n\ntype LowerTaskOutput struct {\n\tTransformedMessage string\n}\n\ntype UpperTaskOutput struct {\n\tTransformedMessage string\n}\n\n// > Run workflow on event\nconst SimpleEvent = \"simple-event:create\"\n\nfunc Lower(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[EventInput, LowerTaskOutput] {\n\treturn factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"lower\",\n\t\t\t// 👀 Declare the event that will trigger the workflow\n\t\t\tOnEvents: []string{SimpleEvent},\n\t\t}, func(ctx worker.HatchetContext, input EventInput) (*LowerTaskOutput, error) {\n\t\t\t// Transform the input message to lowercase\n\t\t\treturn &LowerTaskOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n}\n\n\nfunc Upper(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[EventInput, UpperTaskOutput] {\n\treturn factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"upper\",\n\t\t\tOnEvents: []string{SimpleEvent},\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input EventInput) (*UpperTaskOutput, error) {\n\t\t\treturn &UpperTaskOutput{\n\t\t\t\tTransformedMessage: strings.ToUpper(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n}\n",
|
||||
"source": "out/go/workflows/on-event.go",
|
||||
"blocks": {
|
||||
"run_workflow_on_event": {
|
||||
"start": 26,
|
||||
"stop": 43
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'errors\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype AlwaysFailsOutput struct {\n\tTransformedMessage string\n}\n\ntype OnFailureOutput struct {\n\tFailureRan bool\n}\n\ntype OnFailureSuccessResult struct {\n\tAlwaysFails AlwaysFailsOutput\n}\n\nfunc OnFailure(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[any, OnFailureSuccessResult] {\n\n\tsimple := factory.NewWorkflow[any, OnFailureSuccessResult](\n\t\tcreate.WorkflowCreateOpts[any]{\n\t\t\tName: \'on-failure\',\n\t\t},\n\t\thatchet,\n\t)\n\n\tsimple.Task(\n\t\tcreate.WorkflowTask[any, OnFailureSuccessResult]{\n\t\t\tName: \'AlwaysFails\',\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, _ any) (interface{}, error) {\n\t\t\treturn &AlwaysFailsOutput{\n\t\t\t\tTransformedMessage: \'always fails\',\n\t\t\t}, errors.New(\'always fails\')\n\t\t},\n\t)\n\n\tsimple.OnFailure(\n\t\tcreate.WorkflowOnFailureTask[any, OnFailureSuccessResult]{},\n\t\tfunc(ctx worker.HatchetContext, _ any) (interface{}, error) {\n\t\t\treturn &OnFailureOutput{\n\t\t\t\tFailureRan: true,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n',
|
||||
'source': 'out/go/workflows/on-failure.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"errors\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype AlwaysFailsOutput struct {\n\tTransformedMessage string\n}\n\ntype OnFailureOutput struct {\n\tFailureRan bool\n}\n\ntype OnFailureSuccessResult struct {\n\tAlwaysFails AlwaysFailsOutput\n}\n\nfunc OnFailure(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[any, OnFailureSuccessResult] {\n\n\tsimple := factory.NewWorkflow[any, OnFailureSuccessResult](\n\t\tcreate.WorkflowCreateOpts[any]{\n\t\t\tName: \"on-failure\",\n\t\t},\n\t\thatchet,\n\t)\n\n\tsimple.Task(\n\t\tcreate.WorkflowTask[any, OnFailureSuccessResult]{\n\t\t\tName: \"AlwaysFails\",\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, _ any) (interface{}, error) {\n\t\t\treturn &AlwaysFailsOutput{\n\t\t\t\tTransformedMessage: \"always fails\",\n\t\t\t}, errors.New(\"always fails\")\n\t\t},\n\t)\n\n\tsimple.OnFailure(\n\t\tcreate.WorkflowOnFailureTask[any, OnFailureSuccessResult]{},\n\t\tfunc(ctx worker.HatchetContext, _ any) (interface{}, error) {\n\t\t\treturn &OnFailureOutput{\n\t\t\t\tFailureRan: true,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn simple\n}\n",
|
||||
"source": "out/go/workflows/on-failure.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'time\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype PriorityInput struct {\n\tUserId string `json:\'userId\'`\n}\n\ntype PriorityOutput struct {\n\tTransformedMessage string `json:\'TransformedMessage\'`\n}\n\ntype Result struct {\n\tStep PriorityOutput\n}\n\nfunc Priority(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[PriorityInput, Result] {\n\t// Create a standalone task that transforms a message\n\n\t// > Default priority\n\tdefaultPriority := int32(1)\n\n\tworkflow := factory.NewWorkflow[PriorityInput, Result](\n\t\tcreate.WorkflowCreateOpts[PriorityInput]{\n\t\t\tName: \'priority\',\n\t\t\tDefaultPriority: &defaultPriority,\n\t\t},\n\t\thatchet,\n\t)\n\n\t// > Defining a Task\n\tworkflow.Task(\n\t\tcreate.WorkflowTask[PriorityInput, Result]{\n\t\t\tName: \'step\',\n\t\t}, func(ctx worker.HatchetContext, input PriorityInput) (interface{}, error) {\n\t\t\ttime.Sleep(time.Second * 5)\n\t\t\treturn &PriorityOutput{\n\t\t\t\tTransformedMessage: input.UserId,\n\t\t\t}, nil\n\t\t},\n\t)\n\treturn workflow\n}\n\n',
|
||||
'source': 'out/go/workflows/priority.go',
|
||||
'blocks': {
|
||||
'default_priority': {
|
||||
'start': 29,
|
||||
'stop': 37
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"time\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype PriorityInput struct {\n\tUserId string `json:\"userId\"`\n}\n\ntype PriorityOutput struct {\n\tTransformedMessage string `json:\"TransformedMessage\"`\n}\n\ntype Result struct {\n\tStep PriorityOutput\n}\n\nfunc Priority(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[PriorityInput, Result] {\n\t// Create a standalone task that transforms a message\n\n\t// > Default priority\n\tdefaultPriority := int32(1)\n\n\tworkflow := factory.NewWorkflow[PriorityInput, Result](\n\t\tcreate.WorkflowCreateOpts[PriorityInput]{\n\t\t\tName: \"priority\",\n\t\t\tDefaultPriority: &defaultPriority,\n\t\t},\n\t\thatchet,\n\t)\n\n\t// > Defining a Task\n\tworkflow.Task(\n\t\tcreate.WorkflowTask[PriorityInput, Result]{\n\t\t\tName: \"step\",\n\t\t}, func(ctx worker.HatchetContext, input PriorityInput) (interface{}, error) {\n\t\t\ttime.Sleep(time.Second * 5)\n\t\t\treturn &PriorityOutput{\n\t\t\t\tTransformedMessage: input.UserId,\n\t\t\t}, nil\n\t\t},\n\t)\n\treturn workflow\n}\n\n",
|
||||
"source": "out/go/workflows/priority.go",
|
||||
"blocks": {
|
||||
"default_priority": {
|
||||
"start": 29,
|
||||
"stop": 37
|
||||
},
|
||||
'defining_a_task': {
|
||||
'start': 40,
|
||||
'stop': 49
|
||||
"defining_a_task": {
|
||||
"start": 40,
|
||||
"stop": 49
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'strings\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\t\'github.com/hatchet-dev/hatchet/pkg/client/types\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/features\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype RateLimitInput struct {\n\tUserId string `json:\'userId\'`\n}\n\ntype RateLimitOutput struct {\n\tTransformedMessage string `json:\'TransformedMessage\'`\n}\n\nfunc upsertRateLimit(hatchet v1.HatchetClient) {\n\t// > Upsert Rate Limit\n\thatchet.RateLimits().Upsert(\n\t\tfeatures.CreateRatelimitOpts{\n\t\t\tKey: \'api-service-rate-limit\',\n\t\t\tLimit: 10,\n\t\t\tDuration: types.Second,\n\t\t},\n\t)\n}\n\n// > Static Rate Limit\nfunc StaticRateLimit(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RateLimitInput, RateLimitOutput] {\n\t// Create a standalone task that transforms a message\n\n\t// define the parameters for the rate limit\n\trateLimitKey := \'api-service-rate-limit\'\n\tunits := 1\n\n\trateLimitTask := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'rate-limit-task\',\n\t\t\t// 👀 add a static rate limit\n\t\t\tRateLimits: []*types.RateLimit{\n\t\t\t\t{\n\t\t\t\t\tKey: rateLimitKey,\n\t\t\t\t\tUnits: &units,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input RateLimitInput) (*RateLimitOutput, error) {\n\t\t\treturn &RateLimitOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.UserId),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn rateLimitTask\n}\n\n\n// > Dynamic Rate Limit\nfunc RateLimit(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RateLimitInput, RateLimitOutput] {\n\t// Create a standalone task that transforms a message\n\n\t// define the parameters for the rate limit\n\texpression := \'input.userId\'\n\tunits := 1\n\tduration := types.Second\n\n\trateLimitTask := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'rate-limit-task\',\n\t\t\t// 👀 add a dynamic rate limit\n\t\t\tRateLimits: []*types.RateLimit{\n\t\t\t\t{\n\t\t\t\t\tKeyExpr: &expression,\n\t\t\t\t\tUnits: &units,\n\t\t\t\t\tDuration: &duration,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input RateLimitInput) (*RateLimitOutput, error) {\n\t\t\treturn &RateLimitOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.UserId),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn rateLimitTask\n}\n\n',
|
||||
'source': 'out/go/workflows/ratelimit.go',
|
||||
'blocks': {
|
||||
'upsert_rate_limit': {
|
||||
'start': 25,
|
||||
'stop': 31
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"strings\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\t\"github.com/hatchet-dev/hatchet/pkg/client/types\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/features\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype RateLimitInput struct {\n\tUserId string `json:\"userId\"`\n}\n\ntype RateLimitOutput struct {\n\tTransformedMessage string `json:\"TransformedMessage\"`\n}\n\nfunc upsertRateLimit(hatchet v1.HatchetClient) {\n\t// > Upsert Rate Limit\n\thatchet.RateLimits().Upsert(\n\t\tfeatures.CreateRatelimitOpts{\n\t\t\tKey: \"api-service-rate-limit\",\n\t\t\tLimit: 10,\n\t\t\tDuration: types.Second,\n\t\t},\n\t)\n}\n\n// > Static Rate Limit\nfunc StaticRateLimit(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RateLimitInput, RateLimitOutput] {\n\t// Create a standalone task that transforms a message\n\n\t// define the parameters for the rate limit\n\trateLimitKey := \"api-service-rate-limit\"\n\tunits := 1\n\n\trateLimitTask := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"rate-limit-task\",\n\t\t\t// 👀 add a static rate limit\n\t\t\tRateLimits: []*types.RateLimit{\n\t\t\t\t{\n\t\t\t\t\tKey: rateLimitKey,\n\t\t\t\t\tUnits: &units,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input RateLimitInput) (*RateLimitOutput, error) {\n\t\t\treturn &RateLimitOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.UserId),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn rateLimitTask\n}\n\n\n// > Dynamic Rate Limit\nfunc RateLimit(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RateLimitInput, RateLimitOutput] {\n\t// Create a standalone task that transforms a message\n\n\t// define the parameters for the rate limit\n\texpression := \"input.userId\"\n\tunits := 1\n\tduration := types.Second\n\n\trateLimitTask := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"rate-limit-task\",\n\t\t\t// 👀 add a dynamic rate limit\n\t\t\tRateLimits: []*types.RateLimit{\n\t\t\t\t{\n\t\t\t\t\tKeyExpr: &expression,\n\t\t\t\t\tUnits: &units,\n\t\t\t\t\tDuration: &duration,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input RateLimitInput) (*RateLimitOutput, error) {\n\t\t\treturn &RateLimitOutput{\n\t\t\t\tTransformedMessage: strings.ToLower(input.UserId),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn rateLimitTask\n}\n\n",
|
||||
"source": "out/go/workflows/ratelimit.go",
|
||||
"blocks": {
|
||||
"upsert_rate_limit": {
|
||||
"start": 25,
|
||||
"stop": 31
|
||||
},
|
||||
'static_rate_limit': {
|
||||
'start': 35,
|
||||
'stop': 63
|
||||
"static_rate_limit": {
|
||||
"start": 35,
|
||||
"stop": 63
|
||||
},
|
||||
'dynamic_rate_limit': {
|
||||
'start': 66,
|
||||
'stop': 96
|
||||
"dynamic_rate_limit": {
|
||||
"start": 66,
|
||||
"stop": 96
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'errors\'\n\t\'fmt\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype RetriesInput struct{}\ntype RetriesResult struct{}\n\n// Simple retries example that always fails\nfunc Retries(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RetriesInput, RetriesResult] {\n\t// > Simple Step Retries\n\tretries := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'retries-task\',\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input RetriesInput) (*RetriesResult, error) {\n\t\t\treturn nil, errors.New(\'intentional failure\')\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn retries\n}\n\ntype RetriesWithCountInput struct{}\ntype RetriesWithCountResult struct {\n\tMessage string `json:\'message\'`\n}\n\n// Retries example that succeeds after a certain number of retries\nfunc RetriesWithCount(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RetriesWithCountInput, RetriesWithCountResult] {\n\t// > Retries with Count\n\tretriesWithCount := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'fail-twice-task\',\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input RetriesWithCountInput) (*RetriesWithCountResult, error) {\n\t\t\t// Get the current retry count\n\t\t\tretryCount := ctx.RetryCount()\n\n\t\t\tfmt.Printf(\'Retry count: %d\\n\', retryCount)\n\n\t\t\tif retryCount < 2 {\n\t\t\t\treturn nil, errors.New(\'intentional failure\')\n\t\t\t}\n\n\t\t\treturn &RetriesWithCountResult{\n\t\t\t\tMessage: \'success\',\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn retriesWithCount\n}\n\ntype BackoffInput struct{}\ntype BackoffResult struct{}\n\n// Retries example with simple backoff (no configuration in this API version)\nfunc WithBackoff(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[BackoffInput, BackoffResult] {\n\t// > Retries with Backoff\n\twithBackoff := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'with-backoff-task\',\n\t\t\t// 👀 Maximum number of seconds to wait between retries\n\t\t\tRetries: 3,\n\t\t\t// 👀 Factor to increase the wait time between retries.\n\t\t\tRetryBackoffFactor: 2,\n\t\t\t// 👀 Maximum number of seconds to wait between retries\n\t\t\t// This sequence will be 2s, 4s, 8s, 10s, 10s, 10s... due to the maxSeconds limit\n\t\t\tRetryMaxBackoffSeconds: 10,\n\t\t}, func(ctx worker.HatchetContext, input BackoffInput) (*BackoffResult, error) {\n\t\t\treturn nil, errors.New(\'intentional failure\')\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn withBackoff\n}\n',
|
||||
'source': 'out/go/workflows/retries.go',
|
||||
'blocks': {
|
||||
'simple_step_retries': {
|
||||
'start': 20,
|
||||
'stop': 28
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype RetriesInput struct{}\ntype RetriesResult struct{}\n\n// Simple retries example that always fails\nfunc Retries(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RetriesInput, RetriesResult] {\n\t// > Simple Step Retries\n\tretries := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"retries-task\",\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input RetriesInput) (*RetriesResult, error) {\n\t\t\treturn nil, errors.New(\"intentional failure\")\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn retries\n}\n\ntype RetriesWithCountInput struct{}\ntype RetriesWithCountResult struct {\n\tMessage string `json:\"message\"`\n}\n\n// Retries example that succeeds after a certain number of retries\nfunc RetriesWithCount(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[RetriesWithCountInput, RetriesWithCountResult] {\n\t// > Retries with Count\n\tretriesWithCount := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"fail-twice-task\",\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input RetriesWithCountInput) (*RetriesWithCountResult, error) {\n\t\t\t// Get the current retry count\n\t\t\tretryCount := ctx.RetryCount()\n\n\t\t\tfmt.Printf(\"Retry count: %d\\n\", retryCount)\n\n\t\t\tif retryCount < 2 {\n\t\t\t\treturn nil, errors.New(\"intentional failure\")\n\t\t\t}\n\n\t\t\treturn &RetriesWithCountResult{\n\t\t\t\tMessage: \"success\",\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn retriesWithCount\n}\n\ntype BackoffInput struct{}\ntype BackoffResult struct{}\n\n// Retries example with simple backoff (no configuration in this API version)\nfunc WithBackoff(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[BackoffInput, BackoffResult] {\n\t// > Retries with Backoff\n\twithBackoff := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"with-backoff-task\",\n\t\t\t// 👀 Maximum number of seconds to wait between retries\n\t\t\tRetries: 3,\n\t\t\t// 👀 Factor to increase the wait time between retries.\n\t\t\tRetryBackoffFactor: 2,\n\t\t\t// 👀 Maximum number of seconds to wait between retries\n\t\t\t// This sequence will be 2s, 4s, 8s, 10s, 10s, 10s... due to the maxSeconds limit\n\t\t\tRetryMaxBackoffSeconds: 10,\n\t\t}, func(ctx worker.HatchetContext, input BackoffInput) (*BackoffResult, error) {\n\t\t\treturn nil, errors.New(\"intentional failure\")\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn withBackoff\n}\n",
|
||||
"source": "out/go/workflows/retries.go",
|
||||
"blocks": {
|
||||
"simple_step_retries": {
|
||||
"start": 20,
|
||||
"stop": 28
|
||||
},
|
||||
'retries_with_count': {
|
||||
'start': 41,
|
||||
'stop': 60
|
||||
"retries_with_count": {
|
||||
"start": 41,
|
||||
"stop": 60
|
||||
},
|
||||
'retries_with_backoff': {
|
||||
'start': 71,
|
||||
'stop': 85
|
||||
"retries_with_backoff": {
|
||||
"start": 71,
|
||||
"stop": 85
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'context\'\n\t\'fmt\'\n\t\'strings\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\tv1worker \'github.com/hatchet-dev/hatchet/pkg/v1/worker\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype SimpleInput struct {\n\tMessage string\n}\ntype SimpleResult struct {\n\tTransformedMessage string\n}\n\nfunc Simple(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[SimpleInput, SimpleResult] {\n\n\t// Create a simple standalone task using the task factory\n\t// Note the use of typed generics for both input and output\n\n\t// > Declaring a Task\n\tsimple := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'simple-task\',\n\t\t}, func(ctx worker.HatchetContext, input SimpleInput) (*SimpleResult, error) {\n\t\t\t// Transform the input message to lowercase\n\t\t\treturn &SimpleResult{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\t// Example of running a task\n\t_ = func() error {\n\t\t// > Running a Task\n\t\tresult, err := simple.Run(context.Background(), SimpleInput{Message: \'Hello, World!\'})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfmt.Println(result.TransformedMessage)\n\t\treturn nil\n\t}\n\n\t// Example of registering a task on a worker\n\t_ = func() error {\n\t\t// > Declaring a Worker\n\t\tw, err := hatchet.Worker(v1worker.WorkerOpts{\n\t\t\tName: \'simple-worker\',\n\t\t\tWorkflows: []workflow.WorkflowBase{\n\t\t\t\tsimple,\n\t\t\t},\n\t\t})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\terr = w.StartBlocking(context.Background())\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}\n\n\treturn simple\n}\n\nfunc ParentTask(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[SimpleInput, SimpleResult] {\n\n\t// > Spawning Tasks from within a Task\n\tsimple := Simple(hatchet)\n\n\tparent := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'parent-task\',\n\t\t}, func(ctx worker.HatchetContext, input SimpleInput) (*SimpleResult, error) {\n\n\t\t\t// Run the child task\n\t\t\tchild, err := workflow.RunChildWorkflow(ctx, simple, SimpleInput{Message: input.Message})\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\t// Transform the input message to lowercase\n\t\t\treturn &SimpleResult{\n\t\t\t\tTransformedMessage: child.TransformedMessage,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn parent\n}\n',
|
||||
'source': 'out/go/workflows/simple.go',
|
||||
'blocks': {
|
||||
'declaring_a_task': {
|
||||
'start': 29,
|
||||
'stop': 39
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\tv1worker \"github.com/hatchet-dev/hatchet/pkg/v1/worker\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype SimpleInput struct {\n\tMessage string\n}\ntype SimpleResult struct {\n\tTransformedMessage string\n}\n\nfunc Simple(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[SimpleInput, SimpleResult] {\n\n\t// Create a simple standalone task using the task factory\n\t// Note the use of typed generics for both input and output\n\n\t// > Declaring a Task\n\tsimple := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"simple-task\",\n\t\t}, func(ctx worker.HatchetContext, input SimpleInput) (*SimpleResult, error) {\n\t\t\t// Transform the input message to lowercase\n\t\t\treturn &SimpleResult{\n\t\t\t\tTransformedMessage: strings.ToLower(input.Message),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\t// Example of running a task\n\t_ = func() error {\n\t\t// > Running a Task\n\t\tresult, err := simple.Run(context.Background(), SimpleInput{Message: \"Hello, World!\"})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfmt.Println(result.TransformedMessage)\n\t\treturn nil\n\t}\n\n\t// Example of registering a task on a worker\n\t_ = func() error {\n\t\t// > Declaring a Worker\n\t\tw, err := hatchet.Worker(v1worker.WorkerOpts{\n\t\t\tName: \"simple-worker\",\n\t\t\tWorkflows: []workflow.WorkflowBase{\n\t\t\t\tsimple,\n\t\t\t},\n\t\t})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\terr = w.StartBlocking(context.Background())\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}\n\n\treturn simple\n}\n\nfunc ParentTask(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[SimpleInput, SimpleResult] {\n\n\t// > Spawning Tasks from within a Task\n\tsimple := Simple(hatchet)\n\n\tparent := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"parent-task\",\n\t\t}, func(ctx worker.HatchetContext, input SimpleInput) (*SimpleResult, error) {\n\n\t\t\t// Run the child task\n\t\t\tchild, err := workflow.RunChildWorkflow(ctx, simple, SimpleInput{Message: input.Message})\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\t// Transform the input message to lowercase\n\t\t\treturn &SimpleResult{\n\t\t\t\tTransformedMessage: child.TransformedMessage,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn parent\n}\n",
|
||||
"source": "out/go/workflows/simple.go",
|
||||
"blocks": {
|
||||
"declaring_a_task": {
|
||||
"start": 29,
|
||||
"stop": 39
|
||||
},
|
||||
'running_a_task': {
|
||||
'start': 44,
|
||||
'stop': 48
|
||||
"running_a_task": {
|
||||
"start": 44,
|
||||
"stop": 48
|
||||
},
|
||||
'declaring_a_worker': {
|
||||
'start': 55,
|
||||
'stop': 67
|
||||
"declaring_a_worker": {
|
||||
"start": 55,
|
||||
"stop": 67
|
||||
},
|
||||
'spawning_tasks_from_within_a_task': {
|
||||
'start': 77,
|
||||
'stop': 96
|
||||
"spawning_tasks_from_within_a_task": {
|
||||
"start": 77,
|
||||
"stop": 96
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'fmt\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype StickyInput struct{}\n\ntype StickyResult struct {\n\tResult string `json:\'result\'`\n}\n\ntype StickyDagResult struct {\n\tStickyTask1 StickyResult `json:\'sticky-task-1\'`\n\tStickyTask2 StickyResult `json:\'sticky-task-2\'`\n}\n\nfunc StickyDag(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[StickyInput, StickyDagResult] {\n\tstickyDag := factory.NewWorkflow[StickyInput, StickyDagResult](\n\t\tcreate.WorkflowCreateOpts[StickyInput]{\n\t\t\tName: \'sticky-dag\',\n\t\t},\n\t\thatchet,\n\t)\n\n\tstickyDag.Task(\n\t\tcreate.WorkflowTask[StickyInput, StickyDagResult]{\n\t\t\tName: \'sticky-task\',\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input StickyInput) (interface{}, error) {\n\t\t\tworkerId := ctx.Worker().ID()\n\n\t\t\treturn &StickyResult{\n\t\t\t\tResult: workerId,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\tstickyDag.Task(\n\t\tcreate.WorkflowTask[StickyInput, StickyDagResult]{\n\t\t\tName: \'sticky-task-2\',\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input StickyInput) (interface{}, error) {\n\t\t\tworkerId := ctx.Worker().ID()\n\n\t\t\treturn &StickyResult{\n\t\t\t\tResult: workerId,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn stickyDag\n}\n\nfunc Sticky(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[StickyInput, StickyResult] {\n\tsticky := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'sticky-task\',\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input StickyInput) (*StickyResult, error) {\n\t\t\t// Run a child workflow on the same worker\n\t\t\tchildWorkflow := Child(hatchet)\n\t\t\tsticky := true\n\t\t\tchildResult, err := childWorkflow.RunAsChild(ctx, ChildInput{N: 1}, workflow.RunAsChildOpts{\n\t\t\t\tSticky: &sticky,\n\t\t\t})\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &StickyResult{\n\t\t\t\tResult: fmt.Sprintf(\'child-result-%d\', childResult.Value),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn sticky\n}\n',
|
||||
'source': 'out/go/workflows/sticky.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype StickyInput struct{}\n\ntype StickyResult struct {\n\tResult string `json:\"result\"`\n}\n\ntype StickyDagResult struct {\n\tStickyTask1 StickyResult `json:\"sticky-task-1\"`\n\tStickyTask2 StickyResult `json:\"sticky-task-2\"`\n}\n\nfunc StickyDag(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[StickyInput, StickyDagResult] {\n\tstickyDag := factory.NewWorkflow[StickyInput, StickyDagResult](\n\t\tcreate.WorkflowCreateOpts[StickyInput]{\n\t\t\tName: \"sticky-dag\",\n\t\t},\n\t\thatchet,\n\t)\n\n\tstickyDag.Task(\n\t\tcreate.WorkflowTask[StickyInput, StickyDagResult]{\n\t\t\tName: \"sticky-task\",\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input StickyInput) (interface{}, error) {\n\t\t\tworkerId := ctx.Worker().ID()\n\n\t\t\treturn &StickyResult{\n\t\t\t\tResult: workerId,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\tstickyDag.Task(\n\t\tcreate.WorkflowTask[StickyInput, StickyDagResult]{\n\t\t\tName: \"sticky-task-2\",\n\t\t},\n\t\tfunc(ctx worker.HatchetContext, input StickyInput) (interface{}, error) {\n\t\t\tworkerId := ctx.Worker().ID()\n\n\t\t\treturn &StickyResult{\n\t\t\t\tResult: workerId,\n\t\t\t}, nil\n\t\t},\n\t)\n\n\treturn stickyDag\n}\n\nfunc Sticky(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[StickyInput, StickyResult] {\n\tsticky := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"sticky-task\",\n\t\t\tRetries: 3,\n\t\t}, func(ctx worker.HatchetContext, input StickyInput) (*StickyResult, error) {\n\t\t\t// Run a child workflow on the same worker\n\t\t\tchildWorkflow := Child(hatchet)\n\t\t\tsticky := true\n\t\t\tchildResult, err := childWorkflow.RunAsChild(ctx, ChildInput{N: 1}, workflow.RunAsChildOpts{\n\t\t\t\tSticky: &sticky,\n\t\t\t})\n\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\treturn &StickyResult{\n\t\t\t\tResult: fmt.Sprintf(\"child-result-%d\", childResult.Value),\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn sticky\n}\n",
|
||||
"source": "out/go/workflows/sticky.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package v1_workflows\n\nimport (\n\t\'errors\'\n\t\'time\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client/create\'\n\tv1 \'github.com/hatchet-dev/hatchet/pkg/v1\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/factory\'\n\t\'github.com/hatchet-dev/hatchet/pkg/v1/workflow\'\n\t\'github.com/hatchet-dev/hatchet/pkg/worker\'\n)\n\ntype TimeoutInput struct{}\ntype TimeoutResult struct {\n\tCompleted bool\n}\n\nfunc Timeout(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[TimeoutInput, TimeoutResult] {\n\n\t// > Execution Timeout\n\t// Create a task with a timeout of 3 seconds that tries to sleep for 10 seconds\n\ttimeout := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'timeout-task\',\n\t\t\tExecutionTimeout: 3 * time.Second, // Task will timeout after 3 seconds\n\t\t}, func(ctx worker.HatchetContext, input TimeoutInput) (*TimeoutResult, error) {\n\t\t\t// Sleep for 10 seconds\n\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t// Check if the context was cancelled due to timeout\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn nil, errors.New(\'TASK TIMED OUT\')\n\t\t\tdefault:\n\t\t\t\t// Continue execution\n\t\t\t}\n\n\t\t\treturn &TimeoutResult{\n\t\t\t\tCompleted: true,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn timeout\n}\n\nfunc RefreshTimeout(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[TimeoutInput, TimeoutResult] {\n\n\t// > Refresh Timeout\n\ttimeout := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \'timeout-task\',\n\t\t\tExecutionTimeout: 3 * time.Second, // Task will timeout after 3 seconds\n\t\t}, func(ctx worker.HatchetContext, input TimeoutInput) (*TimeoutResult, error) {\n\n\t\t\t// Refresh the timeout by 10 seconds (new timeout will be 13 seconds)\n\t\t\tctx.RefreshTimeout(\'10s\')\n\n\t\t\t// Sleep for 10 seconds\n\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t// Check if the context was cancelled due to timeout\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn nil, errors.New(\'TASK TIMED OUT\')\n\t\t\tdefault:\n\t\t\t\t// Continue execution\n\t\t\t}\n\n\t\t\treturn &TimeoutResult{\n\t\t\t\tCompleted: true,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn timeout\n}\n',
|
||||
'source': 'out/go/workflows/timeouts.go',
|
||||
'blocks': {
|
||||
'execution_timeout': {
|
||||
'start': 22,
|
||||
'stop': 44
|
||||
"language": "go",
|
||||
"content": "package v1_workflows\n\nimport (\n\t\"errors\"\n\t\"time\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client/create\"\n\tv1 \"github.com/hatchet-dev/hatchet/pkg/v1\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/factory\"\n\t\"github.com/hatchet-dev/hatchet/pkg/v1/workflow\"\n\t\"github.com/hatchet-dev/hatchet/pkg/worker\"\n)\n\ntype TimeoutInput struct{}\ntype TimeoutResult struct {\n\tCompleted bool\n}\n\nfunc Timeout(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[TimeoutInput, TimeoutResult] {\n\n\t// > Execution Timeout\n\t// Create a task with a timeout of 3 seconds that tries to sleep for 10 seconds\n\ttimeout := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"timeout-task\",\n\t\t\tExecutionTimeout: 3 * time.Second, // Task will timeout after 3 seconds\n\t\t}, func(ctx worker.HatchetContext, input TimeoutInput) (*TimeoutResult, error) {\n\t\t\t// Sleep for 10 seconds\n\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t// Check if the context was cancelled due to timeout\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn nil, errors.New(\"TASK TIMED OUT\")\n\t\t\tdefault:\n\t\t\t\t// Continue execution\n\t\t\t}\n\n\t\t\treturn &TimeoutResult{\n\t\t\t\tCompleted: true,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn timeout\n}\n\nfunc RefreshTimeout(hatchet v1.HatchetClient) workflow.WorkflowDeclaration[TimeoutInput, TimeoutResult] {\n\n\t// > Refresh Timeout\n\ttimeout := factory.NewTask(\n\t\tcreate.StandaloneTask{\n\t\t\tName: \"timeout-task\",\n\t\t\tExecutionTimeout: 3 * time.Second, // Task will timeout after 3 seconds\n\t\t}, func(ctx worker.HatchetContext, input TimeoutInput) (*TimeoutResult, error) {\n\n\t\t\t// Refresh the timeout by 10 seconds (new timeout will be 13 seconds)\n\t\t\tctx.RefreshTimeout(\"10s\")\n\n\t\t\t// Sleep for 10 seconds\n\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t// Check if the context was cancelled due to timeout\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn nil, errors.New(\"TASK TIMED OUT\")\n\t\t\tdefault:\n\t\t\t\t// Continue execution\n\t\t\t}\n\n\t\t\treturn &TimeoutResult{\n\t\t\t\tCompleted: true,\n\t\t\t}, nil\n\t\t},\n\t\thatchet,\n\t)\n\n\treturn timeout\n}\n",
|
||||
"source": "out/go/workflows/timeouts.go",
|
||||
"blocks": {
|
||||
"execution_timeout": {
|
||||
"start": 22,
|
||||
"stop": 44
|
||||
},
|
||||
'refresh_timeout': {
|
||||
'start': 52,
|
||||
'stop': 77
|
||||
"refresh_timeout": {
|
||||
"start": 52,
|
||||
"stop": 77
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\n\t\'github.com/joho/godotenv\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/cmdutils\'\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run()\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(\'cleanup() error = %v\', err))\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/assignment-affinity/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/joho/godotenv\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/cmdutils\"\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run()\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(\"cleanup() error = %v\", err))\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/assignment-affinity/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\n\t\'github.com/joho/godotenv\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/cmdutils\'\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run()\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(\'cleanup() error = %v\', err))\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/assignment-sticky/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/joho/godotenv\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/cmdutils\"\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run()\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(\"cleanup() error = %v\", err))\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/assignment-sticky/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
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)\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': {
|
||||
'start': 30,
|
||||
'stop': 68
|
||||
"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)\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": {
|
||||
"start": 30,
|
||||
"stop": 68
|
||||
},
|
||||
'stickychild': {
|
||||
'start': 75,
|
||||
'stop': 90
|
||||
"stickychild": {
|
||||
"start": 75,
|
||||
"stop": 90
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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/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\t_, err = run()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n}\n\nfunc run() (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\ttestSvc := w.NewService(\'test\')\n\n\terr = testSvc.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events(\'user:create:bulk\'),\n\t\t\tName: \'bulk\',\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\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),\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\tvar events []client.EventWithAdditionalMetadata\n\n\t// 20000 times to test the bulk push\n\n\tfor i := 0; i < 20000; i++ {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: \'echo-test\',\n\t\t\tUserID: \'1234 \' + fmt.Sprint(i),\n\t\t\tData: map[string]string{\n\t\t\t\t\'test\': \'test \' + fmt.Sprint(i),\n\t\t\t},\n\t\t}\n\t\tevents = append(events, client.EventWithAdditionalMetadata{\n\t\t\tEvent: testEvent,\n\t\t\tAdditionalMetadata: map[string]string{\'hello\': \'world \' + fmt.Sprint(i)},\n\t\t\tKey: \'user:create:bulk\',\n\t\t})\n\t}\n\n\tlog.Printf(\'pushing event user:create:bulk\')\n\n\terr = c.Event().BulkPush(\n\t\tcontext.Background(),\n\t\tevents,\n\t)\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error pushing event: %w\', err))\n\t}\n\n\treturn nil, nil\n\n}\n',
|
||||
'source': 'out/go/z_v0/bulk_imports/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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/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\t_, err = run()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n}\n\nfunc run() (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\ttestSvc := w.NewService(\"test\")\n\n\terr = testSvc.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.Events(\"user:create:bulk\"),\n\t\t\tName: \"bulk\",\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\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),\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\tvar events []client.EventWithAdditionalMetadata\n\n\t// 20000 times to test the bulk push\n\n\tfor i := 0; i < 20000; i++ {\n\t\ttestEvent := userCreateEvent{\n\t\t\tUsername: \"echo-test\",\n\t\t\tUserID: \"1234 \" + fmt.Sprint(i),\n\t\t\tData: map[string]string{\n\t\t\t\t\"test\": \"test \" + fmt.Sprint(i),\n\t\t\t},\n\t\t}\n\t\tevents = append(events, client.EventWithAdditionalMetadata{\n\t\t\tEvent: testEvent,\n\t\t\tAdditionalMetadata: map[string]string{\"hello\": \"world \" + fmt.Sprint(i)},\n\t\t\tKey: \"user:create:bulk\",\n\t\t})\n\t}\n\n\tlog.Printf(\"pushing event user:create:bulk\")\n\n\terr = c.Event().BulkPush(\n\t\tcontext.Background(),\n\t\tevents,\n\t)\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error pushing event: %w\", err))\n\t}\n\n\treturn nil, nil\n\n}\n",
|
||||
"source": "out/go/z_v0/bulk_imports/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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/client/types\'\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\tch := cmdutils.InterruptChan()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tworkflowName := \'simple-bulk-workflow\'\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error creating client: %w\', err))\n\t}\n\n\t_, err = registerWorkflow(c, workflowName)\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error registering workflow: %w\', err))\n\t}\n\n\tquantity := 999\n\n\toverallStart := time.Now()\n\titerations := 10\n\tfor i := 0; i < iterations; i++ {\n\t\tstartTime := time.Now()\n\n\t\tfmt.Printf(\'Running the %dth bulk workflow \\n\', i)\n\n\t\terr = runBulk(workflowName, quantity)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tfmt.Printf(\'Time taken to queue %dth bulk workflow: %v\\n\', i, time.Since(startTime))\n\t}\n\tfmt.Println(\'Overall time taken: \', time.Since(overallStart))\n\tfmt.Printf(\'That is %d workflows per second\\n\', int(float64(quantity*iterations)/time.Since(overallStart).Seconds()))\n\tfmt.Println(\'Starting the worker\')\n\n\t// err = runSingles(workflowName, quantity)\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error creating client: %w\', err))\n\t}\n\n\t// I want to start the wofklow worker here\n\n\tw, err := registerWorkflow(c, workflowName)\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error creating worker: %w\', err))\n\t}\n\n\tcleanup, err := w.Start()\n\tfmt.Println(\'Starting the worker\')\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error starting worker: %w\', 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}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn \'my-key\', nil\n}\n\nfunc registerWorkflow(c client.Client, workflowName string) (w *worker.Worker, err error) {\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:bulk-simple\'),\n\t\t\tName: workflowName,\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey).MaxRuns(200).LimitStrategy(types.GroupRoundRobin),\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\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\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\treturn w, nil\n}\n',
|
||||
'source': 'out/go/z_v0/bulk_workflows/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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/client/types\"\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\tch := cmdutils.InterruptChan()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tworkflowName := \"simple-bulk-workflow\"\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error creating client: %w\", err))\n\t}\n\n\t_, err = registerWorkflow(c, workflowName)\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error registering workflow: %w\", err))\n\t}\n\n\tquantity := 999\n\n\toverallStart := time.Now()\n\titerations := 10\n\tfor i := 0; i < iterations; i++ {\n\t\tstartTime := time.Now()\n\n\t\tfmt.Printf(\"Running the %dth bulk workflow \\n\", i)\n\n\t\terr = runBulk(workflowName, quantity)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tfmt.Printf(\"Time taken to queue %dth bulk workflow: %v\\n\", i, time.Since(startTime))\n\t}\n\tfmt.Println(\"Overall time taken: \", time.Since(overallStart))\n\tfmt.Printf(\"That is %d workflows per second\\n\", int(float64(quantity*iterations)/time.Since(overallStart).Seconds()))\n\tfmt.Println(\"Starting the worker\")\n\n\t// err = runSingles(workflowName, quantity)\n\t// if err != nil {\n\t// \tpanic(err)\n\t// }\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error creating client: %w\", err))\n\t}\n\n\t// I want to start the wofklow worker here\n\n\tw, err := registerWorkflow(c, workflowName)\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error creating worker: %w\", err))\n\t}\n\n\tcleanup, err := w.Start()\n\tfmt.Println(\"Starting the worker\")\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error starting worker: %w\", 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}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn \"my-key\", nil\n}\n\nfunc registerWorkflow(c client.Client, workflowName string) (w *worker.Worker, err error) {\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:bulk-simple\"),\n\t\t\tName: workflowName,\n\t\t\tConcurrency: worker.Concurrency(getConcurrencyKey).MaxRuns(200).LimitStrategy(types.GroupRoundRobin),\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\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\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\treturn w, nil\n}\n",
|
||||
"source": "out/go/z_v0/bulk_workflows/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\t\'log\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/client\'\n)\n\nfunc runBulk(workflowName string, quantity int) error {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn fmt.Errorf(\'error creating client: %w\', err)\n\t}\n\n\tlog.Printf(\'pushing %d workflows in bulk\', quantity)\n\n\tvar workflows []*client.WorkflowRun\n\tfor i := 0; i < quantity; i++ {\n\t\tdata := map[string]interface{}{\n\t\t\t\'username\': fmt.Sprintf(\'echo-test-%d\', i),\n\t\t\t\'user_id\': fmt.Sprintf(\'1234-%d\', i),\n\t\t}\n\t\tworkflows = append(workflows, &client.WorkflowRun{\n\t\t\tName: workflowName,\n\t\t\tInput: data,\n\t\t\tOptions: []client.RunOptFunc{\n\t\t\t\t// setting a dedupe key so these shouldn\'t all run\n\t\t\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\t\t\t// \'dedupe\': \'dedupe1\',\n\t\t\t\t}),\n\t\t\t},\n\t\t})\n\n\t}\n\n\touts, err := c.Admin().BulkRunWorkflow(workflows)\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error pushing event: %w\', err))\n\t}\n\n\tfor _, out := range outs {\n\t\tlog.Printf(\'workflow run id: %v\', out)\n\t}\n\n\treturn nil\n\n}\n\nfunc runSingles(workflowName string, quantity int) error {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn fmt.Errorf(\'error creating client: %w\', err)\n\t}\n\n\tlog.Printf(\'pushing %d single workflows\', quantity)\n\n\tvar workflows []*client.WorkflowRun\n\tfor i := 0; i < quantity; i++ {\n\t\tdata := map[string]interface{}{\n\t\t\t\'username\': fmt.Sprintf(\'echo-test-%d\', i),\n\t\t\t\'user_id\': fmt.Sprintf(\'1234-%d\', i),\n\t\t}\n\t\tworkflows = append(workflows, &client.WorkflowRun{\n\t\t\tName: workflowName,\n\t\t\tInput: data,\n\t\t\tOptions: []client.RunOptFunc{\n\t\t\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\t\t\t// \'dedupe\': \'dedupe1\',\n\t\t\t\t}),\n\t\t\t},\n\t\t})\n\t}\n\n\tfor _, wf := range workflows {\n\n\t\tgo func() {\n\t\t\tout, err := c.Admin().RunWorkflow(wf.Name, wf.Input, wf.Options...)\n\t\t\tif err != nil {\n\t\t\t\tpanic(fmt.Errorf(\'error pushing event: %w\', err))\n\t\t\t}\n\n\t\t\tlog.Printf(\'workflow run id: %v\', out)\n\t\t}()\n\n\t}\n\n\treturn nil\n}\n',
|
||||
'source': 'out/go/z_v0/bulk_workflows/run.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/client\"\n)\n\nfunc runBulk(workflowName string, quantity int) error {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error creating client: %w\", err)\n\t}\n\n\tlog.Printf(\"pushing %d workflows in bulk\", quantity)\n\n\tvar workflows []*client.WorkflowRun\n\tfor i := 0; i < quantity; i++ {\n\t\tdata := map[string]interface{}{\n\t\t\t\"username\": fmt.Sprintf(\"echo-test-%d\", i),\n\t\t\t\"user_id\": fmt.Sprintf(\"1234-%d\", i),\n\t\t}\n\t\tworkflows = append(workflows, &client.WorkflowRun{\n\t\t\tName: workflowName,\n\t\t\tInput: data,\n\t\t\tOptions: []client.RunOptFunc{\n\t\t\t\t// setting a dedupe key so these shouldn't all run\n\t\t\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\t\t\t// \"dedupe\": \"dedupe1\",\n\t\t\t\t}),\n\t\t\t},\n\t\t})\n\n\t}\n\n\touts, err := c.Admin().BulkRunWorkflow(workflows)\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error pushing event: %w\", err))\n\t}\n\n\tfor _, out := range outs {\n\t\tlog.Printf(\"workflow run id: %v\", out)\n\t}\n\n\treturn nil\n\n}\n\nfunc runSingles(workflowName string, quantity int) error {\n\tc, err := client.New()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error creating client: %w\", err)\n\t}\n\n\tlog.Printf(\"pushing %d single workflows\", quantity)\n\n\tvar workflows []*client.WorkflowRun\n\tfor i := 0; i < quantity; i++ {\n\t\tdata := map[string]interface{}{\n\t\t\t\"username\": fmt.Sprintf(\"echo-test-%d\", i),\n\t\t\t\"user_id\": fmt.Sprintf(\"1234-%d\", i),\n\t\t}\n\t\tworkflows = append(workflows, &client.WorkflowRun{\n\t\t\tName: workflowName,\n\t\t\tInput: data,\n\t\t\tOptions: []client.RunOptFunc{\n\t\t\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\t\t\t// \"dedupe\": \"dedupe1\",\n\t\t\t\t}),\n\t\t\t},\n\t\t})\n\t}\n\n\tfor _, wf := range workflows {\n\n\t\tgo func() {\n\t\t\tout, err := c.Admin().RunWorkflow(wf.Name, wf.Input, wf.Options...)\n\t\t\tif err != nil {\n\t\t\t\tpanic(fmt.Errorf(\"error pushing event: %w\", err))\n\t\t\t}\n\n\t\t\tlog.Printf(\"workflow run id: %v\", out)\n\t\t}()\n\n\t}\n\n\treturn nil\n}\n",
|
||||
"source": "out/go/z_v0/bulk_workflows/run.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\n\t\'github.com/joho/godotenv\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/cmdutils\'\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run(events)\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(\'cleanup() error = %v\', err))\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/cancellation/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/joho/godotenv\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/cmdutils\"\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run(events)\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(\"cleanup() error = %v\", err))\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/cancellation/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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/client/types\'\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\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:concurrency\'),\n\t\t\tName: \'simple-concurrency\',\n\t\t\tDescription: \'This runs to test concurrency.\',\n\t\t\tConcurrency: worker.Expression(\'\'concurrency\'\').MaxRuns(1).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 := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\t// we sleep to simulate a long running task\n\t\t\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t\t\tif err != nil {\n\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tif ctx.Err() != nil {\n\t\t\t\t\t\treturn nil, ctx.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\tif ctx.Err() != nil {\n\t\t\t\t\t\treturn nil, ctx.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\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\tgo func() {\n\t\t// do this 10 times to test concurrency\n\t\tfor i := 0; i < 10; i++ {\n\n\t\t\twfr_id, err := c.Admin().RunWorkflow(\'simple-concurrency\', testEvent)\n\n\t\t\tlog.Println(\'Starting workflow run id: \', wfr_id)\n\n\t\t\tif err != nil {\n\t\t\t\tpanic(fmt.Errorf(\'error running workflow: %w\', err))\n\t\t\t}\n\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/concurrency/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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/client/types\"\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\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:concurrency\"),\n\t\t\tName: \"simple-concurrency\",\n\t\t\tDescription: \"This runs to test concurrency.\",\n\t\t\tConcurrency: worker.Expression(\"'concurrency'\").MaxRuns(1).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 := &userCreateEvent{}\n\n\t\t\t\t\terr = ctx.WorkflowInput(input)\n\n\t\t\t\t\t// we sleep to simulate a long running task\n\t\t\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t\t\tif err != nil {\n\n\t\t\t\t\t\treturn nil, err\n\t\t\t\t\t}\n\n\t\t\t\t\tif ctx.Err() != nil {\n\t\t\t\t\t\treturn nil, ctx.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\tif ctx.Err() != nil {\n\t\t\t\t\t\treturn nil, ctx.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\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\tgo func() {\n\t\t// do this 10 times to test concurrency\n\t\tfor i := 0; i < 10; i++ {\n\n\t\t\twfr_id, err := c.Admin().RunWorkflow(\"simple-concurrency\", testEvent)\n\n\t\t\tlog.Println(\"Starting workflow run id: \", wfr_id)\n\n\t\t\tif err != nil {\n\t\t\t\tpanic(fmt.Errorf(\"error running workflow: %w\", err))\n\t\t\t}\n\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/concurrency/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'context\'\n\t\'fmt\'\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\n// > Create\n// ... normal workflow definition\ntype printOutput struct{}\n\nfunc print(ctx context.Context) (result *printOutput, err error) {\n\tfmt.Println(\'called print:print\')\n\n\treturn &printOutput{}, nil\n}\n\n// ,\nfunc main() {\n\t// ... initialize client, worker and workflow\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tName: \'cron-workflow\',\n\t\t\tDescription: \'Demonstrates a simple cron workflow\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(print),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ,\n\n\tgo func() {\n\t\t// 👀 define the cron expression to run every minute\n\t\tcron, err := c.Cron().Create(\n\t\t\tcontext.Background(),\n\t\t\t\'cron-workflow\',\n\t\t\t&client.CronOpts{\n\t\t\t\tName: \'every-minute\',\n\t\t\t\tExpression: \'* * * * *\',\n\t\t\t\tInput: map[string]interface{}{\n\t\t\t\t\t\'message\': \'Hello, world!\',\n\t\t\t\t},\n\t\t\t\tAdditionalMetadata: map[string]string{},\n\t\t\t},\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tfmt.Println(*cron.Name, cron.Cron)\n\t}()\n\n\t// ... wait for interrupt signal\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\t// ,\n}\n\n\nfunc ListCrons() {\n\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > List\n\tcrons, err := c.Cron().List(context.Background())\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, cron := range *crons.Rows {\n\t\tfmt.Println(cron.Cron, *cron.Name)\n\t}\n}\n\nfunc DeleteCron(id string) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > Delete\n\t// 👀 id is the cron\'s metadata id, can get it via cron.Metadata.Id\n\terr = c.Cron().Delete(context.Background(), id)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n}\n',
|
||||
'source': 'out/go/z_v0/cron-programmatic/main.go',
|
||||
'blocks': {
|
||||
'create': {
|
||||
'start': 15,
|
||||
'stop': 106
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\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\n// > Create\n// ... normal workflow definition\ntype printOutput struct{}\n\nfunc print(ctx context.Context) (result *printOutput, err error) {\n\tfmt.Println(\"called print:print\")\n\n\treturn &printOutput{}, nil\n}\n\n// ,\nfunc main() {\n\t// ... initialize client, worker and workflow\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tName: \"cron-workflow\",\n\t\t\tDescription: \"Demonstrates a simple cron workflow\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(print),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ,\n\n\tgo func() {\n\t\t// 👀 define the cron expression to run every minute\n\t\tcron, err := c.Cron().Create(\n\t\t\tcontext.Background(),\n\t\t\t\"cron-workflow\",\n\t\t\t&client.CronOpts{\n\t\t\t\tName: \"every-minute\",\n\t\t\t\tExpression: \"* * * * *\",\n\t\t\t\tInput: map[string]interface{}{\n\t\t\t\t\t\"message\": \"Hello, world!\",\n\t\t\t\t},\n\t\t\t\tAdditionalMetadata: map[string]string{},\n\t\t\t},\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tfmt.Println(*cron.Name, cron.Cron)\n\t}()\n\n\t// ... wait for interrupt signal\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\t// ,\n}\n\n\nfunc ListCrons() {\n\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > List\n\tcrons, err := c.Cron().List(context.Background())\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, cron := range *crons.Rows {\n\t\tfmt.Println(cron.Cron, *cron.Name)\n\t}\n}\n\nfunc DeleteCron(id string) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > Delete\n\t// 👀 id is the cron's metadata id, can get it via cron.Metadata.Id\n\terr = c.Cron().Delete(context.Background(), id)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n}\n",
|
||||
"source": "out/go/z_v0/cron-programmatic/main.go",
|
||||
"blocks": {
|
||||
"create": {
|
||||
"start": 15,
|
||||
"stop": 106
|
||||
},
|
||||
'list': {
|
||||
'start': 117,
|
||||
'stop': 117
|
||||
"list": {
|
||||
"start": 117,
|
||||
"stop": 117
|
||||
},
|
||||
'delete': {
|
||||
'start': 136,
|
||||
'stop': 137
|
||||
"delete": {
|
||||
"start": 136,
|
||||
"stop": 137
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'context\'\n\t\'fmt\'\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\n// > Workflow Definition Cron Trigger\n// ... normal workflow definition\ntype printOutput struct{}\n\nfunc print(ctx context.Context) (result *printOutput, err error) {\n\tfmt.Println(\'called print:print\')\n\n\treturn &printOutput{}, nil\n}\n\n// ,\nfunc main() {\n\t// ... initialize client and worker\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\t// ,\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\t// 👀 define the cron expression to run every minute\n\t\t\tOn: worker.Cron(\'* * * * *\'),\n\t\t\tName: \'cron-workflow\',\n\t\t\tDescription: \'Demonstrates a simple cron workflow\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(print),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ... start worker\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\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\t// ,\n}\n\n',
|
||||
'source': 'out/go/z_v0/cron/main.go',
|
||||
'blocks': {
|
||||
'workflow_definition_cron_trigger': {
|
||||
'start': 15,
|
||||
'stop': 84
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\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\n// > Workflow Definition Cron Trigger\n// ... normal workflow definition\ntype printOutput struct{}\n\nfunc print(ctx context.Context) (result *printOutput, err error) {\n\tfmt.Println(\"called print:print\")\n\n\treturn &printOutput{}, nil\n}\n\n// ,\nfunc main() {\n\t// ... initialize client and worker\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\t// ,\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\t// 👀 define the cron expression to run every minute\n\t\t\tOn: worker.Cron(\"* * * * *\"),\n\t\t\tName: \"cron-workflow\",\n\t\t\tDescription: \"Demonstrates a simple cron workflow\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(print),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ... start worker\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\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\t// ,\n}\n\n",
|
||||
"source": "out/go/z_v0/cron/main.go",
|
||||
"blocks": {
|
||||
"workflow_definition_cron_trigger": {
|
||||
"start": 15,
|
||||
"stop": 84
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'unknown',
|
||||
'content': 'name: \'test-step-requeue\'\nversion: v0.2.0\ntriggers:\n events:\n - example:event\njobs:\n requeue-job:\n steps:\n - id: requeue\n action: requeue:requeue\n timeout: 10s\n',
|
||||
'source': 'out/go/z_v0/deprecated/requeue/.hatchet/job-requeue-workflow.yaml',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "unknown",
|
||||
"content": "name: \"test-step-requeue\"\nversion: v0.2.0\ntriggers:\n events:\n - example:event\njobs:\n requeue-job:\n steps:\n - id: requeue\n action: requeue:requeue\n timeout: 10s\n",
|
||||
"source": "out/go/z_v0/deprecated/requeue/.hatchet/job-requeue-workflow.yaml",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'unknown',
|
||||
'content': 'name: \'test-schedule-timeout\'\nversion: v0.1.0\ntriggers:\n events:\n - user:create\njobs:\n timeout-job:\n steps:\n - id: timeout\n action: timeout:timeout\n',
|
||||
'source': 'out/go/z_v0/deprecated/schedule-timeout/.hatchet/schedule-timeout-workflow.yaml',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "unknown",
|
||||
"content": "name: \"test-schedule-timeout\"\nversion: v0.1.0\ntriggers:\n events:\n - user:create\njobs:\n timeout-job:\n steps:\n - id: timeout\n action: timeout:timeout\n",
|
||||
"source": "out/go/z_v0/deprecated/schedule-timeout/.hatchet/schedule-timeout-workflow.yaml",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'unknown',
|
||||
'content': 'name: \'test-job-timeout\'\nversion: v0.1.0\ntriggers:\n events:\n - user:create\njobs:\n timeout-job:\n timeout: 3s\n steps:\n - id: timeout\n action: timeout:timeout\n timeout: 10s\n',
|
||||
'source': 'out/go/z_v0/deprecated/timeout/.hatchet/job-timeout-workflow.yaml',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "unknown",
|
||||
"content": "name: \"test-job-timeout\"\nversion: v0.1.0\ntriggers:\n events:\n - user:create\njobs:\n timeout-job:\n timeout: 3s\n steps:\n - id: timeout\n action: timeout:timeout\n timeout: 10s\n",
|
||||
"source": "out/go/z_v0/deprecated/timeout/.hatchet/job-timeout-workflow.yaml",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'unknown',
|
||||
'content': 'name: \'test-step-timeout\'\nversion: v0.1.0\ntriggers:\n events:\n - user:create\njobs:\n timeout-job:\n steps:\n - id: timeout\n action: timeout:timeout\n timeout: 5s\n # This step should not be reached\n - id: later-step\n action: timeout:timeout\n timeout: 5s\n',
|
||||
'source': 'out/go/z_v0/deprecated/timeout/.hatchet/step-timeout-workflow.yaml',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "unknown",
|
||||
"content": "name: \"test-step-timeout\"\nversion: v0.1.0\ntriggers:\n events:\n - user:create\njobs:\n timeout-job:\n steps:\n - id: timeout\n action: timeout:timeout\n timeout: 5s\n # This step should not be reached\n - id: later-step\n action: timeout:timeout\n timeout: 5s\n",
|
||||
"source": "out/go/z_v0/deprecated/timeout/.hatchet/step-timeout-workflow.yaml",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'unknown',
|
||||
'content': 'name: \'post-user-sign-up\'\nversion: v0.2.0\ntriggers:\n events:\n - user:create\njobs:\n print-user:\n steps:\n - id: echo1\n action: echo:echo\n timeout: 60s\n with:\n message: \'Username is {{ .input.username }}\'\n - id: echo2\n action: echo:echo\n timeout: 60s\n with:\n message: \'Above message is: {{ .steps.echo1.message }}\'\n - id: echo3\n action: echo:echo\n timeout: 60s\n with:\n message: \'Above message is: {{ .steps.echo2.message }}\'\n - id: testObject\n action: echo:object\n timeout: 60s\n with:\n object: \'{{ .steps.echo3.json }}\'\n',
|
||||
'source': 'out/go/z_v0/deprecated/yaml/.hatchet/sample-workflow.yaml',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "unknown",
|
||||
"content": "name: \"post-user-sign-up\"\nversion: v0.2.0\ntriggers:\n events:\n - user:create\njobs:\n print-user:\n steps:\n - id: echo1\n action: echo:echo\n timeout: 60s\n with:\n message: \"Username is {{ .input.username }}\"\n - id: echo2\n action: echo:echo\n timeout: 60s\n with:\n message: \"Above message is: {{ .steps.echo1.message }}\"\n - id: echo3\n action: echo:echo\n timeout: 60s\n with:\n message: \"Above message is: {{ .steps.echo2.message }}\"\n - id: testObject\n action: echo:object\n timeout: 60s\n with:\n object: \"{{ .steps.echo3.json }}\"\n",
|
||||
"source": "out/go/z_v0/deprecated/yaml/.hatchet/sample-workflow.yaml",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'unknown',
|
||||
'content': '## YAML Workflow Example\n\nThis example shows how you can create a YAML file in your repository to define the structure of a workflow. This example runs the [sample-workflow.yaml](./.hatchet/sample-workflow.yaml).\n\n## Explanation\n\nThis folder contains a demo example of a workflow that simply echoes the input message as an output. The workflow file showcases the following features:\n\n- Running a simple job with a set of dependent steps\n- Variable references within step arguments -- each subsequent step in a workflow can call `.steps.<step_id>.<field>` to access output arguments\n\n## How to run\n\nNavigate to this directory and run the following steps:\n\n1. Make sure you have a Hatchet server running (see the instructions [here](../../README.md)). After running `task seed`, grab the tenant ID which is output to the console.\n2. Set your environment variables -- if you\'re using the bundled Temporal server, this will look like:\n\n```sh\ncat > .env <<EOF\nHATCHET_CLIENT_TENANT_ID=<tenant-id-from-seed-command>\nHATCHET_CLIENT_TLS_ROOT_CA_FILE=../../hack/dev/certs/ca.cert\nHATCHET_CLIENT_TLS_CERT_FILE=../../hack/dev/certs/client-worker.pem\nHATCHET_CLIENT_TLS_KEY_FILE=../../hack/dev/certs/client-worker.key\nHATCHET_CLIENT_TLS_SERVER_NAME=cluster\nEOF\n```\n\n3. Run the following within this directory:\n\n```sh\ngo run main.go\';\n```\n',
|
||||
'source': 'out/go/z_v0/deprecated/yaml/README.md',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "unknown",
|
||||
"content": "## YAML Workflow Example\n\nThis example shows how you can create a YAML file in your repository to define the structure of a workflow. This example runs the [sample-workflow.yaml](./.hatchet/sample-workflow.yaml).\n\n## Explanation\n\nThis folder contains a demo example of a workflow that simply echoes the input message as an output. The workflow file showcases the following features:\n\n- Running a simple job with a set of dependent steps\n- Variable references within step arguments -- each subsequent step in a workflow can call `.steps.<step_id>.<field>` to access output arguments\n\n## How to run\n\nNavigate to this directory and run the following steps:\n\n1. Make sure you have a Hatchet server running (see the instructions [here](../../README.md)). After running `task seed`, grab the tenant ID which is output to the console.\n2. Set your environment variables -- if you're using the bundled Temporal server, this will look like:\n\n```sh\ncat > .env <<EOF\nHATCHET_CLIENT_TENANT_ID=<tenant-id-from-seed-command>\nHATCHET_CLIENT_TLS_ROOT_CA_FILE=../../hack/dev/certs/ca.cert\nHATCHET_CLIENT_TLS_CERT_FILE=../../hack/dev/certs/client-worker.pem\nHATCHET_CLIENT_TLS_KEY_FILE=../../hack/dev/certs/client-worker.key\nHATCHET_CLIENT_TLS_SERVER_NAME=cluster\nEOF\n```\n\n3. Run the following within this directory:\n\n```sh\ngo run main.go';\n```\n",
|
||||
"source": "out/go/z_v0/deprecated/yaml/README.md",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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\'sync\'\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\tConcurrencyKey string `json:\'concurrency_key\'`\n\tUserId int `json:\'user_id\'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\'message\'`\n\tConcurrencyWhenFinished int `json:\'concurrency_when_finished\'`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tctx, cancel := cmdutils.NewInterruptContext()\n\tdefer cancel()\n\n\tif err := run(ctx); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn \'concurrency\', nil\n}\n\nvar done = make(chan struct{})\nvar errChan = make(chan error)\n\nvar workflowCount int\nvar countMux sync.Mutex\n\nfunc run(ctx context.Context) 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\t// runningCount := 0\n\n\tcountMux := sync.Mutex{}\n\n\tvar countMap = make(map[string]int)\n\tmaxConcurrent := 2\n\n\terr = w.RegisterWorkflow(\n\n\t\t&worker.WorkflowJob{\n\t\t\tName: \'concurrency-limit-round-robin-existing-workflows\',\n\t\t\tDescription: \'This limits concurrency to maxConcurrent runs at a time.\',\n\t\t\tOn: worker.Events(\'test:concurrency-limit-round-robin-existing-workflows\'),\n\t\t\tConcurrency: worker.Expression(\'input.concurrency_key\').MaxRuns(int32(maxConcurrent)).LimitStrategy(types.GroupRoundRobin),\n\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\t\t\t\t\tconcurrencyKey := input.ConcurrencyKey\n\t\t\t\t\tcountMux.Lock()\n\n\t\t\t\t\tif countMap[concurrencyKey]+1 > maxConcurrent {\n\t\t\t\t\t\tcountMux.Unlock()\n\t\t\t\t\t\te := fmt.Errorf(\'concurrency limit exceeded for %d we have %d workers running\', input.UserId, countMap[concurrencyKey])\n\t\t\t\t\t\terrChan <- e\n\t\t\t\t\t\treturn nil, e\n\t\t\t\t\t}\n\t\t\t\t\tcountMap[concurrencyKey]++\n\n\t\t\t\t\tcountMux.Unlock()\n\n\t\t\t\t\tfmt.Println(\'received event\', input.UserId)\n\n\t\t\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t\t\tfmt.Println(\'processed event\', input.UserId)\n\n\t\t\t\t\tcountMux.Lock()\n\t\t\t\t\tcountMap[concurrencyKey]--\n\t\t\t\t\tcountMux.Unlock()\n\n\t\t\t\t\tdone <- struct{}{}\n\n\t\t\t\t\treturn &stepOneOutput{}, 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\tgo func() {\n\t\tvar workflowRuns []*client.WorkflowRun\n\n\t\tfor i := 0; i < 1; i++ {\n\t\t\tworkflowCount++\n\t\t\tevent := concurrencyLimitEvent{\n\t\t\t\tConcurrencyKey: \'key\',\n\t\t\t\tUserId: i,\n\t\t\t}\n\t\t\tworkflowRuns = append(workflowRuns, &client.WorkflowRun{\n\t\t\t\tName: \'concurrency-limit-round-robin-existing-workflows\',\n\t\t\t\tInput: event,\n\t\t\t})\n\n\t\t}\n\n\t\t// create a second one with a different key\n\n\t\t// so the bug we are testing here is that total concurrency for any one group should be 2\n\t\t// but if we have more than one group we end up with 4 running when only 2 + 1 are eligible to run\n\n\t\tfor i := 0; i < 3; i++ {\n\t\t\tworkflowCount++\n\n\t\t\tevent := concurrencyLimitEvent{\n\t\t\t\tConcurrencyKey: \'secondKey\',\n\t\t\t\tUserId: i,\n\t\t\t}\n\t\t\tworkflowRuns = append(workflowRuns, &client.WorkflowRun{\n\t\t\t\tName: \'concurrency-limit-round-robin-existing-workflows\',\n\t\t\t\tInput: event,\n\t\t\t})\n\n\t\t}\n\n\t\t_, err := c.Admin().BulkRunWorkflow(workflowRuns)\n\t\tif err != nil {\n\t\t\tfmt.Println(\'error running workflow\', err)\n\t\t}\n\n\t\tfmt.Println(\'ran workflows\')\n\n\t}()\n\n\ttime.Sleep(2 * time.Second)\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf(\'error starting worker: %w\', err)\n\t}\n\tdefer cleanup()\n\n\tfor {\n\t\tselect {\n\t\tcase <-ctx.Done():\n\t\t\treturn nil\n\t\tcase <-time.After(20 * time.Second):\n\t\t\treturn fmt.Errorf(\'timeout\')\n\t\tcase err := <-errChan:\n\t\t\treturn err\n\t\tcase <-done:\n\t\t\tcountMux.Lock()\n\t\t\tworkflowCount--\n\t\t\tcountMux.Unlock()\n\t\t\tif workflowCount == 0 {\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t\treturn nil\n\t\t\t}\n\n\t\t}\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/limit-concurrency/group-round-robin-advanced/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"sync\"\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\tConcurrencyKey string `json:\"concurrency_key\"`\n\tUserId int `json:\"user_id\"`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\"message\"`\n\tConcurrencyWhenFinished int `json:\"concurrency_when_finished\"`\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tctx, cancel := cmdutils.NewInterruptContext()\n\tdefer cancel()\n\n\tif err := run(ctx); err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc getConcurrencyKey(ctx worker.HatchetContext) (string, error) {\n\treturn \"concurrency\", nil\n}\n\nvar done = make(chan struct{})\nvar errChan = make(chan error)\n\nvar workflowCount int\nvar countMux sync.Mutex\n\nfunc run(ctx context.Context) 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\t// runningCount := 0\n\n\tcountMux := sync.Mutex{}\n\n\tvar countMap = make(map[string]int)\n\tmaxConcurrent := 2\n\n\terr = w.RegisterWorkflow(\n\n\t\t&worker.WorkflowJob{\n\t\t\tName: \"concurrency-limit-round-robin-existing-workflows\",\n\t\t\tDescription: \"This limits concurrency to maxConcurrent runs at a time.\",\n\t\t\tOn: worker.Events(\"test:concurrency-limit-round-robin-existing-workflows\"),\n\t\t\tConcurrency: worker.Expression(\"input.concurrency_key\").MaxRuns(int32(maxConcurrent)).LimitStrategy(types.GroupRoundRobin),\n\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\t\t\t\t\tconcurrencyKey := input.ConcurrencyKey\n\t\t\t\t\tcountMux.Lock()\n\n\t\t\t\t\tif countMap[concurrencyKey]+1 > maxConcurrent {\n\t\t\t\t\t\tcountMux.Unlock()\n\t\t\t\t\t\te := fmt.Errorf(\"concurrency limit exceeded for %d we have %d workers running\", input.UserId, countMap[concurrencyKey])\n\t\t\t\t\t\terrChan <- e\n\t\t\t\t\t\treturn nil, e\n\t\t\t\t\t}\n\t\t\t\t\tcountMap[concurrencyKey]++\n\n\t\t\t\t\tcountMux.Unlock()\n\n\t\t\t\t\tfmt.Println(\"received event\", input.UserId)\n\n\t\t\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\t\t\tfmt.Println(\"processed event\", input.UserId)\n\n\t\t\t\t\tcountMux.Lock()\n\t\t\t\t\tcountMap[concurrencyKey]--\n\t\t\t\t\tcountMux.Unlock()\n\n\t\t\t\t\tdone <- struct{}{}\n\n\t\t\t\t\treturn &stepOneOutput{}, 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\tgo func() {\n\t\tvar workflowRuns []*client.WorkflowRun\n\n\t\tfor i := 0; i < 1; i++ {\n\t\t\tworkflowCount++\n\t\t\tevent := concurrencyLimitEvent{\n\t\t\t\tConcurrencyKey: \"key\",\n\t\t\t\tUserId: i,\n\t\t\t}\n\t\t\tworkflowRuns = append(workflowRuns, &client.WorkflowRun{\n\t\t\t\tName: \"concurrency-limit-round-robin-existing-workflows\",\n\t\t\t\tInput: event,\n\t\t\t})\n\n\t\t}\n\n\t\t// create a second one with a different key\n\n\t\t// so the bug we are testing here is that total concurrency for any one group should be 2\n\t\t// but if we have more than one group we end up with 4 running when only 2 + 1 are eligible to run\n\n\t\tfor i := 0; i < 3; i++ {\n\t\t\tworkflowCount++\n\n\t\t\tevent := concurrencyLimitEvent{\n\t\t\t\tConcurrencyKey: \"secondKey\",\n\t\t\t\tUserId: i,\n\t\t\t}\n\t\t\tworkflowRuns = append(workflowRuns, &client.WorkflowRun{\n\t\t\t\tName: \"concurrency-limit-round-robin-existing-workflows\",\n\t\t\t\tInput: event,\n\t\t\t})\n\n\t\t}\n\n\t\t_, err := c.Admin().BulkRunWorkflow(workflowRuns)\n\t\tif err != nil {\n\t\t\tfmt.Println(\"error running workflow\", err)\n\t\t}\n\n\t\tfmt.Println(\"ran workflows\")\n\n\t}()\n\n\ttime.Sleep(2 * time.Second)\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error starting worker: %w\", err)\n\t}\n\tdefer cleanup()\n\n\tfor {\n\t\tselect {\n\t\tcase <-ctx.Done():\n\t\t\treturn nil\n\t\tcase <-time.After(20 * time.Second):\n\t\t\treturn fmt.Errorf(\"timeout\")\n\t\tcase err := <-errChan:\n\t\t\treturn err\n\t\tcase <-done:\n\t\t\tcountMux.Lock()\n\t\t\tworkflowCount--\n\t\t\tcountMux.Unlock()\n\t\t\tif workflowCount == 0 {\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t\treturn nil\n\t\t\t}\n\n\t\t}\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/limit-concurrency/group-round-robin-advanced/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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(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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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(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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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)\n\ntype userCreateEvent struct {\n\tUsername string `json:\'username\'`\n\tUserID string `json:\'user_id\'`\n\tData map[string]string `json:\'data\'`\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\ttime.Sleep(1 * time.Second)\n\n\t// trigger workflow\n\tworkflow, err := c.Admin().RunWorkflow(\n\t\t\'post-user-update\',\n\t\t&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\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\t\'hello\': \'world\',\n\t\t}),\n\t)\n\n\tif err != nil {\n\t\treturn fmt.Errorf(\'error running workflow: %w\', err)\n\t}\n\n\tfmt.Println(\'workflow run id:\', workflow.WorkflowRunId())\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\terr = c.Subscribe().On(interruptCtx, workflow.WorkflowRunId(), func(event client.WorkflowEvent) error {\n\t\tfmt.Println(event.EventPayload)\n\n\t\treturn nil\n\t})\n\n\treturn err\n}\n',
|
||||
'source': 'out/go/z_v0/manual-trigger/trigger/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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)\n\ntype userCreateEvent struct {\n\tUsername string `json:\"username\"`\n\tUserID string `json:\"user_id\"`\n\tData map[string]string `json:\"data\"`\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\ttime.Sleep(1 * time.Second)\n\n\t// trigger workflow\n\tworkflow, err := c.Admin().RunWorkflow(\n\t\t\"post-user-update\",\n\t\t&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\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\t\"hello\": \"world\",\n\t\t}),\n\t)\n\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error running workflow: %w\", err)\n\t}\n\n\tfmt.Println(\"workflow run id:\", workflow.WorkflowRunId())\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(ch)\n\tdefer cancel()\n\n\terr = c.Subscribe().On(interruptCtx, workflow.WorkflowRunId(), func(event client.WorkflowEvent) error {\n\t\tfmt.Println(event.EventPayload)\n\n\t\treturn nil\n\t})\n\n\treturn err\n}\n",
|
||||
"source": "out/go/z_v0/manual-trigger/trigger/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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 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)\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\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: \'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\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf(\'error starting worker: %w\', err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\treturn fmt.Errorf(\'error cleaning up: %w\', err)\n\t}\n\n\treturn nil\n}\n',
|
||||
'source': 'out/go/z_v0/manual-trigger/worker/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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 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)\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\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: \"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\tcleanup, err := w.Start()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error starting worker: %w\", err)\n\t}\n\n\t<-ch\n\n\tif err := cleanup(); err != nil {\n\t\treturn fmt.Errorf(\"error cleaning up: %w\", err)\n\t}\n\n\treturn nil\n}\n",
|
||||
"source": "out/go/z_v0/manual-trigger/worker/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\n\t\'github.com/joho/godotenv\'\n\n\t\'github.com/hatchet-dev/hatchet/pkg/cmdutils\'\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run(events)\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(\'cleanup() error = %v\', err))\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/middleware/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/joho/godotenv\"\n\n\t\"github.com/hatchet-dev/hatchet/pkg/cmdutils\"\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\tch := cmdutils.InterruptChan()\n\tcleanup, err := run(events)\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(\"cleanup() error = %v\", err))\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/middleware/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\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 stepOutput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\'error creating client: %v\', 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\tpanic(fmt.Sprintf(\'error creating worker: %v\', err))\n\t}\n\n\ttestSvc := w.NewService(\'test\')\n\n\terr = testSvc.On(\n\t\tworker.Events(\'simple\'),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \'simple-workflow\',\n\t\t\tDescription: \'Simple one-step workflow.\',\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\tfmt.Println(\'executed step 1\')\n\n\t\t\t\t\treturn &stepOutput{}, 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\tpanic(fmt.Sprintf(\'error registering workflow: %v\', err))\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\'error starting worker: %v\', err))\n\t}\n\n\t<-interruptCtx.Done()\n\tif err := cleanup(); err != nil {\n\t\tpanic(err)\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/no-tls/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\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 stepOutput struct{}\n\nfunc main() {\n\terr := godotenv.Load()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"error creating client: %v\", 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\tpanic(fmt.Sprintf(\"error creating worker: %v\", err))\n\t}\n\n\ttestSvc := w.NewService(\"test\")\n\n\terr = testSvc.On(\n\t\tworker.Events(\"simple\"),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \"simple-workflow\",\n\t\t\tDescription: \"Simple one-step workflow.\",\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\tfmt.Println(\"executed step 1\")\n\n\t\t\t\t\treturn &stepOutput{}, 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\tpanic(fmt.Sprintf(\"error registering workflow: %v\", err))\n\t}\n\n\tinterruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())\n\tdefer cancel()\n\n\tcleanup, err := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"error starting worker: %v\", err))\n\t}\n\n\t<-interruptCtx.Done()\n\tif err := cleanup(); err != nil {\n\t\tpanic(err)\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/no-tls/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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 stepOneOutput struct {\n\tMessage string `json:\'message\'`\n}\n\n// > OnFailure Step\n// This workflow will fail because the step will throw an error\n// we define an onFailure step to handle this case\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t// 👀 this step will always raise an exception\n\treturn nil, fmt.Errorf(\'test on failure\')\n}\n\nfunc OnFailure(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t// run cleanup code or notifications here\n\n\t// 👀 you can access the error from the failed step(s) like this\n\tfmt.Println(ctx.StepRunErrors())\n\n\treturn &stepOneOutput{\n\t\tMessage: \'Failure!\',\n\t}, nil\n}\n\nfunc main() {\n\t// ...\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// 👀 we define an onFailure step to handle this case\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \'on-failure-workflow\',\n\t\t\tDescription: \'This runs at a scheduled time.\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\'step-one\'),\n\t\t\t},\n\t\t\tOnFailure: &worker.WorkflowJob{\n\t\t\t\tName: \'scheduled-workflow-failure\',\n\t\t\t\tDescription: \'This runs when the scheduled workflow fails.\',\n\t\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\t\tworker.Fn(OnFailure).SetName(\'on-failure\'),\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t)\n\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 := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error cleaning up: %w\', 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\t// ,\n}\n\n',
|
||||
'source': 'out/go/z_v0/on-failure/main.go',
|
||||
'blocks': {
|
||||
'onfailure_step': {
|
||||
'start': 19,
|
||||
'stop': 108
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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 stepOneOutput struct {\n\tMessage string `json:\"message\"`\n}\n\n// > OnFailure Step\n// This workflow will fail because the step will throw an error\n// we define an onFailure step to handle this case\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t// 👀 this step will always raise an exception\n\treturn nil, fmt.Errorf(\"test on failure\")\n}\n\nfunc OnFailure(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t// run cleanup code or notifications here\n\n\t// 👀 you can access the error from the failed step(s) like this\n\tfmt.Println(ctx.StepRunErrors())\n\n\treturn &stepOneOutput{\n\t\tMessage: \"Failure!\",\n\t}, nil\n}\n\nfunc main() {\n\t// ...\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// 👀 we define an onFailure step to handle this case\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \"on-failure-workflow\",\n\t\t\tDescription: \"This runs at a scheduled time.\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\"step-one\"),\n\t\t\t},\n\t\t\tOnFailure: &worker.WorkflowJob{\n\t\t\t\tName: \"scheduled-workflow-failure\",\n\t\t\t\tDescription: \"This runs when the scheduled workflow fails.\",\n\t\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\t\tworker.Fn(OnFailure).SetName(\"on-failure\"),\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t)\n\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 := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error cleaning up: %w\", 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\t// ,\n}\n\n",
|
||||
"source": "out/go/z_v0/on-failure/main.go",
|
||||
"blocks": {
|
||||
"onfailure_step": {
|
||||
"start": 19,
|
||||
"stop": 108
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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 rateLimitInput struct {\n\tIndex int `json:\'index\'`\n\tUserId string `json:\'user_id\'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\'message\'`\n}\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &rateLimitInput{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tctx.StreamEvent([]byte(fmt.Sprintf(\'This is a stream event %d\', input.Index)))\n\n\treturn &stepOneOutput{\n\t\tMessage: fmt.Sprintf(\'This ran at %s\', time.Now().String()),\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = c.Admin().PutRateLimit(\'api1\', &types.RateLimitOpts{\n\t\tMax: 12,\n\t\tDuration: types.Minute,\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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tunitExpr := \'int(input.index) + 1\'\n\tkeyExpr := \'input.user_id\'\n\tlimitValueExpr := \'3\'\n\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \'rate-limit-workflow\',\n\t\t\tDescription: \'This illustrates rate limiting.\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\'step-one\').SetRateLimit(\n\t\t\t\t\tworker.RateLimit{\n\t\t\t\t\t\tKey: \'per-user-rate-limit\',\n\t\t\t\t\t\tKeyExpr: &keyExpr,\n\t\t\t\t\t\tUnitsExpr: &unitExpr,\n\t\t\t\t\t\tLimitValueExpr: &limitValueExpr,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor i := 0; i < 12; i++ {\n\t\tfor j := 0; j < 3; j++ {\n\t\t\t_, err = c.Admin().RunWorkflow(\'rate-limit-workflow\', &rateLimitInput{\n\t\t\t\tIndex: j,\n\t\t\t\tUserId: fmt.Sprintf(\'user-%d\', i),\n\t\t\t})\n\n\t\t\tif err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\t\t}\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\t<-interrupt\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/rate-limit/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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 rateLimitInput struct {\n\tIndex int `json:\"index\"`\n\tUserId string `json:\"user_id\"`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\"message\"`\n}\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &rateLimitInput{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tctx.StreamEvent([]byte(fmt.Sprintf(\"This is a stream event %d\", input.Index)))\n\n\treturn &stepOneOutput{\n\t\tMessage: fmt.Sprintf(\"This ran at %s\", time.Now().String()),\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = c.Admin().PutRateLimit(\"api1\", &types.RateLimitOpts{\n\t\tMax: 12,\n\t\tDuration: types.Minute,\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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tunitExpr := \"int(input.index) + 1\"\n\tkeyExpr := \"input.user_id\"\n\tlimitValueExpr := \"3\"\n\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \"rate-limit-workflow\",\n\t\t\tDescription: \"This illustrates rate limiting.\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\"step-one\").SetRateLimit(\n\t\t\t\t\tworker.RateLimit{\n\t\t\t\t\t\tKey: \"per-user-rate-limit\",\n\t\t\t\t\t\tKeyExpr: &keyExpr,\n\t\t\t\t\t\tUnitsExpr: &unitExpr,\n\t\t\t\t\t\tLimitValueExpr: &limitValueExpr,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor i := 0; i < 12; i++ {\n\t\tfor j := 0; j < 3; j++ {\n\t\t\t_, err = c.Admin().RunWorkflow(\"rate-limit-workflow\", &rateLimitInput{\n\t\t\t\tIndex: j,\n\t\t\t\tUserId: fmt.Sprintf(\"user-%d\", i),\n\t\t\t})\n\n\t\t\tif err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\t\t}\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\t<-interrupt\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/rate-limit/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\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 stepOneOutput struct {\n\tMessage string `json:\'message\'`\n}\n\n// > Backoff\n\n// ... normal function definition\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tif ctx.RetryCount() < 3 {\n\t\treturn nil, fmt.Errorf(\'failure\')\n\t}\n\n\treturn &stepOneOutput{\n\t\tMessage: \'Success!\',\n\t}, nil\n}\n\n// ,\n\nfunc main() {\n\t// ...\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ,\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tName: \'retry-with-backoff-workflow\',\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tDescription: \'Demonstrates retry with exponential backoff.\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\'with-backoff\').\n\t\t\t\t\tSetRetries(10).\n\t\t\t\t\t// 👀 Backoff configuration\n\t\t\t\t\t// 👀 Maximum number of seconds to wait between retries\n\t\t\t\t\tSetRetryBackoffFactor(2.0).\n\t\t\t\t\t// 👀 Factor to increase the wait time between retries.\n\t\t\t\t\t// This sequence will be 2s, 4s, 8s, 16s, 32s, 60s... due to the maxSeconds limit\n\t\t\t\t\tSetRetryMaxBackoffSeconds(60),\n\t\t\t},\n\t\t},\n\t)\n\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 := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error cleaning up: %w\', err))\n\t}\n\n\t<-interruptCtx.Done()\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf(\'error cleaning up: %w\', err))\n\t}\n\n\t// ,\n}\n\n',
|
||||
'source': 'out/go/z_v0/retries-with-backoff/main.go',
|
||||
'blocks': {
|
||||
'backoff': {
|
||||
'start': 18,
|
||||
'stop': 98
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\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 stepOneOutput struct {\n\tMessage string `json:\"message\"`\n}\n\n// > Backoff\n\n// ... normal function definition\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tif ctx.RetryCount() < 3 {\n\t\treturn nil, fmt.Errorf(\"failure\")\n\t}\n\n\treturn &stepOneOutput{\n\t\tMessage: \"Success!\",\n\t}, nil\n}\n\n// ,\n\nfunc main() {\n\t// ...\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ,\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tName: \"retry-with-backoff-workflow\",\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tDescription: \"Demonstrates retry with exponential backoff.\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\"with-backoff\").\n\t\t\t\t\tSetRetries(10).\n\t\t\t\t\t// 👀 Backoff configuration\n\t\t\t\t\t// 👀 Maximum number of seconds to wait between retries\n\t\t\t\t\tSetRetryBackoffFactor(2.0).\n\t\t\t\t\t// 👀 Factor to increase the wait time between retries.\n\t\t\t\t\t// This sequence will be 2s, 4s, 8s, 16s, 32s, 60s... due to the maxSeconds limit\n\t\t\t\t\tSetRetryMaxBackoffSeconds(60),\n\t\t\t},\n\t\t},\n\t)\n\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 := w.Start()\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error cleaning up: %w\", err))\n\t}\n\n\t<-interruptCtx.Done()\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf(\"error cleaning up: %w\", err))\n\t}\n\n\t// ,\n}\n\n",
|
||||
"source": "out/go/z_v0/retries-with-backoff/main.go",
|
||||
"blocks": {
|
||||
"backoff": {
|
||||
"start": 18,
|
||||
"stop": 98
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
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\n// > Create\n// ... normal workflow definition\ntype printOutput struct{}\n\nfunc print(ctx context.Context) (result *printOutput, err error) {\n\tfmt.Println(\'called print:print\')\n\n\treturn &printOutput{}, nil\n}\n\n// ,\nfunc main() {\n\t// ... initialize client, worker and workflow\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tName: \'schedule-workflow\',\n\t\t\tDescription: \'Demonstrates a simple scheduled workflow\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(print),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ,\n\n\tgo func() {\n\t\t// 👀 define the scheduled workflow to run in a minute\n\t\tschedule, err := c.Schedule().Create(\n\t\t\tcontext.Background(),\n\t\t\t\'schedule-workflow\',\n\t\t\t&client.ScheduleOpts{\n\t\t\t\t// 👀 define the time to run the scheduled workflow, in UTC\n\t\t\t\tTriggerAt: time.Now().UTC().Add(time.Minute),\n\t\t\t\tInput: map[string]interface{}{\n\t\t\t\t\t\'message\': \'Hello, world!\',\n\t\t\t\t},\n\t\t\t\tAdditionalMetadata: map[string]string{},\n\t\t\t},\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tfmt.Println(schedule.TriggerAt, schedule.WorkflowName)\n\t}()\n\n\t// ... wait for interrupt signal\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\t// ,\n}\n\n\nfunc ListScheduledWorkflows() {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > List\n\tschedules, err := c.Schedule().List(context.Background())\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, schedule := range *schedules.Rows {\n\t\tfmt.Println(schedule.TriggerAt, schedule.WorkflowName)\n\t}\n}\n\nfunc DeleteScheduledWorkflow(id string) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > Delete\n\t// 👀 id is the schedule\'s metadata id, can get it via schedule.Metadata.Id\n\terr = c.Schedule().Delete(context.Background(), id)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/scheduled/main.go',
|
||||
'blocks': {
|
||||
'create': {
|
||||
'start': 16,
|
||||
'stop': 107
|
||||
"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\n// > Create\n// ... normal workflow definition\ntype printOutput struct{}\n\nfunc print(ctx context.Context) (result *printOutput, err error) {\n\tfmt.Println(\"called print:print\")\n\n\treturn &printOutput{}, nil\n}\n\n// ,\nfunc main() {\n\t// ... initialize client, worker and workflow\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.RegisterWorkflow(\n\t\t&worker.WorkflowJob{\n\t\t\tOn: worker.NoTrigger(),\n\t\t\tName: \"schedule-workflow\",\n\t\t\tDescription: \"Demonstrates a simple scheduled workflow\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(print),\n\t\t\t},\n\t\t},\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tinterrupt := cmdutils.InterruptChan()\n\n\tcleanup, err := w.Start()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// ,\n\n\tgo func() {\n\t\t// 👀 define the scheduled workflow to run in a minute\n\t\tschedule, err := c.Schedule().Create(\n\t\t\tcontext.Background(),\n\t\t\t\"schedule-workflow\",\n\t\t\t&client.ScheduleOpts{\n\t\t\t\t// 👀 define the time to run the scheduled workflow, in UTC\n\t\t\t\tTriggerAt: time.Now().UTC().Add(time.Minute),\n\t\t\t\tInput: map[string]interface{}{\n\t\t\t\t\t\"message\": \"Hello, world!\",\n\t\t\t\t},\n\t\t\t\tAdditionalMetadata: map[string]string{},\n\t\t\t},\n\t\t)\n\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tfmt.Println(schedule.TriggerAt, schedule.WorkflowName)\n\t}()\n\n\t// ... wait for interrupt signal\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\t// ,\n}\n\n\nfunc ListScheduledWorkflows() {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > List\n\tschedules, err := c.Schedule().List(context.Background())\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, schedule := range *schedules.Rows {\n\t\tfmt.Println(schedule.TriggerAt, schedule.WorkflowName)\n\t}\n}\n\nfunc DeleteScheduledWorkflow(id string) {\n\tc, err := client.New()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// > Delete\n\t// 👀 id is the schedule's metadata id, can get it via schedule.Metadata.Id\n\terr = c.Schedule().Delete(context.Background(), id)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/scheduled/main.go",
|
||||
"blocks": {
|
||||
"create": {
|
||||
"start": 16,
|
||||
"stop": 107
|
||||
},
|
||||
'list': {
|
||||
'start': 117,
|
||||
'stop': 117
|
||||
"list": {
|
||||
"start": 117,
|
||||
"stop": 117
|
||||
},
|
||||
'delete': {
|
||||
'start': 136,
|
||||
'stop': 137
|
||||
"delete": {
|
||||
"start": 136,
|
||||
"stop": 137
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\t\'math/rand/v2\'\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 streamEventInput struct {\n\tIndex int `json:\'index\'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\'message\'`\n}\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &streamEventInput{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tctx.StreamEvent([]byte(fmt.Sprintf(\'This is a stream event %d\', input.Index)))\n\n\treturn &stepOneOutput{\n\t\tMessage: fmt.Sprintf(\'This ran at %s\', time.Now().String()),\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \'stream-event-workflow\',\n\t\t\tDescription: \'This sends a stream event.\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\'step-one\'),\n\t\t\t},\n\t\t},\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\t_, err = w.Start()\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error cleaning up: %w\', err))\n\t}\n\n\t// Generate a random number between 1 and 100\n\tstreamKey := \'streamKey\'\n\tstreamValue := fmt.Sprintf(\'stream-event-%d\', rand.IntN(100)+1)\n\n\t_, err = c.Admin().RunWorkflow(\'stream-event-workflow\', &streamEventInput{\n\t\tIndex: 0,\n\t},\n\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\tstreamKey: streamValue,\n\t\t}),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = c.Subscribe().StreamByAdditionalMetadata(interruptCtx, streamKey, streamValue, func(event client.StreamEvent) error {\n\t\tfmt.Println(string(event.Message))\n\t\treturn nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/stream-event-by-meta/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\t\"math/rand/v2\"\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 streamEventInput struct {\n\tIndex int `json:\"index\"`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\"message\"`\n}\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &streamEventInput{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tctx.StreamEvent([]byte(fmt.Sprintf(\"This is a stream event %d\", input.Index)))\n\n\treturn &stepOneOutput{\n\t\tMessage: fmt.Sprintf(\"This ran at %s\", time.Now().String()),\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \"stream-event-workflow\",\n\t\t\tDescription: \"This sends a stream event.\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\"step-one\"),\n\t\t\t},\n\t\t},\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\t_, err = w.Start()\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error cleaning up: %w\", err))\n\t}\n\n\t// Generate a random number between 1 and 100\n\tstreamKey := \"streamKey\"\n\tstreamValue := fmt.Sprintf(\"stream-event-%d\", rand.IntN(100)+1)\n\n\t_, err = c.Admin().RunWorkflow(\"stream-event-workflow\", &streamEventInput{\n\t\tIndex: 0,\n\t},\n\t\tclient.WithRunMetadata(map[string]interface{}{\n\t\t\tstreamKey: streamValue,\n\t\t}),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = c.Subscribe().StreamByAdditionalMetadata(interruptCtx, streamKey, streamValue, func(event client.StreamEvent) error {\n\t\tfmt.Println(string(event.Message))\n\t\treturn nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/stream-event-by-meta/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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 streamEventInput struct {\n\tIndex int `json:\'index\'`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\'message\'`\n}\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &streamEventInput{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tctx.StreamEvent([]byte(fmt.Sprintf(\'This is a stream event %d\', input.Index)))\n\n\treturn &stepOneOutput{\n\t\tMessage: fmt.Sprintf(\'This ran at %s\', time.Now().String()),\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \'stream-event-workflow\',\n\t\t\tDescription: \'This sends a stream event.\',\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\'step-one\'),\n\t\t\t},\n\t\t},\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\t_, err = w.Start()\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\'error cleaning up: %w\', err))\n\t}\n\n\tworkflow, err := c.Admin().RunWorkflow(\'stream-event-workflow\', &streamEventInput{\n\t\tIndex: 0,\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = c.Subscribe().Stream(interruptCtx, workflow.WorkflowRunId(), func(event client.StreamEvent) error {\n\t\tfmt.Println(string(event.Message))\n\n\t\treturn nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/stream-event/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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 streamEventInput struct {\n\tIndex int `json:\"index\"`\n}\n\ntype stepOneOutput struct {\n\tMessage string `json:\"message\"`\n}\n\nfunc StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\tinput := &streamEventInput{}\n\n\terr = ctx.WorkflowInput(input)\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tctx.StreamEvent([]byte(fmt.Sprintf(\"This is a stream event %d\", input.Index)))\n\n\treturn &stepOneOutput{\n\t\tMessage: fmt.Sprintf(\"This ran at %s\", time.Now().String()),\n\t}, nil\n}\n\nfunc main() {\n\terr := godotenv.Load()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tc, 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\tc,\n\t\t),\n\t)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = w.On(\n\t\tworker.NoTrigger(),\n\t\t&worker.WorkflowJob{\n\t\t\tName: \"stream-event-workflow\",\n\t\t\tDescription: \"This sends a stream event.\",\n\t\t\tSteps: []*worker.WorkflowStep{\n\t\t\t\tworker.Fn(StepOne).SetName(\"step-one\"),\n\t\t\t},\n\t\t},\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\t_, err = w.Start()\n\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"error cleaning up: %w\", err))\n\t}\n\n\tworkflow, err := c.Admin().RunWorkflow(\"stream-event-workflow\", &streamEventInput{\n\t\tIndex: 0,\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = c.Subscribe().Stream(interruptCtx, workflow.WorkflowRunId(), func(event client.StreamEvent) error {\n\t\tfmt.Println(string(event.Message))\n\n\t\treturn nil\n\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/stream-event/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\n\t\'fmt\'\n\t\'time\'\n\n\t\'github.com/joho/godotenv\'\n\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\n\t// > TimeoutStep\n\tcleanup, err := run(events, worker.WorkflowJob{\n\t\tName: \'timeout\',\n\t\tDescription: \'timeout\',\n\t\tSteps: []*worker.WorkflowStep{\n\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\ttime.Sleep(time.Second * 60)\n\t\t\t\treturn nil, nil\n\t\t\t}).SetName(\'step-one\').SetTimeout(\'10s\'),\n\t\t},\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-events\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf(\'cleanup() error = %v\', err))\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/timeout/main.go',
|
||||
'blocks': {
|
||||
'timeoutstep': {
|
||||
'start': 31,
|
||||
'stop': 40
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/joho/godotenv\"\n\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\n\t// > TimeoutStep\n\tcleanup, err := run(events, worker.WorkflowJob{\n\t\tName: \"timeout\",\n\t\tDescription: \"timeout\",\n\t\tSteps: []*worker.WorkflowStep{\n\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {\n\t\t\t\ttime.Sleep(time.Second * 60)\n\t\t\t\treturn nil, nil\n\t\t\t}).SetName(\"step-one\").SetTimeout(\"10s\"),\n\t\t},\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t<-events\n\n\tif err := cleanup(); err != nil {\n\t\tpanic(fmt.Errorf(\"cleanup() error = %v\", err))\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/timeout/main.go",
|
||||
"blocks": {
|
||||
"timeoutstep": {
|
||||
"start": 31,
|
||||
"stop": 40
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'go',
|
||||
'content': 'package main\n\nimport (\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/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 output 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\tc, err := client.New()\n\tif err != nil {\n\t\tpanic(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\tpanic(fmt.Errorf(\'error creating worker: %w\', err))\n\t}\n\n\tworkflow := \'webhook\'\n\tevent := \'user:create:webhook\'\n\twf := &worker.WorkflowJob{\n\t\tName: workflow,\n\t\tDescription: workflow,\n\t\tSteps: []*worker.WorkflowStep{\n\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *output, err error) {\n\t\t\t\tlog.Printf(\'step name: %s\', ctx.StepName())\n\t\t\t\treturn &output{\n\t\t\t\t\tMessage: \'hi from \' + ctx.StepName(),\n\t\t\t\t}, nil\n\t\t\t}).SetName(\'webhook-step-one\').SetTimeout(\'10s\'),\n\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *output, err error) {\n\t\t\t\tlog.Printf(\'step name: %s\', ctx.StepName())\n\t\t\t\treturn &output{\n\t\t\t\t\tMessage: \'hi from \' + ctx.StepName(),\n\t\t\t\t}, nil\n\t\t\t}).SetName(\'webhook-step-one\').SetTimeout(\'10s\'),\n\t\t},\n\t}\n\n\thandler := w.WebhookHttpHandler(worker.WebhookHandlerOptions{\n\t\tSecret: \'secret\',\n\t}, wf)\n\tport := \'8741\'\n\terr = run(\'webhook-demo\', w, port, handler, c, workflow, event)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n',
|
||||
'source': 'out/go/z_v0/webhook/main.go',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "go",
|
||||
"content": "package main\n\nimport (\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/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 output 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\tc, err := client.New()\n\tif err != nil {\n\t\tpanic(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\tpanic(fmt.Errorf(\"error creating worker: %w\", err))\n\t}\n\n\tworkflow := \"webhook\"\n\tevent := \"user:create:webhook\"\n\twf := &worker.WorkflowJob{\n\t\tName: workflow,\n\t\tDescription: workflow,\n\t\tSteps: []*worker.WorkflowStep{\n\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *output, err error) {\n\t\t\t\tlog.Printf(\"step name: %s\", ctx.StepName())\n\t\t\t\treturn &output{\n\t\t\t\t\tMessage: \"hi from \" + ctx.StepName(),\n\t\t\t\t}, nil\n\t\t\t}).SetName(\"webhook-step-one\").SetTimeout(\"10s\"),\n\t\t\tworker.Fn(func(ctx worker.HatchetContext) (result *output, err error) {\n\t\t\t\tlog.Printf(\"step name: %s\", ctx.StepName())\n\t\t\t\treturn &output{\n\t\t\t\t\tMessage: \"hi from \" + ctx.StepName(),\n\t\t\t\t}, nil\n\t\t\t}).SetName(\"webhook-step-one\").SetTimeout(\"10s\"),\n\t\t},\n\t}\n\n\thandler := w.WebhookHttpHandler(worker.WebhookHandlerOptions{\n\t\tSecret: \"secret\",\n\t}, wf)\n\tport := \"8741\"\n\terr = run(\"webhook-demo\", w, port, handler, c, workflow, event)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n",
|
||||
"source": "out/go/z_v0/webhook/main.go",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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)\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': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"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)\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": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': '',
|
||||
'source': 'out/python/__init__.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "",
|
||||
"source": "out/python/__init__.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from examples.affinity_workers.worker import affinity_worker_workflow\nfrom hatchet_sdk import TriggerWorkflowOptions\n\naffinity_worker_workflow.run(\n options=TriggerWorkflowOptions(additional_metadata={\'hello\': \'moon\'}),\n)\n',
|
||||
'source': 'out/python/affinity_workers/trigger.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "from examples.affinity_workers.worker import affinity_worker_workflow\nfrom hatchet_sdk import TriggerWorkflowOptions\n\naffinity_worker_workflow.run(\n options=TriggerWorkflowOptions(additional_metadata={\"hello\": \"moon\"}),\n)\n",
|
||||
"source": "out/python/affinity_workers/trigger.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from hatchet_sdk import Context, EmptyModel, Hatchet, WorkerLabelComparator\nfrom hatchet_sdk.labels import DesiredWorkerLabel\n\nhatchet = Hatchet(debug=True)\n\n\n# > AffinityWorkflow\n\naffinity_worker_workflow = hatchet.workflow(name=\'AffinityWorkflow\')\n\n\n@affinity_worker_workflow.task(\n desired_worker_labels={\n \'model\': DesiredWorkerLabel(value=\'fancy-ai-model-v2\', weight=10),\n \'memory\': DesiredWorkerLabel(\n value=256,\n required=True,\n comparator=WorkerLabelComparator.LESS_THAN,\n ),\n },\n)\n\n\n\n# > AffinityTask\nasync def step(input: EmptyModel, ctx: Context) -> dict[str, str | None]:\n if ctx.worker.labels().get(\'model\') != \'fancy-ai-model-v2\':\n ctx.worker.upsert_labels({\'model\': \'unset\'})\n # DO WORK TO EVICT OLD MODEL / LOAD NEW MODEL\n ctx.worker.upsert_labels({\'model\': \'fancy-ai-model-v2\'})\n\n return {\'worker\': ctx.worker.id()}\n\n\n\n\ndef main() -> None:\n\n # > AffinityWorker\n worker = hatchet.worker(\n \'affinity-worker\',\n slots=10,\n labels={\n \'model\': \'fancy-ai-model-v2\',\n \'memory\': 512,\n },\n workflows=[affinity_worker_workflow],\n )\n worker.start()\n\n\n\nif __name__ == \'__main__\':\n main()\n',
|
||||
'source': 'out/python/affinity_workers/worker.py',
|
||||
'blocks': {
|
||||
'affinityworkflow': {
|
||||
'start': 8,
|
||||
'stop': 22
|
||||
"language": "python",
|
||||
"content": "from hatchet_sdk import Context, EmptyModel, Hatchet, WorkerLabelComparator\nfrom hatchet_sdk.labels import DesiredWorkerLabel\n\nhatchet = Hatchet(debug=True)\n\n\n# > AffinityWorkflow\n\naffinity_worker_workflow = hatchet.workflow(name=\"AffinityWorkflow\")\n\n\n@affinity_worker_workflow.task(\n desired_worker_labels={\n \"model\": DesiredWorkerLabel(value=\"fancy-ai-model-v2\", weight=10),\n \"memory\": DesiredWorkerLabel(\n value=256,\n required=True,\n comparator=WorkerLabelComparator.LESS_THAN,\n ),\n },\n)\n\n\n\n# > AffinityTask\nasync def step(input: EmptyModel, ctx: Context) -> dict[str, str | None]:\n if ctx.worker.labels().get(\"model\") != \"fancy-ai-model-v2\":\n ctx.worker.upsert_labels({\"model\": \"unset\"})\n # DO WORK TO EVICT OLD MODEL / LOAD NEW MODEL\n ctx.worker.upsert_labels({\"model\": \"fancy-ai-model-v2\"})\n\n return {\"worker\": ctx.worker.id()}\n\n\n\n\ndef main() -> None:\n\n # > AffinityWorker\n worker = hatchet.worker(\n \"affinity-worker\",\n slots=10,\n labels={\n \"model\": \"fancy-ai-model-v2\",\n \"memory\": 512,\n },\n workflows=[affinity_worker_workflow],\n )\n worker.start()\n\n\n\nif __name__ == \"__main__\":\n main()\n",
|
||||
"source": "out/python/affinity_workers/worker.py",
|
||||
"blocks": {
|
||||
"affinityworkflow": {
|
||||
"start": 8,
|
||||
"stop": 22
|
||||
},
|
||||
'affinitytask': {
|
||||
'start': 26,
|
||||
'stop': 34
|
||||
"affinitytask": {
|
||||
"start": 26,
|
||||
"stop": 34
|
||||
},
|
||||
'affinityworker': {
|
||||
'start': 40,
|
||||
'stop': 51
|
||||
"affinityworker": {
|
||||
"start": 40,
|
||||
"stop": 51
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from hatchet_sdk import Hatchet\n\nhatchet = Hatchet(debug=True)\n\n\ndef main() -> None:\n workflow_list = hatchet.workflows.list()\n rows = workflow_list.rows or []\n\n for workflow in rows:\n print(workflow.name)\n print(workflow.metadata.id)\n print(workflow.metadata.created_at)\n print(workflow.metadata.updated_at)\n\n\nif __name__ == \'__main__\':\n main()\n',
|
||||
'source': 'out/python/api/api.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "from hatchet_sdk import Hatchet\n\nhatchet = Hatchet(debug=True)\n\n\ndef main() -> None:\n workflow_list = hatchet.workflows.list()\n rows = workflow_list.rows or []\n\n for workflow in rows:\n print(workflow.name)\n print(workflow.metadata.id)\n print(workflow.metadata.created_at)\n print(workflow.metadata.updated_at)\n\n\nif __name__ == \"__main__\":\n main()\n",
|
||||
"source": "out/python/api/api.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import asyncio\n\nfrom hatchet_sdk import Hatchet\n\nhatchet = Hatchet(debug=True)\n\n\nasync def main() -> None:\n workflow_list = await hatchet.workflows.aio_list()\n rows = workflow_list.rows or []\n\n for workflow in rows:\n print(workflow.name)\n print(workflow.metadata.id)\n print(workflow.metadata.created_at)\n print(workflow.metadata.updated_at)\n\n\nif __name__ == \'__main__\':\n asyncio.run(main())\n',
|
||||
'source': 'out/python/api/async_api.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "import asyncio\n\nfrom hatchet_sdk import Hatchet\n\nhatchet = Hatchet(debug=True)\n\n\nasync def main() -> None:\n workflow_list = await hatchet.workflows.aio_list()\n rows = workflow_list.rows or []\n\n for workflow in rows:\n print(workflow.name)\n print(workflow.metadata.id)\n print(workflow.metadata.created_at)\n print(workflow.metadata.updated_at)\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n",
|
||||
"source": "out/python/api/async_api.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from examples.blocked_async.worker import blocked_worker_workflow\nfrom hatchet_sdk import TriggerWorkflowOptions\n\nblocked_worker_workflow.run(\n options=TriggerWorkflowOptions(additional_metadata={\'hello\': \'moon\'}),\n)\n',
|
||||
'source': 'out/python/blocked_async/trigger.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "from examples.blocked_async.worker import blocked_worker_workflow\nfrom hatchet_sdk import TriggerWorkflowOptions\n\nblocked_worker_workflow.run(\n options=TriggerWorkflowOptions(additional_metadata={\"hello\": \"moon\"}),\n)\n",
|
||||
"source": "out/python/blocked_async/trigger.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import hashlib\nimport time\nfrom datetime import timedelta\n\nfrom hatchet_sdk import Context, EmptyModel, Hatchet\n\nhatchet = Hatchet(debug=True)\n\n# WARNING: this is an example of what NOT to do\n# This workflow is intentionally blocking the main thread\n# and will block the worker from processing other workflows\n#\n# You do not want to run long sync functions in an async def function\n\nblocked_worker_workflow = hatchet.workflow(name=\'Blocked\')\n\n\n@blocked_worker_workflow.task(execution_timeout=timedelta(seconds=11), retries=3)\nasync def step1(input: EmptyModel, ctx: Context) -> dict[str, str | int | float]:\n print(\'Executing step1\')\n\n # CPU-bound task: Calculate a large number of SHA-256 hashes\n start_time = time.time()\n iterations = 10_000_000\n for i in range(iterations):\n hashlib.sha256(f\'data{i}\'.encode()).hexdigest()\n\n end_time = time.time()\n execution_time = end_time - start_time\n\n print(f\'Completed {iterations} hash calculations in {execution_time:.2f} seconds\')\n\n return {\n \'step1\': \'step1\',\n \'iterations\': iterations,\n \'execution_time\': execution_time,\n }\n\n\ndef main() -> None:\n worker = hatchet.worker(\n \'blocked-worker\', slots=3, workflows=[blocked_worker_workflow]\n )\n worker.start()\n\n\nif __name__ == \'__main__\':\n main()\n',
|
||||
'source': 'out/python/blocked_async/worker.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "import hashlib\nimport time\nfrom datetime import timedelta\n\nfrom hatchet_sdk import Context, EmptyModel, Hatchet\n\nhatchet = Hatchet(debug=True)\n\n# WARNING: this is an example of what NOT to do\n# This workflow is intentionally blocking the main thread\n# and will block the worker from processing other workflows\n#\n# You do not want to run long sync functions in an async def function\n\nblocked_worker_workflow = hatchet.workflow(name=\"Blocked\")\n\n\n@blocked_worker_workflow.task(execution_timeout=timedelta(seconds=11), retries=3)\nasync def step1(input: EmptyModel, ctx: Context) -> dict[str, str | int | float]:\n print(\"Executing step1\")\n\n # CPU-bound task: Calculate a large number of SHA-256 hashes\n start_time = time.time()\n iterations = 10_000_000\n for i in range(iterations):\n hashlib.sha256(f\"data{i}\".encode()).hexdigest()\n\n end_time = time.time()\n execution_time = end_time - start_time\n\n print(f\"Completed {iterations} hash calculations in {execution_time:.2f} seconds\")\n\n return {\n \"step1\": \"step1\",\n \"iterations\": iterations,\n \"execution_time\": execution_time,\n }\n\n\ndef main() -> None:\n worker = hatchet.worker(\n \"blocked-worker\", slots=3, workflows=[blocked_worker_workflow]\n )\n worker.start()\n\n\nif __name__ == \"__main__\":\n main()\n",
|
||||
"source": "out/python/blocked_async/worker.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import asyncio\n\nfrom examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\nfrom hatchet_sdk import Hatchet\nfrom hatchet_sdk.clients.admin import TriggerWorkflowOptions\n\nhatchet = Hatchet()\n\n\nasync def main() -> None:\n results = bulk_parent_wf.run_many(\n workflows=[\n bulk_parent_wf.create_bulk_run_item(\n input=ParentInput(n=i),\n options=TriggerWorkflowOptions(\n additional_metadata={\n \'bulk-trigger\': i,\n \'hello-{i}\': \'earth-{i}\',\n }\n ),\n )\n for i in range(20)\n ],\n )\n\n for result in results:\n print(result)\n\n\nif __name__ == \'__main__\':\n asyncio.run(main())\n',
|
||||
'source': 'out/python/bulk_fanout/bulk_trigger.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "import asyncio\n\nfrom examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\nfrom hatchet_sdk import Hatchet\nfrom hatchet_sdk.clients.admin import TriggerWorkflowOptions\n\nhatchet = Hatchet()\n\n\nasync def main() -> None:\n results = bulk_parent_wf.run_many(\n workflows=[\n bulk_parent_wf.create_bulk_run_item(\n input=ParentInput(n=i),\n options=TriggerWorkflowOptions(\n additional_metadata={\n \"bulk-trigger\": i,\n \"hello-{i}\": \"earth-{i}\",\n }\n ),\n )\n for i in range(20)\n ],\n )\n\n for result in results:\n print(result)\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n",
|
||||
"source": "out/python/bulk_fanout/bulk_trigger.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import asyncio\nimport random\n\nfrom examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\nfrom hatchet_sdk import Hatchet\nfrom hatchet_sdk.clients.admin import TriggerWorkflowOptions\n\n\nasync def main() -> None:\n hatchet = Hatchet()\n\n # Generate a random stream key to use to track all\n # stream events for this workflow run.\n\n streamKey = \'streamKey\'\n streamVal = f\'sk-{random.randint(1, 100)}\'\n\n # Specify the stream key as additional metadata\n # when running the workflow.\n\n # This key gets propagated to all child workflows\n # and can have an arbitrary property name.\n bulk_parent_wf.run(\n input=ParentInput(n=2),\n options=TriggerWorkflowOptions(additional_metadata={streamKey: streamVal}),\n )\n\n # Stream all events for the additional meta key value\n listener = hatchet.listener.stream_by_additional_metadata(streamKey, streamVal)\n\n async for event in listener:\n print(event.type, event.payload)\n\n\nif __name__ == \'__main__\':\n asyncio.run(main())\n',
|
||||
'source': 'out/python/bulk_fanout/stream.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "import asyncio\nimport random\n\nfrom examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\nfrom hatchet_sdk import Hatchet\nfrom hatchet_sdk.clients.admin import TriggerWorkflowOptions\n\n\nasync def main() -> None:\n hatchet = Hatchet()\n\n # Generate a random stream key to use to track all\n # stream events for this workflow run.\n\n streamKey = \"streamKey\"\n streamVal = f\"sk-{random.randint(1, 100)}\"\n\n # Specify the stream key as additional metadata\n # when running the workflow.\n\n # This key gets propagated to all child workflows\n # and can have an arbitrary property name.\n bulk_parent_wf.run(\n input=ParentInput(n=2),\n options=TriggerWorkflowOptions(additional_metadata={streamKey: streamVal}),\n )\n\n # Stream all events for the additional meta key value\n listener = hatchet.listener.stream_by_additional_metadata(streamKey, streamVal)\n\n async for event in listener:\n print(event.type, event.payload)\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n",
|
||||
"source": "out/python/bulk_fanout/stream.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import pytest\n\nfrom examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\n\n\n@pytest.mark.asyncio(loop_scope=\'session\')\nasync def test_run() -> None:\n result = await bulk_parent_wf.aio_run(input=ParentInput(n=12))\n\n assert len(result[\'spawn\'][\'results\']) == 12\n',
|
||||
'source': 'out/python/bulk_fanout/test_bulk_fanout.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "import pytest\n\nfrom examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\n\n\n@pytest.mark.asyncio(loop_scope=\"session\")\nasync def test_run() -> None:\n result = await bulk_parent_wf.aio_run(input=ParentInput(n=12))\n\n assert len(result[\"spawn\"][\"results\"]) == 12\n",
|
||||
"source": "out/python/bulk_fanout/test_bulk_fanout.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\nfrom hatchet_sdk import TriggerWorkflowOptions\n\nbulk_parent_wf.run(\n ParentInput(n=999),\n TriggerWorkflowOptions(additional_metadata={\'no-dedupe\': \'world\'}),\n)\n',
|
||||
'source': 'out/python/bulk_fanout/trigger.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "from examples.bulk_fanout.worker import ParentInput, bulk_parent_wf\nfrom hatchet_sdk import TriggerWorkflowOptions\n\nbulk_parent_wf.run(\n ParentInput(n=999),\n TriggerWorkflowOptions(additional_metadata={\"no-dedupe\": \"world\"}),\n)\n",
|
||||
"source": "out/python/bulk_fanout/trigger.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'from datetime import timedelta\nfrom typing import Any\n\nfrom pydantic import BaseModel\n\nfrom hatchet_sdk import Context, Hatchet\nfrom hatchet_sdk.clients.admin import TriggerWorkflowOptions\n\nhatchet = Hatchet(debug=True)\n\n\nclass ParentInput(BaseModel):\n n: int = 100\n\n\nclass ChildInput(BaseModel):\n a: str\n\n\nbulk_parent_wf = hatchet.workflow(name=\'BulkFanoutParent\', input_validator=ParentInput)\nbulk_child_wf = hatchet.workflow(name=\'BulkFanoutChild\', input_validator=ChildInput)\n\n\n# > BulkFanoutParent\n@bulk_parent_wf.task(execution_timeout=timedelta(minutes=5))\nasync def spawn(input: ParentInput, ctx: Context) -> dict[str, list[dict[str, Any]]]:\n # 👀 Create each workflow run to spawn\n child_workflow_runs = [\n bulk_child_wf.create_bulk_run_item(\n input=ChildInput(a=str(i)),\n key=f\'child{i}\',\n options=TriggerWorkflowOptions(additional_metadata={\'hello\': \'earth\'}),\n )\n for i in range(input.n)\n ]\n\n # 👀 Run workflows in bulk to improve performance\n spawn_results = await bulk_child_wf.aio_run_many(child_workflow_runs)\n\n return {\'results\': spawn_results}\n\n\n\n\n@bulk_child_wf.task()\ndef process(input: ChildInput, ctx: Context) -> dict[str, str]:\n print(f\'child process {input.a}\')\n return {\'status\': \'success \' + input.a}\n\n\n@bulk_child_wf.task()\ndef process2(input: ChildInput, ctx: Context) -> dict[str, str]:\n print(\'child process2\')\n return {\'status2\': \'success\'}\n\n\ndef main() -> None:\n worker = hatchet.worker(\n \'fanout-worker\', slots=40, workflows=[bulk_parent_wf, bulk_child_wf]\n )\n worker.start()\n\n\nif __name__ == \'__main__\':\n main()\n',
|
||||
'source': 'out/python/bulk_fanout/worker.py',
|
||||
'blocks': {
|
||||
'bulkfanoutparent': {
|
||||
'start': 25,
|
||||
'stop': 42
|
||||
"language": "python",
|
||||
"content": "from datetime import timedelta\nfrom typing import Any\n\nfrom pydantic import BaseModel\n\nfrom hatchet_sdk import Context, Hatchet\nfrom hatchet_sdk.clients.admin import TriggerWorkflowOptions\n\nhatchet = Hatchet(debug=True)\n\n\nclass ParentInput(BaseModel):\n n: int = 100\n\n\nclass ChildInput(BaseModel):\n a: str\n\n\nbulk_parent_wf = hatchet.workflow(name=\"BulkFanoutParent\", input_validator=ParentInput)\nbulk_child_wf = hatchet.workflow(name=\"BulkFanoutChild\", input_validator=ChildInput)\n\n\n# > BulkFanoutParent\n@bulk_parent_wf.task(execution_timeout=timedelta(minutes=5))\nasync def spawn(input: ParentInput, ctx: Context) -> dict[str, list[dict[str, Any]]]:\n # 👀 Create each workflow run to spawn\n child_workflow_runs = [\n bulk_child_wf.create_bulk_run_item(\n input=ChildInput(a=str(i)),\n key=f\"child{i}\",\n options=TriggerWorkflowOptions(additional_metadata={\"hello\": \"earth\"}),\n )\n for i in range(input.n)\n ]\n\n # 👀 Run workflows in bulk to improve performance\n spawn_results = await bulk_child_wf.aio_run_many(child_workflow_runs)\n\n return {\"results\": spawn_results}\n\n\n\n\n@bulk_child_wf.task()\ndef process(input: ChildInput, ctx: Context) -> dict[str, str]:\n print(f\"child process {input.a}\")\n return {\"status\": \"success \" + input.a}\n\n\n@bulk_child_wf.task()\ndef process2(input: ChildInput, ctx: Context) -> dict[str, str]:\n print(\"child process2\")\n return {\"status2\": \"success\"}\n\n\ndef main() -> None:\n worker = hatchet.worker(\n \"fanout-worker\", slots=40, workflows=[bulk_parent_wf, bulk_child_wf]\n )\n worker.start()\n\n\nif __name__ == \"__main__\":\n main()\n",
|
||||
"source": "out/python/bulk_fanout/worker.py",
|
||||
"blocks": {
|
||||
"bulkfanoutparent": {
|
||||
"start": 25,
|
||||
"stop": 42
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': '# > Setup\n\nfrom datetime import datetime, timedelta\n\nfrom hatchet_sdk import BulkCancelReplayOpts, Hatchet, RunFilter, V1TaskStatus\n\nhatchet = Hatchet()\n\nworkflows = hatchet.workflows.list()\n\nassert workflows.rows\n\nworkflow = workflows.rows[0]\n\n\n# > List runs\nworkflow_runs = hatchet.runs.list(workflow_ids=[workflow.metadata.id])\n\n# > Cancel by run ids\nworkflow_run_ids = [workflow_run.metadata.id for workflow_run in workflow_runs.rows]\n\nbulk_cancel_by_ids = BulkCancelReplayOpts(ids=workflow_run_ids)\n\nhatchet.runs.bulk_cancel(bulk_cancel_by_ids)\n\n# > Cancel by filters\n\nbulk_cancel_by_filters = BulkCancelReplayOpts(\n filters=RunFilter(\n since=datetime.today() - timedelta(days=1),\n until=datetime.now(),\n statuses=[V1TaskStatus.RUNNING],\n workflow_ids=[workflow.metadata.id],\n additional_metadata={\'key\': \'value\'},\n )\n)\n\nhatchet.runs.bulk_cancel(bulk_cancel_by_filters)\n',
|
||||
'source': 'out/python/bulk_operations/cancel.py',
|
||||
'blocks': {
|
||||
'setup': {
|
||||
'start': 2,
|
||||
'stop': 14
|
||||
"language": "python",
|
||||
"content": "# > Setup\n\nfrom datetime import datetime, timedelta\n\nfrom hatchet_sdk import BulkCancelReplayOpts, Hatchet, RunFilter, V1TaskStatus\n\nhatchet = Hatchet()\n\nworkflows = hatchet.workflows.list()\n\nassert workflows.rows\n\nworkflow = workflows.rows[0]\n\n\n# > List runs\nworkflow_runs = hatchet.runs.list(workflow_ids=[workflow.metadata.id])\n\n# > Cancel by run ids\nworkflow_run_ids = [workflow_run.metadata.id for workflow_run in workflow_runs.rows]\n\nbulk_cancel_by_ids = BulkCancelReplayOpts(ids=workflow_run_ids)\n\nhatchet.runs.bulk_cancel(bulk_cancel_by_ids)\n\n# > Cancel by filters\n\nbulk_cancel_by_filters = BulkCancelReplayOpts(\n filters=RunFilter(\n since=datetime.today() - timedelta(days=1),\n until=datetime.now(),\n statuses=[V1TaskStatus.RUNNING],\n workflow_ids=[workflow.metadata.id],\n additional_metadata={\"key\": \"value\"},\n )\n)\n\nhatchet.runs.bulk_cancel(bulk_cancel_by_filters)\n",
|
||||
"source": "out/python/bulk_operations/cancel.py",
|
||||
"blocks": {
|
||||
"setup": {
|
||||
"start": 2,
|
||||
"stop": 14
|
||||
},
|
||||
'list_runs': {
|
||||
'start': 17,
|
||||
'stop': 17
|
||||
"list_runs": {
|
||||
"start": 17,
|
||||
"stop": 17
|
||||
},
|
||||
'cancel_by_run_ids': {
|
||||
'start': 20,
|
||||
'stop': 24
|
||||
"cancel_by_run_ids": {
|
||||
"start": 20,
|
||||
"stop": 24
|
||||
},
|
||||
'cancel_by_filters': {
|
||||
'start': 27,
|
||||
'stop': 38
|
||||
"cancel_by_filters": {
|
||||
"start": 27,
|
||||
"stop": 38
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': '# > Setup\n\nfrom datetime import datetime, timedelta\n\nfrom hatchet_sdk import BulkCancelReplayOpts, Hatchet, RunFilter, V1TaskStatus\n\nhatchet = Hatchet()\n\nworkflows = hatchet.workflows.list()\n\nassert workflows.rows\n\nworkflow = workflows.rows[0]\n\n\n# > List runs\nworkflow_runs = hatchet.runs.list(workflow_ids=[workflow.metadata.id])\n\n# > Replay by run ids\nworkflow_run_ids = [workflow_run.metadata.id for workflow_run in workflow_runs.rows]\n\nbulk_replay_by_ids = BulkCancelReplayOpts(ids=workflow_run_ids)\n\nhatchet.runs.bulk_replay(bulk_replay_by_ids)\n\n# > Replay by filters\nbulk_replay_by_filters = BulkCancelReplayOpts(\n filters=RunFilter(\n since=datetime.today() - timedelta(days=1),\n until=datetime.now(),\n statuses=[V1TaskStatus.RUNNING],\n workflow_ids=[workflow.metadata.id],\n additional_metadata={\'key\': \'value\'},\n )\n)\n\nhatchet.runs.bulk_replay(bulk_replay_by_filters)\n',
|
||||
'source': 'out/python/bulk_operations/replay.py',
|
||||
'blocks': {
|
||||
'setup': {
|
||||
'start': 2,
|
||||
'stop': 14
|
||||
"language": "python",
|
||||
"content": "# > Setup\n\nfrom datetime import datetime, timedelta\n\nfrom hatchet_sdk import BulkCancelReplayOpts, Hatchet, RunFilter, V1TaskStatus\n\nhatchet = Hatchet()\n\nworkflows = hatchet.workflows.list()\n\nassert workflows.rows\n\nworkflow = workflows.rows[0]\n\n\n# > List runs\nworkflow_runs = hatchet.runs.list(workflow_ids=[workflow.metadata.id])\n\n# > Replay by run ids\nworkflow_run_ids = [workflow_run.metadata.id for workflow_run in workflow_runs.rows]\n\nbulk_replay_by_ids = BulkCancelReplayOpts(ids=workflow_run_ids)\n\nhatchet.runs.bulk_replay(bulk_replay_by_ids)\n\n# > Replay by filters\nbulk_replay_by_filters = BulkCancelReplayOpts(\n filters=RunFilter(\n since=datetime.today() - timedelta(days=1),\n until=datetime.now(),\n statuses=[V1TaskStatus.RUNNING],\n workflow_ids=[workflow.metadata.id],\n additional_metadata={\"key\": \"value\"},\n )\n)\n\nhatchet.runs.bulk_replay(bulk_replay_by_filters)\n",
|
||||
"source": "out/python/bulk_operations/replay.py",
|
||||
"blocks": {
|
||||
"setup": {
|
||||
"start": 2,
|
||||
"stop": 14
|
||||
},
|
||||
'list_runs': {
|
||||
'start': 17,
|
||||
'stop': 17
|
||||
"list_runs": {
|
||||
"start": 17,
|
||||
"stop": 17
|
||||
},
|
||||
'replay_by_run_ids': {
|
||||
'start': 20,
|
||||
'stop': 24
|
||||
"replay_by_run_ids": {
|
||||
"start": 20,
|
||||
"stop": 24
|
||||
},
|
||||
'replay_by_filters': {
|
||||
'start': 27,
|
||||
'stop': 37
|
||||
"replay_by_filters": {
|
||||
"start": 27,
|
||||
"stop": 37
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import asyncio\n\nimport pytest\n\nfrom examples.cancellation.worker import cancellation_workflow\nfrom hatchet_sdk import Hatchet\nfrom hatchet_sdk.clients.rest.models.v1_task_status import V1TaskStatus\n\n\n@pytest.mark.asyncio(loop_scope=\'session\')\nasync def test_cancellation(hatchet: Hatchet) -> None:\n ref = await cancellation_workflow.aio_run_no_wait()\n\n \'\'\'Sleep for a long time since we only need cancellation to happen _eventually_\'\'\'\n await asyncio.sleep(10)\n\n for i in range(30):\n run = await hatchet.runs.aio_get(ref.workflow_run_id)\n\n if run.run.status == V1TaskStatus.RUNNING:\n await asyncio.sleep(1)\n continue\n\n assert run.run.status == V1TaskStatus.CANCELLED\n assert not run.run.output\n\n break\n else:\n assert False, \'Workflow run did not cancel in time\'\n',
|
||||
'source': 'out/python/cancellation/test_cancellation.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "import asyncio\n\nimport pytest\n\nfrom examples.cancellation.worker import cancellation_workflow\nfrom hatchet_sdk import Hatchet\nfrom hatchet_sdk.clients.rest.models.v1_task_status import V1TaskStatus\n\n\n@pytest.mark.asyncio(loop_scope=\"session\")\nasync def test_cancellation(hatchet: Hatchet) -> None:\n ref = await cancellation_workflow.aio_run_no_wait()\n\n \"\"\"Sleep for a long time since we only need cancellation to happen _eventually_\"\"\"\n await asyncio.sleep(10)\n\n for i in range(30):\n run = await hatchet.runs.aio_get(ref.workflow_run_id)\n\n if run.run.status == V1TaskStatus.RUNNING:\n await asyncio.sleep(1)\n continue\n\n assert run.run.status == V1TaskStatus.CANCELLED\n assert not run.run.output\n\n break\n else:\n assert False, \"Workflow run did not cancel in time\"\n",
|
||||
"source": "out/python/cancellation/test_cancellation.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import time\n\nfrom examples.cancellation.worker import cancellation_workflow, hatchet\n\nid = cancellation_workflow.run_no_wait()\n\ntime.sleep(5)\n\nhatchet.runs.cancel(id.workflow_run_id)\n',
|
||||
'source': 'out/python/cancellation/trigger.py',
|
||||
'blocks': {},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"language": "python",
|
||||
"content": "import time\n\nfrom examples.cancellation.worker import cancellation_workflow, hatchet\n\nid = cancellation_workflow.run_no_wait()\n\ntime.sleep(5)\n\nhatchet.runs.cancel(id.workflow_run_id)\n",
|
||||
"source": "out/python/cancellation/trigger.py",
|
||||
"blocks": {},
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import asyncio\nimport time\n\nfrom hatchet_sdk import Context, EmptyModel, Hatchet\n\nhatchet = Hatchet(debug=True)\n\ncancellation_workflow = hatchet.workflow(name=\'CancelWorkflow\')\n\n\n# > Self-cancelling task\n@cancellation_workflow.task()\nasync def self_cancel(input: EmptyModel, ctx: Context) -> dict[str, str]:\n await asyncio.sleep(2)\n\n ## Cancel the task\n await ctx.aio_cancel()\n\n await asyncio.sleep(10)\n\n return {\'error\': \'Task should have been cancelled\'}\n\n\n\n\n# > Checking exit flag\n@cancellation_workflow.task()\ndef check_flag(input: EmptyModel, ctx: Context) -> dict[str, str]:\n for i in range(3):\n time.sleep(1)\n\n # Note: Checking the status of the exit flag is mostly useful for cancelling\n # sync tasks without needing to forcibly kill the thread they\'re running on.\n if ctx.exit_flag:\n print(\'Task has been cancelled\')\n raise ValueError(\'Task has been cancelled\')\n\n return {\'error\': \'Task should have been cancelled\'}\n\n\n\n\ndef main() -> None:\n worker = hatchet.worker(\'cancellation-worker\', workflows=[cancellation_workflow])\n worker.start()\n\n\nif __name__ == \'__main__\':\n main()\n',
|
||||
'source': 'out/python/cancellation/worker.py',
|
||||
'blocks': {
|
||||
'self_cancelling_task': {
|
||||
'start': 12,
|
||||
'stop': 23
|
||||
"language": "python",
|
||||
"content": "import asyncio\nimport time\n\nfrom hatchet_sdk import Context, EmptyModel, Hatchet\n\nhatchet = Hatchet(debug=True)\n\ncancellation_workflow = hatchet.workflow(name=\"CancelWorkflow\")\n\n\n# > Self-cancelling task\n@cancellation_workflow.task()\nasync def self_cancel(input: EmptyModel, ctx: Context) -> dict[str, str]:\n await asyncio.sleep(2)\n\n ## Cancel the task\n await ctx.aio_cancel()\n\n await asyncio.sleep(10)\n\n return {\"error\": \"Task should have been cancelled\"}\n\n\n\n\n# > Checking exit flag\n@cancellation_workflow.task()\ndef check_flag(input: EmptyModel, ctx: Context) -> dict[str, str]:\n for i in range(3):\n time.sleep(1)\n\n # Note: Checking the status of the exit flag is mostly useful for cancelling\n # sync tasks without needing to forcibly kill the thread they're running on.\n if ctx.exit_flag:\n print(\"Task has been cancelled\")\n raise ValueError(\"Task has been cancelled\")\n\n return {\"error\": \"Task should have been cancelled\"}\n\n\n\n\ndef main() -> None:\n worker = hatchet.worker(\"cancellation-worker\", workflows=[cancellation_workflow])\n worker.start()\n\n\nif __name__ == \"__main__\":\n main()\n",
|
||||
"source": "out/python/cancellation/worker.py",
|
||||
"blocks": {
|
||||
"self_cancelling_task": {
|
||||
"start": 12,
|
||||
"stop": 23
|
||||
},
|
||||
'checking_exit_flag': {
|
||||
'start': 27,
|
||||
'stop': 40
|
||||
"checking_exit_flag": {
|
||||
"start": 27,
|
||||
"stop": 40
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Snippet } from '@/lib/generated/snips/types';
|
||||
|
||||
const snippet: Snippet = {
|
||||
'language': 'python',
|
||||
'content': 'import asyncio\n\n# > Running a Task\nfrom examples.child.worker import SimpleInput, child_task\n\nchild_task.run(SimpleInput(message=\'Hello, World!\'))\n\n\nasync def main() -> None:\n # > Bulk Run a Task\n greetings = [\'Hello, World!\', \'Hello, Moon!\', \'Hello, Mars!\']\n\n results = await child_task.aio_run_many(\n [\n # run each greeting as a task in parallel\n child_task.create_bulk_run_item(\n input=SimpleInput(message=greeting),\n )\n for greeting in greetings\n ]\n )\n\n # this will await all results and return a list of results\n print(results)\n\n # > Running Multiple Tasks\n result1 = child_task.aio_run(SimpleInput(message=\'Hello, World!\'))\n result2 = child_task.aio_run(SimpleInput(message=\'Hello, Moon!\'))\n\n # gather the results of the two tasks\n gather_results = await asyncio.gather(result1, result2)\n\n # print the results of the two tasks\n print(gather_results[0][\'transformed_message\'])\n print(gather_results[1][\'transformed_message\'])\n',
|
||||
'source': 'out/python/child/bulk.py',
|
||||
'blocks': {
|
||||
'running_a_task': {
|
||||
'start': 4,
|
||||
'stop': 6
|
||||
"language": "python",
|
||||
"content": "import asyncio\n\n# > Running a Task\nfrom examples.child.worker import SimpleInput, child_task\n\nchild_task.run(SimpleInput(message=\"Hello, World!\"))\n\n\nasync def main() -> None:\n # > Bulk Run a Task\n greetings = [\"Hello, World!\", \"Hello, Moon!\", \"Hello, Mars!\"]\n\n results = await child_task.aio_run_many(\n [\n # run each greeting as a task in parallel\n child_task.create_bulk_run_item(\n input=SimpleInput(message=greeting),\n )\n for greeting in greetings\n ]\n )\n\n # this will await all results and return a list of results\n print(results)\n\n # > Running Multiple Tasks\n result1 = child_task.aio_run(SimpleInput(message=\"Hello, World!\"))\n result2 = child_task.aio_run(SimpleInput(message=\"Hello, Moon!\"))\n\n # gather the results of the two tasks\n gather_results = await asyncio.gather(result1, result2)\n\n # print the results of the two tasks\n print(gather_results[0][\"transformed_message\"])\n print(gather_results[1][\"transformed_message\"])\n",
|
||||
"source": "out/python/child/bulk.py",
|
||||
"blocks": {
|
||||
"running_a_task": {
|
||||
"start": 4,
|
||||
"stop": 6
|
||||
},
|
||||
'bulk_run_a_task': {
|
||||
'start': 11,
|
||||
'stop': 24
|
||||
"bulk_run_a_task": {
|
||||
"start": 11,
|
||||
"stop": 24
|
||||
},
|
||||
'running_multiple_tasks': {
|
||||
'start': 27,
|
||||
'stop': 35
|
||||
"running_multiple_tasks": {
|
||||
"start": 27,
|
||||
"stop": 35
|
||||
}
|
||||
},
|
||||
'highlights': {}
|
||||
}; // Then replace double quotes with single quotes
|
||||
"highlights": {}
|
||||
};
|
||||
|
||||
export default snippet;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user