on 514b5fc help sign in
152 lines 4.4 kB history raw
1+++
2title = "Stop Thread"
3weight = 4
4template = "doc.html"
5+++
7Here we've added one last card to `on-poke` to stop the thread and a little extra to `on-agent` to print things for demonstrative purposes.
9#### `thread-starter.hoon`
11```hoon
12/+ default-agent, dbug
13=* card card:agent:gall
14%- agent:dbug
15^- agent:gall
16|_ =bowl:gall
17+* this .
18 def ~(. (default-agent this %|) bowl)
19::
20++ on-init on-init:def
21++ on-save on-save:def
22++ on-load on-load:def
23++ on-poke
24 |= [=mark =vase]
25 ^- (quip card _this)
26 ?+ mark (on-poke:def mark vase)
27 %noun
28 ?+ q.vase (on-poke:def mark vase)
29 (pair term term)
30 =/ tid `@ta`(cat 3 'thread_' (scot %uv (sham eny.bowl)))
31 =/ ta-now `@ta`(scot %da now.bowl)
32 =/ start-args [~ `tid byk.bowl p.q.vase !>(q.q.vase)]
33 :_ this
34 :~
35 [%pass /thread/[ta-now] %agent [our.bowl %spider] %watch /thread-result/[tid]]
36 [%pass /thread/[ta-now] %agent [our.bowl %spider] %poke %spider-start !>(start-args)]
37 [%pass /thread/updates/[ta-now] %agent [our.bowl %spider] %watch /thread/[tid]/updates]
38 [%pass /thread-stop/[ta-now] %agent [our.bowl %spider] %poke %spider-stop !>([tid %.y])]
39 ==
40 ==
41 ==
42++ on-watch on-watch:def
43++ on-leave on-leave:def
44++ on-peek on-peek:def
45++ on-agent
46 |= [=wire =sign:agent:gall]
47 ^- (quip card _this)
48 ?+ -.wire (on-agent:def wire sign)
49 %thread
50 ?+ -.sign (on-agent:def wire sign)
51 %poke-ack
52 ?~ p.sign
53 %- (slog leaf+"Thread started successfully" ~)
54 `this
55 %- (slog leaf+"Thread failed to start" u.p.sign)
56 `this
57 ::
58 %fact
59 ?+ p.cage.sign (on-agent:def wire sign)
60 %thread-fail
61 =/ err !< (pair term tang) q.cage.sign
62 %- (slog leaf+"Thread failed: {(trip p.err)}" q.err)
63 `this
64 %thread-done
65 ?: =(q.cage.sign *vase)
66 %- (slog leaf+"Thread cancelled nicely" ~)
67 `this
68 =/ res (trip !<(term q.cage.sign))
69 %- (slog leaf+"Result: {res}" ~)
70 `this
71 %update
72 =/ msg !< tape q.cage.sign
73 %- (slog leaf+msg ~)
74 `this
75 ==
76 ==
77 %thread-stop
78 ?+ -.sign (on-agent:def wire sign)
79 %poke-ack
80 ?~ p.sign
81 %- (slog leaf+"Thread cancelled successfully" ~)
82 `this
83 %- (slog leaf+"Thread failed to stop" u.p.sign)
84 `this
85 ==
86 ==
87++ on-arvo on-arvo:def
88++ on-fail on-fail:def
89--
90```
92We've also added a `sleep` to the thread to keep it running for demonstration.
94#### `test-thread.hoon`
96```hoon
97/- spider
98/+ *strandio
99=, strand=strand:spider
100^- thread:spider
101|= arg=vase
102=/ m (strand ,vase)
103^- form:m
104;< =path bind:m take-watch
105;< ~ bind:m (send-raw-card [%give %fact ~[path] %update !>("message 1")])
106;< ~ bind:m %- send-raw-cards
107 :~ [%give %fact ~[path] %update !>("message 2")]
108 [%give %fact ~[path] %update !>("message 3")]
109 [%give %fact ~[path] %update !>("message 4")]
110 ==
111;< ~ bind:m (send-raw-card [%give %kick ~[path] ~])
112;< ~ bind:m (sleep ~m1)
113|= strand-input:strand
114?+ q.arg [~ %fail %not-foo ~]
115 %foo
116 [~ %done arg]
117==
118```
120Save these, `|commit`, and run with `:thread-starter [%test-thread %foo]`. You should see:
122```
123Thread started successfully
124message 1
125message 2
126message 3
127message 4
128Thread cancelled successfully
129Thread cancelled nicely
130```
132Now, try changing the vase in our new card from `!>([tid %.y])` to `!>([tid %.n])`. Save, `|commit`, and run again. You should see:
134```
135Thread started successfully
136message 1
137message 2
138message 3
139message 4
140Thread cancelled successfully
141Thread failed: cancelled
142```
144### Analysis
146The card we've added to our agent:
148```hoon
149[%pass /thread-stop/[ta-now] %agent [our.bowl %spider] %poke %spider-stop !>([tid %.y])]
150```
152...pokes spider with mark `%spider-stop` and a vase containing the tid of the thread we want to stop and a `?`. The `?` specifies whether to end it nicely or not. If `%.y` it will end with `%thread-done` and a `*vase` bunted vase. If `%.n` it will end with `%thread-fail` and a vase containing `[term tang]` where `term` is `%cancelled` and `tang` is `~`. You can see the difference in our tests above.