Skip to content

Commit 635abad

Browse files
committed
snippets
1 parent c8c2e99 commit 635abad

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

README.md

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,64 +42,62 @@ You add durable workflows to your existing Golang program by registering ordinar
4242
package main
4343

4444
import (
45-
"context"
46-
"fmt"
47-
"os"
45+
"context"
46+
"fmt"
47+
"os"
48+
"time"
4849

49-
"github.com/dbos-inc/dbos-transact-golang/dbos"
50+
"github.com/dbos-inc/dbos-transact-golang/dbos"
5051
)
5152

5253
func workflow(dbosCtx dbos.DBOSContext, _ string) (string, error) {
53-
_, err := dbos.RunAsStep(dbosCtx, func(ctx context.Context) (string, error) {
54-
return stepOne(ctx)
55-
})
56-
if err != nil {
57-
return "", err
58-
}
59-
return dbos.RunAsStep(dbosCtx, func(ctx context.Context) (string, error) {
60-
return stepTwo(ctx)
61-
})
54+
_, err := dbos.RunAsStep(dbosCtx, stepOne)
55+
if err != nil {
56+
return "", err
57+
}
58+
return dbos.RunAsStep(dbosCtx, stepTwo)
6259
}
6360

6461
func stepOne(ctx context.Context) (string, error) {
65-
fmt.Println("Step one completed!")
66-
return "Step 1 completed", nil
62+
fmt.Println("Step one completed!")
63+
return "Step 1 completed", nil
6764
}
6865

6966
func stepTwo(ctx context.Context) (string, error) {
70-
fmt.Println("Step two completed!")
71-
return "Step 2 completed - Workflow finished successfully", nil
67+
fmt.Println("Step two completed!")
68+
return "Step 2 completed - Workflow finished successfully", nil
7269
}
70+
7371
func main() {
7472
// Initialize a DBOS context
75-
ctx, err := dbos.NewDBOSContext(context.Background(), dbos.Config{
76-
DatabaseURL: os.Getenv("DBOS_SYSTEM_DATABASE_URL"),
77-
AppName: "myapp",
78-
})
79-
if err != nil {
80-
panic(err)
81-
}
73+
ctx, err := dbos.NewDBOSContext(context.Background(), dbos.Config{
74+
DatabaseURL: os.Getenv("DBOS_SYSTEM_DATABASE_URL"),
75+
AppName: "myapp",
76+
})
77+
if err != nil {
78+
panic(err)
79+
}
8280

8381
// Register a workflow
84-
dbos.RegisterWorkflow(ctx, workflow)
82+
dbos.RegisterWorkflow(ctx, workflow)
8583

8684
// Launch DBOS
87-
err = ctx.Launch()
88-
if err != nil {
89-
panic(err)
90-
}
91-
defer ctx.Cancel()
85+
err = ctx.Launch()
86+
if err != nil {
87+
panic(err)
88+
}
89+
defer ctx.Shutdown(2 * time.Second)
9290

9391
// Run a durable workflow and get its result
94-
handle, err := dbos.RunWorkflow(ctx, workflow, "")
95-
if err != nil {
96-
panic(err)
97-
}
98-
res, err := handle.GetResult()
99-
if err != nil {
100-
panic(err)
101-
}
102-
fmt.Println("Workflow result:", res)
92+
handle, err := dbos.RunWorkflow(ctx, workflow, "")
93+
if err != nil {
94+
panic(err)
95+
}
96+
res, err := handle.GetResult()
97+
if err != nil {
98+
panic(err)
99+
}
100+
fmt.Println("Workflow result:", res)
103101
}
104102
```
105103

@@ -130,6 +128,7 @@ They don't require a separate queueing service or message broker—just Post
130128
package main
131129

132130
import (
131+
"context"
133132
"fmt"
134133
"os"
135134
"time"
@@ -138,7 +137,7 @@ import (
138137
)
139138

140139
func task(ctx dbos.DBOSContext, i int) (int, error) {
141-
ctx.Sleep(5 * time.Second)
140+
dbos.Sleep(ctx, 5*time.Second)
142141
fmt.Printf("Task %d completed\n", i)
143142
return i, nil
144143
}
@@ -162,7 +161,7 @@ func main() {
162161
if err != nil {
163162
panic(err)
164163
}
165-
defer ctx.Cancel()
164+
defer ctx.Shutdown(2 * time.Second)
166165

167166
// Enqueue tasks and gather results
168167
fmt.Println("Enqueuing workflows")
@@ -218,7 +217,7 @@ It stores its wakeup time in Postgres so the workflow sleeps through any interru
218217

219218
```golang
220219
func workflow(ctx dbos.DBOSContext, duration time.Duration) (string, error) {
221-
ctx.Sleep(duration)
220+
dbos.Sleep(ctx, duration)
222221
return fmt.Sprintf("Workflow slept for %s", duration), nil
223222
}
224223

0 commit comments

Comments
 (0)