Skip to content
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

feat: add initial docs site #14

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 18 additions & 272 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,293 +1,39 @@
[![slack](https://img.shields.io/badge/Join%20Our%20Community-Slack-blue)](https://join.slack.com/t/hatchet-co/signup) [![License: MIT](https://img.shields.io/badge/License-MIT-purple.svg)](https://opensource.org/licenses/MIT)

<!-- [![Go Reference](https://pkg.go.dev/badge/github.com/hatchet-dev/hatchet.svg)](https://pkg.go.dev/github.com/hatchet-dev/hatchet) -->
[![Discord](https://img.shields.io/discord/1088927970518909068?style=social&logo=discord)](https://discord.gg/ZMeUafwH89) [![License: MIT](https://img.shields.io/badge/License-MIT-purple.svg)](https://opensource.org/licenses/MIT) [![Go Reference](https://pkg.go.dev/badge/github.com/hatchet-dev/hatchet.svg)](https://pkg.go.dev/github.com/hatchet-dev/hatchet)

## Introduction

_**Note:** Hatchet is in early development. Changes are not guaranteed to be backwards-compatible. If you'd like to run Hatchet in production, feel free to reach out on Slack for tips._
_**Note:** Hatchet is in early development. Changes are not guaranteed to be backwards-compatible. If you'd like to run Hatchet in production, feel free to reach out on Discord for tips._

Hatchet is an event storage API and workflow engine for distributed applications. Using Hatchet, you can create workers which process a set of background tasks based on different triggers, like events created within your system or a cron schedule.
Hatchet is a self-hostable workflow engine built for application developers.

As a simple example, let's say you want to perform 3 actions when a user has signed up for your app:
**What is a workflow?**

1. Initialize a set of resources for the user (perhaps a sandbox environment for testing).
2. Send the user an automated greeting over email
3. Add the user to a newsletter campaign
The term `workflow` tends to be overloaded, so let's make things more clear - in Hatchet, a workflow is a set of functions which are executed in response to an external trigger (an event, schedule, or API call). For example, if you'd like to send notifications to a user after they've signed up, you could create a workflow for that.

With Hatchet, this would look something like the following:
**Why is that useful?**

```yaml
name: "post-user-sign-up"
version: v0.2.0
triggers:
events:
- user:create
jobs:
create-resources:
steps:
- id: createSandbox
action: sandbox:create
timeout: 60s
greet-user:
steps:
- id: greetUser
action: postmark:email-from-template
timeout: 15s
with:
firstName: "{{ .user.firstName }}"
email: "{{ .user.email }}"
add-to-newsletter:
steps:
- id: addUserToNewsletter
action: newsletter:add-user
timeout: 15s
with:
email: "{{ .user.email }}"
```
Instead of processing background tasks and functions in your application handlers, which can lead to complex code, hard-to-debug errors, and resource contention, you can distribute these workflows between a set of `workers`. Workers are long-running processes which listen for events, and execute the functions defined in your workflows.

In your codebase, you would then create a worker which could perform the following actions:
**What is a workflow engine?**

- `sandbox:create` responsible for creating/tearing down a sandbox environment
- `postmark:email-from-template` for sending an email from a template
- `newsletter:add-user` for adding a user to a newsletter campaign
A workflow engine orchestrates the execution of workflows. It schedules workflows on workers, retries failed workflows, and provides integrations for monitoring and debugging workflows.

Ultimately, the goal of Hatchet workflows are that you don't need to write these actions yourself -- creating a robust set of prebuilt integrations is one of the goals of the project.
## Project Goals

### Why is this useful?
Hatchet has the following high-level goals:

- When deploying a workflow engine or task queue, one of the first breaking points is requiring a robust event architecture for monitoring and replaying events. Hatchet provides this out of the box, allowing you to replay your events and retrigger your workflows.
- No need to build all of your plumbing logic (action 1 -> event 1 -> action 2 -> event 2). Just define your jobs and steps and write your business logic. This is particularly useful the more complex your workflows become.
- Using prebuilt integrations with a standard interface makes building auxiliary services like notification systems, billing, backups, and auditing much easier. **Please file an issue if you'd like to see an integration supported.** The following are on the roadmap:
- Email providers: Sendgrid, Postmark, AWS SES
- Stripe
- AWS S3
- Additionally, if you're already familiar with/using a workflow engine, making workflows declarative provides several benefits:
- Makes spec'ing, debugging and visualizing workflows much simpler
- Automatically updates triggers, schedules, and timeouts when they change, rather than doing this through a UI/CLI/SDK
- Makes monitoring easier to build by logically separating units of work - jobs will automatically correspond to `BeginSpan`. OpenTelemetry support is on the roadmap.
1. **Serve application developers:** we aim to support a broad set of languages and frameworks, to make it easier to support your existing applications. We currently support a Go SDK, with more languages coming soon.
2. **Simple to setup:** we've seen too many overengineered stacks built on a fragile task queue with overly complex infrastructure. Hatchet is designed to be simple to setup, run locally, and deploy to your own infrastructure.
3. **Flexibility when you need it:** as your application grows, you can use Hatchet to support complex, multi-step distributed workflows. Hatchet's backend is modular, allowing for customizing the implementation of the event storage API, queueing system, authentication, and more.

## Getting Started

For a set of end-to-end examples, see the [examples](./examples) directory.

### Starting Hatchet

We are working on making it easier to start a Hatchet server. For now, see the [contributing guide](./CONTRIBUTING.md) for starting the Hatchet engine.

### Writing a Workflow

By default, Hatchet searches for workflows in the `.hatchet` folder relative to the directory you run your application in.

There are two main sections of a workflow file:

**`triggers`**

This section specifies what triggers a workflow. This can be events or a crontab-like schedule. For example, the following are valid triggers:

```yaml
triggers:
events:
- event_key_1
- event_key_2
```

```yaml
triggers:
crons:
- "* * * * *"
```

**Jobs**

After defining your triggers, you define a list of jobs to run based on the triggers. **Jobs run in parallel.** Jobs contain the following fields:

```yaml
# ...
jobs:
my-awesome-job:
# (optional) A timeout value for the entire job
timeout: 60s
# (required) A set of steps for the job; see below
steps: []
```

Within each job, there are a set of **steps** which run sequentially. A step can contain the following fields:

```yaml
# (required) the name of the step
name: Step 1
# (required) a unique id for the step (can be referenced by future steps)
id: step-1
# (required) the action id in the form of "integration_id:action".
action: "slack:create-channel"
# (required) the timeout of the individual step
timeout: 15s
# (optional or required, depending on integration) input data to the integration
with:
key: val
```

### Creating a Worker

Workers can be created using:

```go
package main

import (
"context"
"time"

"github.com/hatchet-dev/hatchet/cmd/cmdutils"
"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/worker"
)

type userCreateEvent struct {
Username string `json:"username"`
UserId string `json:"user_id"`
Data map[string]string `json:"data"`
}

type actionInput struct {
Message string `json:"message"`
}

func main() {
client, err := client.New(
client.InitWorkflows(),
)

if err != nil {
panic(err)
}

worker, err := worker.NewWorker(
worker.WithDispatcherClient(
client.Dispatcher(),
),
)

if err != nil {
panic(err)
}

err = worker.RegisterAction("echo:echo", func(ctx context.Context, input *actionInput) (result any, err error) {
return map[string]interface{}{
"message": input.Message,
}, nil
})

if err != nil {
panic(err)
}

interruptCtx, cancel := cmdutils.InterruptContext(cmdutils.InterruptChan())
defer cancel()

worker.Start(interruptCtx)
}
```

You can configure the worker with your own set of workflow files using the `client.WithWorkflowFiles` option.

### Triggering Events

To trigger events from your main application, use the `client.Event().Push` method:

```go
package main

import (
"context"

"github.com/hatchet-dev/hatchet/pkg/client"
)

type userCreateEvent struct {
Username string `json:"username"`
UserId string `json:"user_id"`
Data map[string]string `json:"data"`
}

func main() {
client, err := client.New()

testEvent := userCreateEvent{
Username: "echo-test",
UserId: "1234",
Data: map[string]string{
"test": "test",
},
}

err = client.Event().Push(
context.Background(),
"user:create",
testEvent,
)

if err != nil {
panic(err)
}
}
```

You can configure the dispatcher with your own set of workflow files using the `dispatcher.WithWorkflowFiles` option.

## Why should I care?

**If you're unfamiliar with background task processing**

Many APIs start out without a task processing/worker service. You might not need it, but at a certain level of complexity, you probably will. There are a few use-cases where workers start to make sense:

1. You need to run scheduled tasks which that aren't triggered from your core API. For example, this may be a daily cleanup task, like traversing soft-deleted database entries or backing up data to S3.
2. You need to run tasks which are triggered by API events, but aren't required for the core business logic of the handler. For example, you want to add a user to your CRM after they sign up.

For both of these cases, it's typical to re-use a lot of core functionality from your API, so the most natural place to start is by adding some automation within your API itself; for example, after returning `201 Created`, you might send a greeting to the user, initialize a sandbox environment, send an internal notification that a user signed up, etc, all within your API handlers. Let's say you've handled this case as following:

```go
// Hypothetical handler called via a routing package, let's just pretend it returns an error
func MyHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
// Boilerplate code to parse the request
var newUser User
err := json.NewDecoder(r.Body).Decode(&newUser)
if err != nil {
http.Error(w, "Invalid user data", http.StatusBadRequest)
return err
}

// Validate email and password fields...
// (Add your validation logic here)

// Create a user in the database
user, err := createUser(ctx, newUser.Email, newUser.Password)
if err != nil {
// Handle database errors, such as unique constraint violation
http.Error(w, "Error creating user", http.StatusInternalServerError)
return err
}

// Return 201 created with user type
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)

// Send user a greeting
err := email.SendGreetingEmail(context.Background(), user)

if err != nil {
// can't return an error, since header is already set
fmt.Println(err)
}

// ... other post-signup operations
}
```
To get started, see the Hatchet documentation [here](https://docs.hatchet.run).

At some point, you realize all of these background operations don't really belong in the handler -- when they're part of the handler, they're more difficult to monitor and observe, difficult to retry (especially if a third-party service goes down), and bloat your handlers (which could cause goroutine leakage or memory issues).
## Github Issues

This is where a service like Hatchet suited for background/task processing comes in.
Please submit any bugs that you encounter via Github issues. However, please reach out on [Discord](https://discord.gg/ZMeUafwH89) before submitting a feature request - as the project is very early, we'd like to build a solid foundation before adding more complex features.

## I'd Like to Contribute

Hatchet is still in very early development -- as a result, there are very few development docs. However, please feel free to reach out on the #contributing channel on [Slack](https://join.slack.com/t/hatchet-co/signup) to shape the direction of the project.
See the contributing docs [here](./CONTRIBUTING.md), and please let us know what you're interesting in working on in the #contributing channel on [Discord](https://discord.gg/ZMeUafwH89). This will help us shape the direction of the project and will make collaboration much easier!
2 changes: 2 additions & 0 deletions frontend/docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.next
node_modules
21 changes: 21 additions & 0 deletions frontend/docs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Shu Ding

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions frontend/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Nextra Docs Template

This is a template for creating documentation with [Nextra](https://nextra.site).

[**Live Demo →**](https://nextra-docs-template.vercel.app)

[![](.github/screenshot.png)](https://nextra-docs-template.vercel.app)

## Quick Start

Click the button to clone this repository and deploy it on Vercel:

[![](https://vercel.com/button)](https://vercel.com/new/clone?s=https%3A%2F%2Fgit.luolix.top%2Fshuding%2Fnextra-docs-template&showOptionalTeamCreation=false)

## Local Development

First, run `pnpm i` to install the dependencies.

Then, run `pnpm dev` to start the development server and visit localhost:3000.

## License

This project is licensed under the MIT License.
6 changes: 6 additions & 0 deletions frontend/docs/components/counters.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.counter {
border: 1px solid #ccc;
border-radius: 5px;
padding: 2px 6px;
margin: 12px 0 0;
}
24 changes: 24 additions & 0 deletions frontend/docs/components/counters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Example from https://beta.reactjs.org/learn

import { useState } from 'react'
import styles from './counters.module.css'

function MyButton() {
const [count, setCount] = useState(0)

function handleClick() {
setCount(count + 1)
}

return (
<div>
<button onClick={handleClick} className={styles.counter}>
Clicked {count} times
</button>
</div>
)
}

export default function MyApp() {
return <MyButton />
}
5 changes: 5 additions & 0 deletions frontend/docs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
Loading