From c565c04ba9de99e6fe4a6d5b84ce46791ff11545 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Thu, 18 Jul 2019 10:35:02 +0200 Subject: [PATCH] - remove ack of message in AMQP client - add a command line parameter to activate AMQP client --- common/amqpclient.go | 9 +++--- common/database.go | 3 ++ routes/simulator/simulatorEndpoints.go | 7 +++-- start.go | 38 ++++++++++++++------------ 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/common/amqpclient.go b/common/amqpclient.go index 18df329..14eb0ad 100644 --- a/common/amqpclient.go +++ b/common/amqpclient.go @@ -85,13 +85,14 @@ func ConnectAMQP(uri string) error { // consuming queue go func() { for message := range client.replies { - err = message.Ack(false) - if err != nil { - fmt.Println("AMQP: Unable to ack message:", err) - } + //err = message.Ack(false) + //if err != nil { + // fmt.Println("AMQP: Unable to ack message:", err) + //} content := string(message.Body) + // any action message sent by the VILLAScontroller should be ignored by the web backend if strings.Contains(content, "action") { continue } diff --git a/common/database.go b/common/database.go index f746885..2154592 100644 --- a/common/database.go +++ b/common/database.go @@ -13,6 +13,7 @@ var DB_HOST string var DB_NAME string var DB_DUMMY string var DB_SSLMODE string +var WITH_AMQP bool var DBpool *gorm.DB @@ -22,11 +23,13 @@ func init() { flag.StringVar(&DB_NAME, "dbname", "villasdb", "Name of the database to use (default is villasdb)") flag.StringVar(&DB_DUMMY, "dbdummy", "testvillasdb", "Name of the test database to use (default is testvillasdb)") flag.StringVar(&DB_SSLMODE, "dbsslmode", "disable", "SSL mode of DB (default is disable)") // TODO: change default for production + flag.BoolVar(&WITH_AMQP, "amqp", false, "If AMQP client for simulators shall be enabled, set this option to true (default is false)") flag.Parse() fmt.Println("DB_HOST has value ", DB_HOST) fmt.Println("DB_NAME has value ", DB_NAME) fmt.Println("DB_DUMMY has value ", DB_DUMMY) fmt.Println("DB_SSLMODE has value ", DB_SSLMODE) + fmt.Println("WITH_AMQP has value ", WITH_AMQP) } // Initialize connection to the database diff --git a/routes/simulator/simulatorEndpoints.go b/routes/simulator/simulatorEndpoints.go index 4777165..d09919a 100644 --- a/routes/simulator/simulatorEndpoints.go +++ b/routes/simulator/simulatorEndpoints.go @@ -18,7 +18,10 @@ func RegisterSimulatorEndpoints(r *gin.RouterGroup) { r.GET("/:simulatorID", getSimulator) r.DELETE("/:simulatorID", deleteSimulator) r.GET("/:simulatorID/models", getModelsOfSimulator) - r.POST("/:simulatorID/action", sendActionToSimulator) + // register action endpoint only if AMQP client is used + if common.WITH_AMQP == true { + r.POST("/:simulatorID/action", sendActionToSimulator) + } } // getSimulators godoc @@ -280,7 +283,7 @@ func getModelsOfSimulator(c *gin.Context) { } // sendActionToSimulator godoc -// @Summary Send an action to simulator +// @Summary Send an action to simulator (only available if backend server is started with -amqp parameter) // @ID sendActionToSimulator // @Tags simulators // @Produce json diff --git a/start.go b/start.go index 43b8454..c0532f4 100644 --- a/start.go +++ b/start.go @@ -65,27 +65,29 @@ func main() { r.GET("swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) - err := common.ConnectAMQP("amqp://localhost") - if err != nil { - panic(err) - } - - // Periodically call the Ping function to check which simulators are still there - ticker := time.NewTicker(10 * time.Second) - go func() { - - for { - select { - case <-ticker.C: - err = common.PingAMQP() - if err != nil { - fmt.Println("AMQP Error: ", err.Error()) - } - } + if common.WITH_AMQP == true { + fmt.Println("Starting AMQP client") + err := common.ConnectAMQP("amqp://localhost") + if err != nil { + panic(err) } - }() + // Periodically call the Ping function to check which simulators are still there + ticker := time.NewTicker(10 * time.Second) + go func() { + for { + select { + case <-ticker.C: + err = common.PingAMQP() + if err != nil { + fmt.Println("AMQP Error: ", err.Error()) + } + } + } + + }() + } // server at port 4000 to match frontend's redirect path r.Run(":4000") }