-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtasksubmitter.go
68 lines (57 loc) · 1.72 KB
/
tasksubmitter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"net/http"
)
const RESTATE_URL = "http://localhost:8080"
type TaskOpts struct {
Id string `json:"id"`
Description string `json:"description"`
}
func SubmitAndAwaitTask(task TaskOpts) error {
idempotencyKey := task.Id
slog.Info("Submitting task with idempotency key: " + idempotencyKey)
client := &http.Client{}
// submit the task; similar to publishing a message to a queue (by adding /send to the url)
// Restate ensures the task is executed exactly once
// Optionally set a delay for the task by adding `?delay=10s` to the URL
url := fmt.Sprintf("%s/AsyncTaskWorker/RunTask/Send", RESTATE_URL)
taskData, _ := json.Marshal(task)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(taskData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
// use a stable uuid as an idempotency key; Restate deduplicates for us
req.Header.Set("idempotency-key", idempotencyKey)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// ... do other things while the task is being processed ...
// Later on, you can retrieve the result of the task (possibly in a different process)
attachUrl := fmt.Sprintf("%s/restate/invocation/AsyncTaskWorker/RunTask/%s/attach", RESTATE_URL, idempotencyKey)
resp, err = http.DefaultClient.Get(attachUrl)
if err != nil {
return err
}
defer resp.Body.Close()
// ... Process the result ...
return nil
}
func main() {
task := TaskOpts{
Id: "task1",
Description: "some heavy work",
}
err := SubmitAndAwaitTask(task)
if err != nil {
slog.Error("Task submission failed", "err", err.Error())
return
}
slog.Info("Task submitted successfully")
}