Added execute single online presence status message

This commit is contained in:
2021-09-27 19:23:30 +02:00
parent 7765cb32a9
commit f72b8c0da1
5 changed files with 73 additions and 23 deletions

View File

@@ -43,6 +43,6 @@ Otherwise build the binary like every other golang binary and then copy it.
| |
| PRESENCE_STATUS_DELAY | NO | 2 min | Delay for online presence message loop |
| COMMAND_REPLY_DELAY | NO | 10 sec | Delay for command message reply |
| EXECUTE_SIGLE_STATUS_MESSAGE | NO | false | Send one presence stanza message for clients and terminate|
| EXECUTE_SINGLE_STATUS_MESSAGE | NO | false | Send one presence stanza message for clients and terminate|
| |

View File

@@ -17,7 +17,7 @@ func Load() {
GeneralOptions: GeneralOptions{
PresenceStatusDelay: int64(getEnvInt("PRESENCE_STATUS_DELAY", 2) * 60),
CommandReplyDelay: getEnvInt("COMMAND_REPLY_DELAY", 10),
ExecuteSingleStatusMessage: getEnvBool("EXECUTE_SIGLE_STATUS_MESSAGE"),
ExecuteSingleStatusMessage: getEnvBool("EXECUTE_SNIGLE_STATUS_MESSAGE"),
},
}
}

View File

@@ -34,7 +34,7 @@ func getEnvInt(key string, defaultValue int) int {
}
// getEnv extracts the bool value from environment variable
func getEnvBool(env string) bool {
func mustGetEnvBool(env string) bool {
envVal := mustGetEnv(env)
value, err := strconv.ParseBool(envVal)
@@ -45,6 +45,18 @@ func getEnvBool(env string) bool {
return value
}
// getEnv extracts the bool value from environment variable
func getEnvBool(env string) bool {
envVal := getEnv(env, "false")
value, err := strconv.ParseBool(envVal)
if err != nil {
value = false
}
return value
}
// mustGetEnv returns value for give key from environment
// if key is not present in environment function will panic
func mustGetEnv(key string) string {

27
main.go
View File

@@ -2,7 +2,6 @@ package main
import (
"log"
"time"
appConfig "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/config"
xmppService "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/services"
@@ -19,25 +18,13 @@ func main() {
// Send online presence stanza
xmppServiceInstance := xmppService.Instance()
for {
for _, xmppClient := range xmppServiceInstance.XMPPClients {
// Send online presence stanza in go rutines
go func(xmppClient xmppService.XMPPClient) {
err := xmppService.SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid)
if err != nil {
log.Printf("There was an error while sending online presence stanza %v", err)
return
}
}(xmppClient)
}
// Delay before sending another message
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.PresenceStatusDelay * 1000000000))
// Execute send online presence status message for every client and termintate program
if appConfig.AppConfig.GeneralOptions.ExecuteSingleStatusMessage {
xmppServiceInstance.ExecuteSingleStatusMessage()
log.Println("ExecuteSingleStatusMessage finished, online presente status message sent sucessfuly")
return
}
// Keep the servbie running
xmppServiceInstance.RunOnlinePresenceStatusIndefinetly()
}

View File

@@ -223,6 +223,57 @@ func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
return nil
}
// RunOnlinePresenceStatusIndefinetly - keep sending online presence status messages indefinetly for all clients with delay
func (xmppService *XMPPService) RunOnlinePresenceStatusIndefinetly() {
for {
for _, xmppClient := range xmppService.XMPPClients {
// Send online presence stanza in go rutines
go func(xmppClient XMPPClient) {
err := SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid)
if err != nil {
log.Printf("There was an error while sending online presence stanza %v", err)
return
}
}(xmppClient)
}
// Delay before sending another message
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.PresenceStatusDelay * 1000000000))
}
}
// ExecuteSingleStatusMessage - Wait for all clients to send online presence stanza messages just once
func (xmppService *XMPPService) ExecuteSingleStatusMessage() {
var wg sync.WaitGroup
for _, xmppClient := range xmppService.XMPPClients {
wg.Add(1)
// Send online presence stanza in go rutines
go func(xmppClient XMPPClient) {
defer wg.Done()
err := SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid)
if err != nil {
log.Printf("There was an error while sending online presence stanza %v", err)
return
}
}(xmppClient)
}
// Wait until all clients sent their online prezence staza messages
wg.Wait()
}
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
var clientCredentials []ClientCredentials