-
Notifications
You must be signed in to change notification settings - Fork 8
/
core.clj
148 lines (125 loc) · 4.55 KB
/
core.clj
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;; Copyright © Manetu, Inc. All rights reserved
(ns temporal.client.core
"Methods for client interaction with Temporal"
(:require [taoensso.timbre :as log]
[taoensso.nippy :as nippy]
[promesa.core :as p]
[temporal.client.options :as copts]
[temporal.internal.workflow :as w]
[temporal.internal.utils :as u]
[temporal.internal.exceptions :as e])
(:import [java.time Duration]
[io.temporal.client WorkflowClient WorkflowStub]))
(defn create-client
"
Creates a new client instance suitable for implementing Temporal workers (See [[temporal.client.worker/start]]) or
workflow clients (See [[create-workflow]]).
Arguments:
- `options`: Options for configuring the `WorkflowClient` (See [[temporal.client.options/workflow-client-options]] and [[temporal.client.options/stub-options]])
- `timeout`: Connection timeout as a [Duration](https://docs.oracle.com/javase/8/docs/api//java/time/Duration.html) (default: 5s)
"
([] (create-client {}))
([options]
(create-client options (Duration/ofSeconds 5)))
([options timeout]
(let [service (copts/service-stub-> options timeout)]
(WorkflowClient/newInstance service (copts/workflow-client-options-> options)))))
(defn create-workflow
"
Create a new workflow-stub instance, suitable for managing and interacting with a workflow through it's lifecycle.
*N.B.: The workflow will remain in an uninitialized and idle state until explicitly started with either ([[start]]) or
([[signal-with-start]]).*
```clojure
(defworkflow my-workflow
[ctx args]
...)
(let [w (create client my-workflow {:task-queue ::my-task-queue})]
;; do something with the instance 'w')
```
"
([^WorkflowClient client workflow-id]
(let [stub (.newUntypedWorkflowStub client (u/namify workflow-id))]
(log/trace "create-workflow id:" workflow-id)
{:client client :stub stub}))
([^WorkflowClient client workflow options]
(let [wf-name (w/get-annotated-name workflow)
options (w/wf-options-> options)
stub (.newUntypedWorkflowStub client wf-name options)]
(log/trace "create-workflow:" wf-name options)
{:client client :stub stub})))
(defn start
"
Starts 'worklow' with 'params'"
[{:keys [^WorkflowStub stub] :as workflow} params]
(log/trace "start:" params)
(.start stub (u/->objarray params)))
(defn signal-with-start
"
Signals 'workflow' with 'signal-params' on signal 'signal-name', starting it if not already running. 'wf-params' are
used as workflow start arguments if the workflow needs to be started"
[{:keys [^WorkflowStub stub] :as workflow} signal-name signal-params wf-params]
(log/trace "signal-with-start->" "signal:" signal-name signal-params "workflow-params:" wf-params)
(.signalWithStart stub (u/namify signal-name) (u/->objarray signal-params) (u/->objarray wf-params)))
(defn >!
"
Sends 'params' as a signal 'signal-name' to 'workflow'
```clojure
(>! workflow ::my-signal {:msg \"Hi\"})
```
"
[{:keys [^WorkflowStub stub] :as workflow} signal-name params]
(log/trace ">!" signal-name params)
(.signal stub (u/namify signal-name) (u/->objarray params)))
(defn get-result
"
Retrieves the final result of 'workflow'. Returns a promise that when derefed will resolve to the evaluation of the
defworkflow once the workflow concludes.
```clojure
(defworkflow my-workflow
[ctx args]
...)
(let [w (create ...)]
(start w ...)
@(get-result w))
```
"
[{:keys [^WorkflowStub stub] :as workflow}]
(-> (.getResultAsync stub u/bytes-type)
(p/then nippy/thaw)
(p/catch e/slingshot? e/recast-stone)
(p/catch (fn [e]
(log/error e)
(throw e)))))
(defn query
"
Sends query with 'query-type' and 'args' to 'workflow', returns a value.
The query result is computed by a query-handler, registered inside the workflow definition
using [[temporal.workflow/register-query-handler!]].
Arguments:
- `query-type`: keyword (or coerceable into a keyword)
- `args`: serializable query params
```clojure
(query workflow ::my-query {:foo \"bar\"})
```
"
[{:keys [^WorkflowStub stub] :as workflow} query-type args]
(-> (.query stub (u/namify query-type) u/bytes-type (u/->objarray args))
(nippy/thaw)))
(defn cancel
"
Gracefully cancels 'workflow'
```clojure
(cancel workflow)
```
"
[{:keys [^WorkflowStub stub] :as workflow}]
(.cancel stub))
(defn terminate
"
Forcefully terminates 'workflow'
```clojure
(terminate workflow \"unresponsive\", {})
```
"
[{:keys [^WorkflowStub stub] :as workflow} reason params]
(.terminate stub reason (u/->objarray params)))