Skip to main content
Version: 0.17.0

Using another unit

A Plugin may ask another unit's questions and follow its streams, without knowing which unit it is. It declares that it needs a unit, and what it uses of it; the application binds the need to one of its Plugin units; an operator grants the use; and the Core hands the Plugin the path of a channel on which it is that unit's client. The Plugin speaks to that channel with its family's interface library — the same one an interface uses — and the unit it asks answers it as it answers any client.

The example on this page is com.example.agenda: it answers agenda.next with what is left of the day, by asking the unit its need calendar is bound to the question calendar.events. In an application that unit is the pool's calendar (a Plugin that uses another).

What it declares​

com.example.agenda.yaml
manifest: 1
id: com.example.agenda
protocol: 1
needs:
- unit:calendar
queries:
- id: agenda.next
uses:
calendar:
questions:
- calendar.events
capabilities:
- name: query.agenda.next.answer
governs:
query: agenda.next
- name: query.calendar.events.ask
governs:
use:
need: calendar
query: calendar.events
DeclaredSays
needs: unit:calendarthe Plugin uses another unit's contract, through a need it calls calendar
uses.calendarwhat it uses of that unit: the questions it asks, the streams it follows, in that unit's names. No commands: a unit never instructs another
query.calendar.events.askthe capability an operator grants for the use; a follow would be stream.<id>.follow

A need of class unit is always bound. There is no optional need: an application that takes agenda binds calendar to a unit, or the gate refuses it.

The program​

The unit is given the channel's path in the argument it declared, ${bind.calendar}, and nothing else — not the name of the unit behind it. It reads that name from the channel's opening, which shows the one unit the channel may address. Asking while it answers, it names the question it answers as the cause: the Core gives the inner question that one's chain, and its deadline less 500 ms, so the asker's wait bounds the whole chain.

plugin/main.go
// Command agenda is a Plugin that answers what is next today, by asking the calendar its need is bound to.
package main

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/yoke-project/yoke-sdk-go/base"
"github.com/yoke-project/yoke-sdk-go/iface"
"github.com/yoke-project/yoke-sdk-go/plugin"
)

func declaration() plugin.Declaration {
return plugin.Declaration{
ID: "com.example.agenda",
Needs: []string{"unit:calendar"},
Queries: []string{"agenda.next"},
Uses: map[string]plugin.Use{"calendar": {Questions: []string{"calendar.events"}}},
Capabilities: []plugin.Capability{
{Name: "query.agenda.next.answer", Governs: plugin.Object{Query: "agenda.next"}},
{Name: "query.calendar.events.ask", Governs: plugin.Object{Use: &plugin.UseObject{Need: "calendar", Query: "calendar.events"}}},
},
}
}

func main() {
if len(os.Args) == 2 && os.Args[1] == "--manifest" {
os.Stdout.Write(declaration().Manifest())
return
}
if len(os.Args) != 3 || os.Args[1] != "--calendar" {
fmt.Fprintln(os.Stderr, "agenda: usage: agenda --calendar ${bind.calendar}")
os.Exit(2)
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
u, err := plugin.Start(ctx, declaration())
if err != nil {
fmt.Fprintln(os.Stderr, "agenda:", err)
os.Exit(1)
}
// The need's channel: its opening shows the one unit it may address.
calendar, err := iface.Attach(ctx, os.Args[2])
if err != nil {
u.Health(50, "the calendar cannot be reached: "+err.Error())
u.Close()
<-u.Done()
os.Exit(1)
}
defer calendar.Close()
bound := ""
for _, r := range calendar.Picture.GetRecords() {
if id := r.GetUnit().GetDeclared().GetIdentity(); id != "" {
bound = id
break
}
}
u.Health(0, "asks "+bound)
for {
select {
case event, open := <-u.Events():
if !open {
return
}
if q, ok := event.(plugin.Question); ok && q.Type == "agenda.next" {
go answer(ctx, u, calendar, bound, q)
}
case <-ctx.Done():
u.Close()
<-u.Done()
return
}
}
}

// answer asks the calendar for the rest of the day, the question answered as the cause, and hands its
// answer on; a refusal is said in this Plugin's own words.
func answer(ctx context.Context, u *plugin.Unit, calendar *iface.Attachment, bound string, q plugin.Question) {
now := time.Now()
window := fmt.Sprintf(`{"from":%d,"to":%d}`, now.UnixMilli(), now.Add(24*time.Hour).UnixMilli())
events, err := calendar.AskCaused(ctx, q.ID, bound, "calendar.events", []byte(window))
var r *base.Refusal
switch {
case err == nil:
u.Answer(q, events)
case errors.As(err, &r):
u.Fail(q.ID, "calendar.refused", bound+" refused calendar.events: "+r.Code+" "+r.Item)
default:
u.Fail(q.ID, "calendar.unavailable", bound+" cannot be asked: "+err.Error())
}
}
PartWhat it does
the attachmentmade once, at the start, to the path in --calendar. On a need's channel nothing prevails and nothing yields: the one client always holds control
the unit boundread from the opening: the channel shows that unit and no other, so the first unit record is it
the causethe identifier of the question being answered. A question with no cause starts a chain of its own, and waits 30 s
the answerhanded on as it came, here; a real Plugin picks what its own contract says
the refusaltranslated: the Plugin answers its own asker in its own terms, calendar.refused or calendar.unavailable, and says what it met

Who asked is in every question, filled by the Core and never by the request: unit:agenda when agenda asks the calendar, channel:<name> from an interface, operator:<person> from yoke. A Plugin that gives different things to different askers decides it from that field.

Following a stream​

A stream the Plugin uses is followed through the same attachment, and the follow outlives a restart of the unit it follows: the library starts the stream where it does not flow, subscribes, and subscribes again each time the delivery ends because the unit's Session did. A stream a need's channel follows is held: another channel's stop is refused stream.held, naming the unit that follows it; an operator's stop still stops it.

FamilyFollowNext frame
GoAttachment.Follow(ctx, unit, stream)Follow.Next(ctx)
RustAttachment::follow(unit, stream)Follow::next()
Pythonawait attachment.follow(unit, stream)await follow.next()
Cyoke_follow_stream(a, unit, stream, …)yoke_follow_next(f, &frame, timeout_ms, &err)
C++attachment->follow(unit, stream)follow->next(…)

A stream followed is declared in uses.<need>.streams and governed by its own capability:

uses:
calendar:
questions: [ calendar.events ]
streams: [ calendar.upcoming ]
capabilities:
- name: stream.calendar.upcoming.follow
governs: { use: { need: calendar, stream: calendar.upcoming } }

When the unit it uses restarts​

Its delivery ends and nothing else does. The Plugin is not restarted; a question asked while the other unit is down is refused unit.not_running or unit.no_session, and the next one is answered once it is back. A follow carries on by itself.

What the Core keeps of it​

Nothing a use carries: not a question's payload, not an answer, not a frame. It reports that a use was refused, unit.use.refused on the using unit, and that a question was caused by another, unit.question.chained on the unit asked, with the chain, the asker and the type. A Plugin that carries a token or a reading for another can count on that.

Next: Handing it on. How an application binds the need and grants the use is a Plugin that uses another; every rule is in the reference, using another unit and a need's channel.