Skip to content

Commit

Permalink
Add goroutine to check if Asterisk entity ID has changed. (#33)
Browse files Browse the repository at this point in the history
* Add goroutine to check if Asterisk entity ID has changed.

* Add comment
  • Loading branch information
mtryfoss authored Jun 30, 2020
1 parent 732492e commit eef7e79
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions proxy/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

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

// 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).
type Announcement struct {
Expand Down
28 changes: 28 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"time"
"os"

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

// Run the entity check handler
go s.runEntityChecker(ctx)

// TODO: run the dialog cleanup routine (remove bindings for entities which no longer exist)
// go s.runDialogCleaner(ctx)

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

// runEntityChecker runs the periodic check againt Asterisk entity id
func (s *Server) runEntityChecker(ctx context.Context) {
ticker := time.NewTicker(proxy.EntityCheckInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
info, err := s.ari.Asterisk().Info(nil)
if err != nil {
s.Log.Error("failed to get info from Asterisk", "error", err)
continue
}
if s.AsteriskID != info.SystemInfo.EntityID {
s.Log.Warn("system entitiy id changed", "old", s.AsteriskID, "new", info.SystemInfo.EntityID)
// We need to exit with non-zero to make sure systemd restarts when service defined with Restart=on-failure
os.Exit(1)
}
}
}
}

// runAnnouncer runs the periodic discovery announcer
func (s *Server) runAnnouncer(ctx context.Context) {
ticker := time.NewTicker(proxy.AnnouncementInterval)
Expand Down

0 comments on commit eef7e79

Please sign in to comment.