Skip to content

Commit

Permalink
example: Make the events command long-running
Browse files Browse the repository at this point in the history
To try & show-case the interrupt support in script, change the
events command to show events until interrupted.

Example run:

  example % go run . repl
  example> hive start
  ...
  example> events
  {Reserve of peanut butter sandwiches: 99%}
  {Elevator music volume now set to 80%}
  {Mission completion:  5%}
  ^C (interrupted)
  example> hive stop

Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Oct 11, 2024
1 parent 0775a98 commit b87deb3
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions example/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var eventsCell = cell.Module(

cell.Provide(
newExampleEvents,
showEventsCommand,
watchEventsCommand,
),
)

Expand Down Expand Up @@ -92,24 +92,17 @@ func newExampleEvents(lc cell.Lifecycle, jobs job.Registry, health cell.Health)
return es
}

// showEventsCommand defines the hive script command "events" that subscribes
// and shows 5 events.
func showEventsCommand(ee ExampleEvents) hive.ScriptCmdOut {
// watchEventsCommand defines the hive script command "events" that subscribes
// to events.
func watchEventsCommand(ee ExampleEvents) hive.ScriptCmdOut {
return hive.NewScriptCmd(
"events",
script.Command(
script.CmdUsage{Summary: "Show 5 events"},
script.CmdUsage{Summary: "Watch events"},
func(s *script.State, args ...string) (script.WaitFunc, error) {
n := 5
ctx, cancel := context.WithCancel(s.Context())
defer cancel()
for e := range stream.ToChannel(ctx, ee) {
if n >= 0 {
s.Logf("%s\n", e)
} else {
cancel()
}
n--
for e := range stream.ToChannel(s.Context(), ee) {
s.Logf("%s\n", e)
s.FlushLog()
}
return nil, nil
},
Expand Down

0 comments on commit b87deb3

Please sign in to comment.