-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 4107 first use case template #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,36 @@ | ||
| # hatchet-quickstarts | ||
|
|
||
| Quickstart templates and generated examples for Hatchet. | ||
| This repository owns the `hatchet quickstart` CLI templates and the generated | ||
| examples. | ||
|
|
||
| This repository will own the `hatchet quickstart` CLI template source, generated | ||
| cloneable examples, dependency automation, and template validation. | ||
| ## Template layout | ||
|
|
||
| Templates live under `templates/`, are embedded into the module, and are | ||
| returned by `TemplatesFS()`. The Hatchet CLI templater reads fixed paths from | ||
| this tree, so the layout is a contract rather than an internal detail. | ||
|
|
||
| The default simple quickstart lives at the language root: | ||
|
|
||
| - `templates/go/` for Go. | ||
| - `templates/<language>/shared/` plus `templates/<language>/<package-manager>/` | ||
| for Python and TypeScript, where the package-manager directory is overlaid on | ||
| shared. | ||
|
|
||
| The main repo templater hardcodes these `templates/<language>/...` paths, so the | ||
| default templates must not move while released CLI versions depend on them. | ||
| Use-case templates therefore go under a separate subtree that mirrors the same | ||
| structure one level down: | ||
|
|
||
| - `templates/use-cases/<use-case>/go/` | ||
| - `templates/use-cases/<use-case>/<language>/shared/` plus | ||
| `templates/use-cases/<use-case>/<language>/<package-manager>/` | ||
|
|
||
| This keeps the default quickstart untouched, avoids collisions with language | ||
| directory names, and lets a future `--use-case` path builder reuse the existing | ||
| overlay of shared and package-manager directories with a different root. | ||
|
|
||
| The first use case is `scheduled`, a Go template at | ||
| `templates/use-cases/scheduled/go/`, with a generated example at | ||
| `examples/use-cases/scheduled/go/`. It registers a cron schedule with the Go | ||
| SDK `WithWorkflowCron` option. The `--use-case` flag itself lives in the main | ||
| Hatchet CLI and is not part of this repo yet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| HATCHET_CLIENT_TOKEN= | ||
| HATCHET_CLIENT_TLS_STRATEGY=none |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| certs | ||
| config | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| FROM golang:1.26-alpine AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Copy go mod files | ||
| COPY go.mod go.sum ./ | ||
|
|
||
| # Download dependencies | ||
| RUN go mod download | ||
|
|
||
| # Copy source code | ||
| COPY . . | ||
|
|
||
| # Build the application | ||
| RUN go build -o worker ./cmd/worker | ||
|
|
||
| FROM alpine:latest | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Copy the binary from builder | ||
| COPY --from=builder /app/worker . | ||
|
|
||
| CMD ["./worker"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Hatchet Scheduled Workflow Example | ||
|
|
||
| This is an example project demonstrating a scheduled Hatchet workflow in Go. The | ||
| worker registers the task with a cron schedule using `WithWorkflowCron`, so it | ||
| runs every five minutes. You can also run it on demand. For detailed setup | ||
| instructions, see the [Hatchet Setup Guide](https://docs.hatchet.run/home/setup). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before running this project, make sure you have the following: | ||
|
|
||
| 1. [Go v1.22 or higher](https://go.dev/doc/install) | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Clone the repository: | ||
|
|
||
| ```bash | ||
| git clone https://github.com/hatchet-dev/hatchet-go-quickstart.git | ||
| cd hatchet-go-quickstart | ||
| ``` | ||
|
|
||
| 2. Set the required environment variable `HATCHET_CLIENT_TOKEN` created in the [Getting Started Guide](https://docs.hatchet.run/home/hatchet-cloud-quickstart). | ||
|
|
||
| ```bash | ||
| export HATCHET_CLIENT_TOKEN=<token> | ||
| ``` | ||
|
|
||
| > Note: If you're self hosting you may need to set `HATCHET_CLIENT_TLS_STRATEGY=none` to disable TLS | ||
|
|
||
| You can also use a `.env` file to load these environment variables. A sample exists in the form of a `.env.sample`. | ||
|
|
||
| 3. Install the project dependencies: | ||
|
|
||
| ```bash | ||
| go mod tidy | ||
| ``` | ||
|
|
||
| ### Running an example | ||
|
|
||
| 1. Start a Hatchet worker: | ||
|
|
||
| ```bash | ||
| go run cmd/worker/main.go | ||
| ``` | ||
|
|
||
| The worker runs the task on its cron schedule while it is connected. | ||
|
|
||
| 2. To run the task on demand, in a new terminal: | ||
|
|
||
| ```bash | ||
| go run cmd/run/main.go | ||
| ``` | ||
|
|
||
| This triggers the task on the worker running in the first terminal and prints the output to the second terminal. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
|
|
||
| hatchet "github.com/hatchet-dev/hatchet/sdks/go" | ||
| "github.com/joho/godotenv" | ||
| ) | ||
|
|
||
| func HatchetClient() (*hatchet.Client, error) { | ||
| if _, err := os.Stat(".env"); os.IsExist(err) { | ||
| err := godotenv.Load() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| // check for HATCHET_CLIENT_TOKEN | ||
| token := os.Getenv("HATCHET_CLIENT_TOKEN") | ||
| if token == "" { | ||
| return nil, errors.New("HATCHET_CLIENT_TOKEN is not set") | ||
| } | ||
|
|
||
| return hatchet.NewClient() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/hatchet-dev/hatchet-go-quickstart/client" | ||
| "github.com/hatchet-dev/hatchet-go-quickstart/workflows" | ||
| ) | ||
|
|
||
| func main() { | ||
| c, err := client.HatchetClient() | ||
| if err != nil { | ||
| log.Fatalf("Failed to create Hatchet client: %v", err) | ||
| } | ||
|
|
||
| scheduled := workflows.ScheduledWorkflow(c) | ||
|
|
||
| result, err := scheduled.Run(context.Background(), workflows.ScheduledInput{ | ||
| Message: "hello from a manual run", | ||
| }) | ||
| if err != nil { | ||
| log.Fatalf("Failed to run Hatchet task: %v", err) | ||
| } | ||
|
|
||
| var scheduledResult workflows.ScheduledOutput | ||
|
|
||
| err = result.Into(&scheduledResult) | ||
| if err != nil { | ||
| log.Fatalf("Failed to convert result to ScheduledOutput: %v", err) | ||
| } | ||
|
|
||
| fmt.Println(scheduledResult.Message) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
|
|
||
| "github.com/hatchet-dev/hatchet-go-quickstart/client" | ||
| "github.com/hatchet-dev/hatchet-go-quickstart/workflows" | ||
| "github.com/hatchet-dev/hatchet/pkg/cmdutils" | ||
| hatchet "github.com/hatchet-dev/hatchet/sdks/go" | ||
| ) | ||
|
|
||
| func main() { | ||
| c, err := client.HatchetClient() | ||
| if err != nil { | ||
| log.Fatalf("Failed to create Hatchet client: %v", err) | ||
| } | ||
|
|
||
| worker, err := c.NewWorker( | ||
| "scheduled-worker", | ||
| hatchet.WithWorkflows(workflows.ScheduledWorkflow(c)), | ||
| ) | ||
| if err != nil { | ||
| log.Fatalf("Failed to create Hatchet worker: %v", err) | ||
| } | ||
|
|
||
| // we construct an interrupt context to handle Ctrl+C | ||
| // you can pass in your own context.Context here to the worker | ||
| interruptCtx, cancel := cmdutils.NewInterruptContext() | ||
|
|
||
| defer cancel() | ||
|
|
||
| err = worker.StartBlocking(interruptCtx) | ||
| if err != nil { | ||
| log.Fatalf("Failed to start Hatchet worker: %v", err) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.