shrub/pkg/arvo/lib/push-hook.hoon

548 lines
14 KiB
Plaintext
Raw Normal View History

2020-08-05 06:55:12 +03:00
:: lib/push-hook: helper for creating a push hook
2020-08-01 04:11:13 +03:00
::
:: lib/push-hook is a helper for automatically pushing data from a
:: local store to the corresponding pull-hook on remote ships. It also
:: proxies remote pokes to the store.
::
:: ## Interfacing notes:
::
2020-09-03 03:13:29 +03:00
:: The inner door may interact with the library by producing cards.
:: Do not pass any cards on a wire beginning with /helper as these
:: wires are reserved by this library. Any watches/pokes/peeks not
:: listed below will be routed to the inner door.
2020-08-01 04:11:13 +03:00
::
:: ## Subscription paths
::
:: /resource/[resource]: Receive initial state and updates to
2020-08-05 06:55:12 +03:00
:: .resource. .resource should be encoded with en-path:resource from
2020-08-01 04:11:13 +03:00
:: /lib/resource. Facts on this path will be of mark
:: update-mark.config
::
:: ## Pokes
::
:: %push-hook-action: Add/remove a resource from pushing.
:: [update-mark.config]: A poke to proxy to the local store or a
:: foreign push-hook
2020-08-01 04:11:13 +03:00
::
2020-06-27 00:13:12 +03:00
/- *push-hook
2021-03-10 08:39:43 +03:00
/+ default-agent, resource, verb, versioning, agentio
~% %push-hook-top ..part ~
2020-06-27 00:13:12 +03:00
|%
+$ card card:agent:gall
::
2020-08-01 04:11:13 +03:00
:: $config: configuration for the push hook
::
:: .store-name: name of the store to proxy pokes and
:: subscriptions to
:: .store-path: subscription path to receive updates on
:: .update-mark: mark that updates will be tagged with
:: .pull-hook-name: name of the corresponding pull-hook
::
2020-06-27 00:13:12 +03:00
+$ config
$: store-name=term
store-path=path
update=mold
update-mark=term
pull-hook-name=term
2021-03-09 09:03:16 +03:00
version=@ud
2021-03-10 08:39:43 +03:00
min-version=@ud
2020-06-27 00:13:12 +03:00
==
2020-08-01 04:11:13 +03:00
::
:: $base-state-0: state for the push hook
2020-08-01 04:11:13 +03:00
::
:: .sharing: resources that the push hook is proxying
:: .inner-state: state given to internal door
::
+$ base-state-0
$: sharing=(set resource)
inner-state=vase
2020-06-27 00:13:12 +03:00
==
::
+$ base-state-1
$: prev-version=@ud
prev-min-version=@ud
base-state-0
==
::
+$ state-0 [%0 base-state-0]
::
+$ state-1 [%1 base-state-0]
+$ state-2 [%2 base-state-1]
::
+$ versioned-state
$% state-0
state-1
state-2
==
2021-05-04 04:06:59 +03:00
:: +diplomatic: only renegotiate if versions changed
::
:: If %.n please leave note as to why renegotiation necessary
::
++ diplomatic
^- ?
2021-05-11 04:50:12 +03:00
%.y
2021-05-04 04:06:59 +03:00
::
2020-06-27 00:13:12 +03:00
++ push-hook
~/ %push-hook
2020-06-27 00:13:12 +03:00
|* =config
$_ ^|
|_ bowl:gall
::
:: +resource-for-update: get affected resources from an update
::
:: Given a vase of the update, the mark of which is
:: update-mark.config, produce the affected resources, if any.
::
++ resource-for-update
|~ vase
*(list resource)
::
2020-08-05 06:55:12 +03:00
:: +take-update: handle update from store
2020-06-27 00:13:12 +03:00
::
2020-08-01 04:11:13 +03:00
:: Given an update from the store, do other things after proxying
:: the update
::
2020-06-27 00:13:12 +03:00
++ take-update
|~ vase
*[(list card) _^|(..on-init)]
:: +transform-proxy-update: optionally transform update
2020-06-27 00:13:12 +03:00
::
:: If ^ is produced, then the update is forwarded to the local
:: store. If ~ is produced, the update is not forwarded and the
:: poke fails.
2020-06-27 00:13:12 +03:00
::
++ transform-proxy-update
2020-06-27 00:13:12 +03:00
|~ vase
*[(list card) (unit vase)]
2020-06-27 00:13:12 +03:00
:: +initial-watch: produce initial state for a subscription
::
:: .resource is the resource being subscribed to.
2020-08-01 04:11:13 +03:00
:: .path is any additional information in the subscription wire.
:: This would typically be used to encode state that the subscriber
:: already has. For example, a chat client might encode
:: the number of messages that it already has, or the date it last
:: received an update.
::
:: If +initial-watch crashes, the subscription fails.
2020-06-27 00:13:12 +03:00
::
++ initial-watch
|~ [path resource]
*vase
2020-08-05 06:55:12 +03:00
:: from agent:gall
::
++ on-init
*[(list card) _^|(..on-init)]
::
++ on-save
*vase
::
++ on-load
|~ vase
*[(list card) _^|(..on-init)]
::
++ on-poke
|~ cage
*[(list card) _^|(..on-init)]
::
++ on-watch
|~ path
*[(list card) _^|(..on-init)]
2020-06-27 00:13:12 +03:00
::
2020-08-05 06:55:12 +03:00
++ on-leave
|~ path
*[(list card) _^|(..on-init)]
::
++ on-peek
|~ path
*(unit (unit cage))
::
++ on-agent
|~ [wire sign:agent:gall]
*[(list card) _^|(..on-init)]
::
++ on-arvo
|~ [wire sign-arvo]
*[(list card) _^|(..on-init)]
::
++ on-fail
|~ [term tang]
*[(list card) _^|(..on-init)]
2020-06-27 00:13:12 +03:00
--
++ agent
|* =config
|= =(push-hook config)
=| state-2
2020-06-27 00:13:12 +03:00
=* state -
^- agent:gall
=<
~% %push-agent-lib ..poke-hook-action ~
2020-06-27 00:13:12 +03:00
|_ =bowl:gall
+* this .
og ~(. push-hook bowl)
hc ~(. +> bowl)
def ~(. (default-agent this %|) bowl)
2021-03-10 08:39:43 +03:00
io ~(. agentio bowl)
pass pass:io
2021-03-11 06:49:42 +03:00
ver ~(. versioning [bowl [update-mark version min-version]:config])
::
2020-06-27 00:13:12 +03:00
++ on-init
=^ cards push-hook
on-init:og
:_ this
[watch-store:hc cards]
::
++ on-load
|= =old=vase
=/ old
!<(versioned-state old-vase)
=| cards=(list card:agent:gall)
|^
?- -.old
%2
=^ og-cards push-hook
(on-load:og inner-state.old)
2021-03-15 07:03:28 +03:00
=/ old-subs
(find-old-subs [prev-version prev-min-version]:old)
2021-03-15 07:03:28 +03:00
=/ version-cards
:- (fact:io version+!>(version.config) /version ~)
?~ old-subs ~
(kick:io old-subs)^~
[:(weld cards og-cards version-cards) this(state old)]
::
%1
%_ $
old [%2 0 0 +.old]
==
2021-03-11 06:49:42 +03:00
::
::
%0
%_ $
-.old %1
::
cards
=/ paths=(list path)
kicked-watches
?~ paths cards
:_ cards
[%give %kick paths ~]
==
==
::
2021-03-15 07:03:28 +03:00
++ find-old-subs
|= [prev-min-version=@ud prev-version=@ud]
?: ?& =(min-version.config prev-min-version)
=(prev-version version.config)
2021-05-04 04:06:59 +03:00
diplomatic
==
:: bail on kick if we didn't change versions
~
2021-03-15 07:03:28 +03:00
%~ tap in
%+ roll
~(val by sup.bowl)
|= [[=ship =path] out=(set path)]
?. ?=([%resource *] path) out
?. ?=([%resource %ver] path)
(~(put in out) path)
2021-03-15 07:03:28 +03:00
=/ path-ver=@ud
(ver-from-path:hc path)
?: (supported:ver (append-version:ver path-ver)) out
(~(put in out) path)
::
++ kicked-watches
^- (list path)
%~ tap in
%+ roll
~(val by sup.bowl)
|= [[=ship =path] out=(set path)]
?~ path out
?. (lth 4 (lent path))
out
(~(put in out) path)
--
2020-06-27 00:13:12 +03:00
::
++ on-save
2021-04-29 08:29:27 +03:00
=: prev-version version.config
prev-min-version min-version.config
inner-state on-save:og
==
2020-06-27 00:13:12 +03:00
!>(state)
::
++ on-poke
~/ %on-poke
2020-06-27 00:13:12 +03:00
|= [=mark =vase]
^- (quip card:agent:gall agent:gall)
?: =(mark %push-hook-action)
?> (team:title our.bowl src.bowl)
=^ cards state
(poke-hook-action:hc !<(action vase))
[cards this]
::
2021-03-11 06:49:42 +03:00
?: (is-root:ver mark)
:_ this
(forward-update:hc mark vase)
2020-06-27 00:13:12 +03:00
=^ cards push-hook
(on-poke:og mark vase)
[cards this]
::
++ on-watch
~/ %on-watch
2020-06-27 00:13:12 +03:00
|= =path
^- (quip card:agent:gall agent:gall)
2021-03-10 08:39:43 +03:00
?: ?=([%version ~] path)
:_ this
2021-03-15 07:03:28 +03:00
(fact-init:io version+!>(min-version.config))^~
2020-06-27 00:13:12 +03:00
?. ?=([%resource *] path)
=^ cards push-hook
(on-watch:og path)
[cards this]
|^
?. ?=([%ver %ship @ @ @ *] t.path)
unversioned
2020-06-27 00:13:12 +03:00
=/ =resource
(de-path:resource t.t.path)
=/ =mark
(append-version:ver (slav %ud i.t.t.t.t.t.path))
?. (supported:ver mark)
2021-03-10 08:39:43 +03:00
:_ this
2021-03-15 07:03:28 +03:00
(fact-init-kick:io version+!>(min-version.config))
2020-06-27 00:13:12 +03:00
:_ this
=- [%give %fact ~ -]~
(convert-to:ver mark (initial-watch:og t.t.t.t.t.t.path resource))
::
++ unversioned
?> ?=([%ship @ @ *] t.path)
=/ =resource
(de-path:resource t.path)
=/ =vase
(initial-watch:og t.t.t.t.path resource)
:_ this
?. =(min-version.config 0)
~& >>> "unversioned req from: {<src.bowl>}, nooping"
~
[%give %fact ~ (convert-to:ver update-mark.config vase)]~
--
2020-06-27 00:13:12 +03:00
::
++ on-agent
~/ %on-agent
2020-06-27 00:13:12 +03:00
|= [=wire =sign:agent:gall]
^- (quip card:agent:gall agent:gall)
?. ?=([%helper %push-hook @ *] wire)
=^ cards push-hook
(on-agent:og wire sign)
[cards this]
?. ?=(%store i.t.t.wire)
(on-agent:def wire sign)
?+ -.sign (on-agent:def wire sign)
%kick [~[watch-store:hc] this]
::
%fact
2021-03-11 06:49:42 +03:00
?. (is-root:ver p.cage.sign)
2020-06-27 00:13:12 +03:00
=^ cards push-hook
(on-agent:og wire sign)
[cards this]
=^ cards push-hook
(take-update:og q.cage.sign)
:_ this
%+ weld
2021-03-11 06:49:42 +03:00
(push-updates:hc cage.sign)
2020-06-27 00:13:12 +03:00
cards
==
::
2020-06-27 00:13:12 +03:00
++ on-leave
|= =path
=^ cards push-hook
(on-leave:og path)
[cards this]
::
2020-06-27 00:13:12 +03:00
++ on-arvo
|= [=wire =sign-arvo]
=^ cards push-hook
(on-arvo:og wire sign-arvo)
[cards this]
::
2020-06-27 00:13:12 +03:00
++ on-fail
|= [=term =tang]
=^ cards push-hook
(on-fail:og term tang)
[cards this]
::
2020-11-30 06:35:11 +03:00
++ on-peek
|= =path
^- (unit (unit cage))
2021-02-10 10:20:01 +03:00
?: =(/x/dbug/state path)
``noun+(slop !>(state(inner-state *vase)) on-save:og)
2021-03-26 09:58:02 +03:00
?+ path (on-peek:og path)
[%x %sharing ~] ``noun+!>(sharing)
[%x %version ~] ``version+!>(version.config)
[%x %min-version ~] ``version+!>(version.config)
==
2020-06-27 00:13:12 +03:00
--
~% %push-helper-lib ..card ~
2020-06-27 00:13:12 +03:00
|_ =bowl:gall
+* og ~(. push-hook bowl)
2021-03-11 06:49:42 +03:00
ver ~(. versioning [bowl [update-mark version min-version]:config])
2021-03-22 02:54:59 +03:00
io ~(. agentio bowl)
pass pass:io
2020-06-27 00:13:12 +03:00
::
++ poke-hook-action
~/ %poke-hook-action
2020-06-27 00:13:12 +03:00
|= =action
^- (quip card:agent:gall _state)
|^
?- -.action
%add (add +.action)
2020-06-27 00:13:12 +03:00
%remove (remove +.action)
%revoke (revoke +.action)
==
::
2020-06-27 00:13:12 +03:00
++ add
|= rid=resource
=. sharing
(~(put in sharing) rid)
`state
::
++ remove
|= rid=resource
=/ pax=path
[%resource (en-path:resource rid)]
=/ paths=(set path)
%- silt
2020-06-27 00:13:12 +03:00
%+ turn
(incoming-subscriptions pax)
|=([ship pox=path] pox)
2020-06-27 00:13:12 +03:00
=. sharing
(~(del in sharing) rid)
:_ state
[%give %kick ~(tap in paths) ~]~
2020-06-27 00:13:12 +03:00
::
++ revoke
|= [ships=(set ship) rid=resource]
=/ pax=path
[%resource (en-path:resource rid)]
:_ state
%+ murn
(incoming-subscriptions pax)
|= [her=ship =path]
^- (unit card)
?. (~(has in ships) her)
~
`[%give %kick ~[path] `her]
--
::
2020-06-27 00:13:12 +03:00
++ incoming-subscriptions
|= prefix=path
^- (list (pair ship path))
%+ skim
~(val by sup.bowl)
|= [him=ship pax=path]
=/ idx=(unit @)
(find prefix pax)
?~ idx %.n
=(u.idx 0)
::
++ make-wire
|= =wire
^+ wire
%+ weld
/helper/push-hook
wire
::
++ watch-store
^- card:agent:gall
=/ =wire
(make-wire /store)
[%pass wire %agent [our.bowl store-name.config] %watch store-path.config]
::
++ push-updates
~/ %push-updates
2021-03-09 09:03:16 +03:00
|= =cage
2020-06-27 00:13:12 +03:00
^- (list card:agent:gall)
2021-03-22 02:54:59 +03:00
%+ roll (resource-for-update q.cage)
|= [rid=resource cards=(list card)]
|^
:(weld cards versioned unversioned)
::
++ versioned
^- (list card:agent:gall)
=/ prefix=path
resource+ver+(en-path:resource rid)
=/ paths=(jug @ud path)
%+ roll
(incoming-subscriptions prefix)
|= [[ship =path] out=(jug @ud path)]
=/ path-ver=@ud
(ver-from-path path)
(~(put ju out) path-ver path)
2021-03-09 09:03:16 +03:00
%+ turn ~(tap by paths)
|= [fact-ver=@ud paths=(set path)]
=/ =mark
(append-version:ver fact-ver)
(fact:io (convert-from:ver mark q.cage) ~(tap in paths))
2021-03-22 02:54:59 +03:00
:: TODO: deprecate
++ unversioned
?. =(min-version.config 0) ~
2021-03-22 02:54:59 +03:00
=/ prefix=path
resource+(en-path:resource rid)
=/ unversioned=(set path)
%- ~(gas in *(set path))
(turn (incoming-subscriptions prefix) tail)
?: =(0 ~(wyt in unversioned)) ~
(fact:io (convert-from:ver update-mark.config q.cage) ~(tap in unversioned))^~
2021-03-22 02:54:59 +03:00
--
::
++ forward-update
~/ %forward-update
|= =cage
^- (list card:agent:gall)
=- lis
=/ vas=vase
q:(convert-to:ver cage)
%+ roll (resource-for-update q.cage)
|= [rid=resource [lis=(list card:agent:gall) tf-vas=(unit vase)]]
^- [(list card:agent:gall) (unit vase)]
=/ =path
resource+(en-path:resource rid)
=* ship entity.rid
2021-05-10 23:16:24 +03:00
=/ out=(pair (list card:agent:gall) (unit vase))
?. =(our.bowl ship)
:: do not transform before forwarding
::
2021-05-10 23:16:24 +03:00
``vas
:: use cached transform
::
2021-05-10 23:16:24 +03:00
?^ tf-vas `tf-vas
:: transform before poking store
::
(transform-proxy-update:og vas)
2021-05-10 23:16:24 +03:00
~| "forwarding failed during transform. mark: {<p.cage>} rid: {<rid>}"
?> ?=(^ q.out)
:_ q.out
:_ (weld lis p.out)
=/ =wire (make-wire path)
=- [%pass wire %agent - %poke [current-version:ver u.q.out]]
:- ship
?. =(our.bowl ship)
:: forward to host
::
2021-05-10 23:16:24 +03:00
dap.bowl
:: poke our store
::
store-name.config
::
2021-03-15 07:03:28 +03:00
++ ver-from-path
|= =path
=/ extra=^path
(slag 5 path)
2021-03-15 07:03:28 +03:00
?> ?=(^ extra)
(slav %ud i.extra)
::
++ resource-for-update
~/ %resource-for-update
|= =vase
^- (list resource)
%~ tap in
%- silt
(resource-for-update:og vase)
2020-06-27 00:13:12 +03:00
--
--