Skip to content

Commit 109a6e9

Browse files
committed
Added a start callback flag to the router
1 parent cbcccca commit 109a6e9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net"
2626
"os"
2727
"os/signal"
28+
"strings"
2829
"sync"
2930
"syscall"
3031
"time"
@@ -34,6 +35,7 @@ import (
3435
"github.com/arduino/arduino-router/msgpackrouter"
3536
"github.com/arduino/arduino-router/msgpackrpc"
3637
networkapi "github.com/arduino/arduino-router/network-api"
38+
"github.com/arduino/go-paths-helper"
3739

3840
"github.com/spf13/cobra"
3941
"go.bug.st/f"
@@ -51,6 +53,7 @@ type Config struct {
5153
SerialPortAddr string
5254
SerialBaudRate int
5355
MonitorPortAddr string
56+
StartCallback string
5457
}
5558

5659
func main() {
@@ -86,6 +89,7 @@ func main() {
8689
cmd.Flags().StringVarP(&cfg.SerialPortAddr, "serial-port", "p", "", "Serial port address")
8790
cmd.Flags().IntVarP(&cfg.SerialBaudRate, "serial-baudrate", "b", 115200, "Serial port baud rate")
8891
cmd.Flags().StringVarP(&cfg.MonitorPortAddr, "monitor-port", "m", "127.0.0.1:7500", "Listening port for MCU monitor proxy")
92+
cmd.Flags().StringVar(&cfg.StartCallback, "after-start", "", "Command to execute when the router has successfully completed startup")
8993
if err := cmd.Execute(); err != nil {
9094
slog.Error("Error executing command.", "error", err)
9195
}
@@ -214,6 +218,10 @@ func startRouter(cfg Config) error {
214218
return true, nil
215219
})
216220
f.Assert(err == nil, "Failed to register $/serial/close method")
221+
222+
var started sync.WaitGroup
223+
started.Add(1)
224+
initialized := sync.OnceFunc(started.Done)
217225
go func() {
218226
for {
219227
serialOpened.L.Lock()
@@ -236,6 +244,7 @@ func startRouter(cfg Config) error {
236244
time.Sleep(5 * time.Second)
237245
continue
238246
}
247+
initialized()
239248
slog.Info("Opened serial connection", "serial", cfg.SerialPortAddr)
240249
wr := &MsgpackDebugStream{Name: cfg.SerialPortAddr, Upstream: serialPort}
241250

@@ -252,6 +261,7 @@ func startRouter(cfg Config) error {
252261
<-routerExit
253262
}
254263
}()
264+
started.Wait()
255265
}
256266

257267
// Wait for incoming connections on all listeners
@@ -270,6 +280,20 @@ func startRouter(cfg Config) error {
270280
}()
271281
}
272282

283+
// Execute start callback if specified
284+
if cfg.StartCallback != "" {
285+
slog.Info("Executing start callback", "cmd", cfg.StartCallback)
286+
cb, err := paths.NewProcess(nil, strings.Split(cfg.StartCallback, " ")...)
287+
if err != nil {
288+
slog.Error("Failed to start callback process", "err", err)
289+
return err
290+
}
291+
if err := cb.Run(); err != nil {
292+
slog.Error("Start callback process failed", "err", err)
293+
return err
294+
}
295+
}
296+
273297
// Sleep forever until interrupted
274298
signalChan := make(chan os.Signal, 1)
275299
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

0 commit comments

Comments
 (0)