Skip to content

Commit eef7e79

Browse files
authored
Add goroutine to check if Asterisk entity ID has changed. (CyCoreSystems#33)
* Add goroutine to check if Asterisk entity ID has changed. * Add comment
1 parent 732492e commit eef7e79

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

proxy/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
// AnnouncementInterval is the amount of time to wait between periodic service availability announcements
1212
var AnnouncementInterval = time.Minute
13+
// EntityCheckInterval is the interval between checks against Asterisk entity ID
14+
var EntityCheckInterval = time.Second * 10
1315

1416
// Announcement describes the structure of an ARI proxy's announcement of availability on the network. These are sent periodically and upon request (by a Ping).
1517
type Announcement struct {

server/server.go

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"time"
7+
"os"
78

89
"github.com/CyCoreSystems/ari-proxy/v5/proxy"
910
"github.com/CyCoreSystems/ari-proxy/v5/server/dialog"
@@ -220,6 +221,9 @@ func (s *Server) listen(ctx context.Context) error {
220221
// Run the event handler
221222
go s.runEventHandler(ctx)
222223

224+
// Run the entity check handler
225+
go s.runEntityChecker(ctx)
226+
223227
// TODO: run the dialog cleanup routine (remove bindings for entities which no longer exist)
224228
// go s.runDialogCleaner(ctx)
225229

@@ -233,6 +237,30 @@ func (s *Server) listen(ctx context.Context) error {
233237
return ctx.Err()
234238
}
235239

240+
// runEntityChecker runs the periodic check againt Asterisk entity id
241+
func (s *Server) runEntityChecker(ctx context.Context) {
242+
ticker := time.NewTicker(proxy.EntityCheckInterval)
243+
defer ticker.Stop()
244+
245+
for {
246+
select {
247+
case <-ctx.Done():
248+
return
249+
case <-ticker.C:
250+
info, err := s.ari.Asterisk().Info(nil)
251+
if err != nil {
252+
s.Log.Error("failed to get info from Asterisk", "error", err)
253+
continue
254+
}
255+
if s.AsteriskID != info.SystemInfo.EntityID {
256+
s.Log.Warn("system entitiy id changed", "old", s.AsteriskID, "new", info.SystemInfo.EntityID)
257+
// We need to exit with non-zero to make sure systemd restarts when service defined with Restart=on-failure
258+
os.Exit(1)
259+
}
260+
}
261+
}
262+
}
263+
236264
// runAnnouncer runs the periodic discovery announcer
237265
func (s *Server) runAnnouncer(ctx context.Context) {
238266
ticker := time.NewTicker(proxy.AnnouncementInterval)

0 commit comments

Comments
 (0)