shrub/sys/vane/behn.hoon

292 lines
8.1 KiB
Plaintext
Raw Normal View History

:: %behn, just a timer
!:
2016-11-24 07:25:07 +03:00
!? 164
::
=, behn
|= pit=vase
=> |%
+$ move [p=duct q=(wind note gift:able)]
+$ note :: out request $->
$~ [%b %wait *@da] ::
$% $: %b :: to self
$>(%wait task:able) :: set timer
== ::
$: %d :: to %dill
$>(%flog task:able:dill) :: log output
== == ::
+$ sign
$~ [%b %wake ~]
$% [%b $>(%wake gift:able)]
==
::
+$ behn-state
$: timers=(list timer)
unix-duct=duct
next-wake=(unit @da)
2019-05-01 00:58:51 +03:00
drips=drip-manager
==
::
+$ drip-manager
$: count=@ud
movs=(map @ud vase)
==
::
+$ timer [date=@da =duct]
--
2016-11-24 07:25:07 +03:00
::
2019-01-30 04:15:54 +03:00
=> |%
++ per-event
=| moves=(list move)
|= [[our=ship now=@da =duct] state=behn-state]
2018-12-03 22:45:50 +03:00
::
2019-01-30 04:15:54 +03:00
|%
:: %entry-points
::
2019-02-12 02:34:08 +03:00
:: +born: urbit restarted; refresh :next-wake and store wakeup timer duct
2019-01-30 04:15:54 +03:00
::
2019-02-12 02:34:08 +03:00
++ born set-unix-wake(next-wake.state ~, unix-duct.state duct)
:: +crud: handle failure of previous arvo event
::
2019-01-30 04:15:54 +03:00
++ crud
|= [tag=@tas error=tang]
2019-01-30 04:15:54 +03:00
^+ [moves state]
:: behn must get activated before other vanes in a %wake
::
:: TODO: uncomment this case after switching %crud tags
::
:: We don't know how to handle other errors, so relay them to %dill
:: to be printed and don't treat them as timer failures.
::
:: ?. =(%wake tag)
:: ~& %behn-crud-not-first-activation^tag
:: [[duct %slip %d %flog %crud tag error]~ state]
::
?: =(~ timers.state) ~| %behn-crud-no-timer^tag^error !!
::
(wake `error)
2019-02-12 02:34:08 +03:00
:: +rest: cancel the timer at :date, then adjust unix wakeup
:: +wait: set a new timer at :date, then adjust unix wakeup
2019-01-30 04:15:54 +03:00
::
2019-02-12 02:34:08 +03:00
++ rest |=(date=@da set-unix-wake(timers.state (unset-timer [date duct])))
++ wait |=(date=@da set-unix-wake(timers.state (set-timer [date duct])))
2019-05-01 00:58:51 +03:00
:: +drip: XX
::
++ drip
|= mov=vase
=< [moves state]
^+ event-core
=. moves
[duct %pass /drip/(scot %ud count.drips.state) %b %wait +(now)]~
=. movs.drips.state
(~(put by movs.drips.state) count.drips.state mov)
=. count.drips.state +(count.drips.state)
event-core
:: +take-drip: XX
::
++ take-drip
|= [num=@ud error=(unit tang)]
=< [moves state]
^+ event-core
=/ drip (~(got by movs.drips.state) num)
=. movs.drips.state (~(del by movs.drips.state) num)
?^ error
:: if we errored, drop it
event-core
event-core(moves [duct %give %meta drip]~)
:: +vega: learn of a kernel upgrade
::
2019-02-12 02:34:08 +03:00
++ vega [moves state]
:: +wake: unix says wake up; process the elapsed timer and set :next-wake
2019-01-30 04:15:54 +03:00
::
++ wake
|= error=(unit tang)
2019-01-30 04:15:54 +03:00
^+ [moves state]
2019-02-12 02:34:08 +03:00
::
?~ timers.state ~| %behn-wake-no-timer^error !!
:: if we errored, pop the timer and notify the client vane of the error
::
?^ error
=< set-unix-wake
(emit-vane-wake(timers.state t.timers.state) duct.i.timers.state error)
2019-02-12 02:34:08 +03:00
:: if unix woke us too early, retry by resetting the unix wakeup timer
::
?: (gth date.i.timers.state now)
~? debug=%.n [%behn-wake-too-soon `@dr`(sub date.i.timers.state now)]
set-unix-wake(next-wake.state ~)
:: pop first timer, tell vane it has elapsed, and adjust next unix wakeup
::
=< set-unix-wake
(emit-vane-wake(timers.state t.timers.state) duct.i.timers.state ~)
2019-01-30 04:15:54 +03:00
:: +wegh: produce memory usage report for |mass
::
++ wegh
^+ [moves state]
:_ state :_ ~
:^ duct %give %mass
2019-01-31 05:11:44 +03:00
:+ %behn %|
:~ timers+&+timers.state
dot+&+state
2019-01-30 04:15:54 +03:00
==
:: %utilities
::
::+|
::
++ event-core .
2019-02-12 02:34:08 +03:00
:: +emit-vane-wake: produce a move to wake a vane; assumes no prior moves
::
++ emit-vane-wake
|= [=^duct error=(unit tang)]
event-core(moves [duct %give %wake error]~)
2019-02-12 02:34:08 +03:00
:: +emit-doze: set new unix wakeup timer in state and emit move to unix
::
2019-01-08 00:47:23 +03:00
:: We prepend the unix %doze event so that it is handled first. Arvo must
:: handle this first because the moves %behn emits will get handled in
:: depth-first order. If we're handling a %wake which causes a move to a
2019-01-08 00:47:23 +03:00
:: different vane and a %doze event to send to unix, Arvo needs to process
2019-01-08 00:58:08 +03:00
:: the %doze first because otherwise if the move to the other vane calls
:: back into %behn and emits a second %doze, the second %doze would be
:: handled by unix first which is incorrect.
::
2019-02-12 02:34:08 +03:00
++ emit-doze
|= =date=(unit @da)
^+ event-core
:: no-op if .unix-duct has not yet been set
::
?~ unix-duct.state
event-core
2019-02-12 02:34:08 +03:00
:: make sure we don't try to wake up in the past
::
=? date-unit ?=(^ date-unit) `(max now u.date-unit)
::
%_ event-core
next-wake.state date-unit
moves [[unix-duct.state %give %doze date-unit] moves]
==
:: +set-unix-wake: set or unset next unix wakeup timer based on :i.timers
::
++ set-unix-wake
=< [moves state]
^+ event-core
2019-01-30 04:15:54 +03:00
::
=* next-wake next-wake.state
=* timers timers.state
:: if no timers, cancel existing wakeup timer or no-op
::
?~ timers
?~ next-wake
2019-02-12 02:34:08 +03:00
event-core
(emit-doze ~)
:: if :next-wake is in the past or not soon enough, reset it
::
?^ next-wake
?: &((gte date.i.timers u.next-wake) (lte now u.next-wake))
2019-02-12 02:34:08 +03:00
event-core
(emit-doze `date.i.timers)
:: there was no unix wakeup timer; set one
::
2019-02-12 02:34:08 +03:00
(emit-doze `date.i.timers)
2019-01-30 04:15:54 +03:00
:: +set-timer: set a timer, maintaining the sort order of the :timers list
::
++ set-timer
=* timers timers.state
|= t=timer
^+ timers
::
?~ timers
~[t]
:: ignore duplicates
::
?: =(t i.timers)
2019-02-12 02:34:08 +03:00
~? debug=%.n [%behn-set-duplicate t]
timers
2019-01-30 04:15:54 +03:00
:: timers at the same date form a fifo queue
::
?: (lth date.t date.i.timers)
[t timers]
::
[i.timers $(timers t.timers)]
:: +unset-timer: cancel a timer; if it already expired, no-op
::
++ unset-timer
=* timers timers.state
2019-02-12 02:34:08 +03:00
|= t=timer
2019-01-30 04:15:54 +03:00
^+ timers
2019-02-12 02:34:08 +03:00
:: if we don't have this timer, no-op
2019-01-30 04:15:54 +03:00
::
?~ timers
2019-02-12 02:34:08 +03:00
~? debug=%.n [%behn-unset-missing t]
2019-01-30 04:15:54 +03:00
~
?: =(i.timers t)
t.timers
::
[i.timers $(timers t.timers)]
--
2019-01-30 04:15:54 +03:00
--
::
=| behn-state
=* state -
|= [our=ship now=@da eny=@uvJ ski=sley]
=* behn-gate .
^?
|%
:: +call: handle a +task:able:behn request
::
++ call
|= $: hen=duct
type=*
wrapped-task=(hobo task:able)
==
^- [(list move) _behn-gate]
::
=/ =task:able
?. ?=(%soft -.wrapped-task)
wrapped-task
2019-05-09 22:46:19 +03:00
;;(task:able p.wrapped-task)
2019-01-30 04:15:54 +03:00
::
=/ event-core (per-event [our now hen] state)
::
=^ moves state
?- -.task
%born born:event-core
%crud (crud:event-core [tag tang]:task)
2019-01-30 04:15:54 +03:00
%rest (rest:event-core date=p.task)
2019-05-01 00:58:51 +03:00
%drip (drip:event-core move=p.task)
%vega vega:event-core
2019-01-30 04:15:54 +03:00
%wait (wait:event-core date=p.task)
%wake (wake:event-core error=~)
2019-01-30 04:15:54 +03:00
%wegh wegh:event-core
==
[moves behn-gate]
:: +load: migrate an old state to a new behn version
2016-11-24 07:25:07 +03:00
::
++ load
|= old=*
2019-01-30 04:15:54 +03:00
^+ behn-gate
::
~| %behn-load-fail
behn-gate(state (behn-state old))
:: +scry: view timer state
::
:: TODO: not referentially transparent w.r.t. elapsed timers,
:: which might or might not show up in the product
2016-11-24 07:25:07 +03:00
::
++ scry
|= [fur=(unit (set monk)) ren=@tas why=shop syd=desk lot=coin tyl=path]
2016-11-24 07:25:07 +03:00
^- (unit (unit cage))
::
?. ?=(%& -.why)
~
[~ ~ %tank !>(>timers<)]
2016-11-24 07:25:07 +03:00
::
++ stay state
2019-01-30 04:15:54 +03:00
++ take
|= [tea=wire hen=duct hin=(hypo sign)]
2019-01-30 04:15:54 +03:00
^- [(list move) _behn-gate]
2019-05-01 00:58:51 +03:00
?> ?=([%drip @ ~] tea)
=/ event-core (per-event [our now hen] state)
=^ moves state
(take-drip:event-core (slav %ud i.t.tea) error.q.hin)
[moves behn-gate]
2016-11-24 07:25:07 +03:00
--
2019-01-31 05:11:44 +03:00