sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/cmd/tester.go

109 lines
2.8 KiB
Go
Raw Normal View History

2018-12-14 23:46:58 +01:00
package cmd
import (
"os"
"os/signal"
"syscall"
2018-02-10 13:34:42 +01:00
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/golang-lib/file"
"dev.sum7.eu/genofire/golang-lib/worker"
"dev.sum7.eu/genofire/yaja/client"
2018-12-14 23:46:58 +01:00
"dev.sum7.eu/genofire/yaja/cmd/tester"
"dev.sum7.eu/genofire/yaja/xmpp"
"github.com/spf13/cobra"
)
2018-02-10 13:34:42 +01:00
var (
configTester = &tester.Config{}
testerInstance = tester.NewTester()
testerWorker *worker.Worker
)
// TesterCMD represents the serve command
var TesterCMD = &cobra.Command{
Use: "tester",
Short: "runs xmpp tester server",
Example: "yaja daemon tester -c /etc/yaja.conf",
Run: func(cmd *cobra.Command, args []string) {
if err := file.ReadTOML(configPath, configTester); err != nil {
log.Fatal("unable to load config file:", err)
}
log.SetLevel(configTester.Logging)
2018-02-10 13:34:42 +01:00
if err := file.ReadJSON(configTester.AccountsPath, testerInstance); err != nil {
log.Warn("unable to load state file:", err)
}
2018-02-11 22:03:58 +01:00
testerInstance.Admins = configTester.Admins
testerInstance.LoggingBots = configTester.LoggingBots
clientLogger := log.New()
clientLogger.SetLevel(configTester.LoggingClients)
testerInstance.LoggingClients = clientLogger.WithField("log", "client")
2018-02-11 22:03:58 +01:00
mainClient := &client.Client{
JID: configTester.Client.JID,
Timeout: configTester.Timeout.Duration,
Logging: clientLogger.WithField("jid", configTester.Client.JID.String()),
2018-02-11 22:03:58 +01:00
}
err := mainClient.Connect(configTester.Client.Password)
if err != nil {
log.Fatal("unable to connect with main jabber client: ", err)
}
2018-02-10 13:34:42 +01:00
defer mainClient.Close()
for _, admin := range configTester.Admins {
mainClient.Send(&xmpp.MessageClient{
2018-02-10 13:34:42 +01:00
To: admin,
Type: "chat",
Body: "yaja tester starts",
})
}
2018-02-11 09:07:07 +01:00
testerInstance.Timeout = configTester.Timeout.Duration
2018-02-10 13:34:42 +01:00
testerInstance.Start(mainClient, configTester.Client.Password)
testerInstance.CheckStatus()
2018-02-11 09:07:07 +01:00
testerWorker = worker.NewWorker(configTester.Interval.Duration, func() {
2018-02-10 13:34:42 +01:00
testerInstance.CheckStatus()
file.SaveJSON(configTester.AccountsPath, testerInstance)
file.SaveJSON(configTester.OutputPath, testerInstance.Output())
})
go testerWorker.Start()
2018-02-10 13:34:42 +01:00
log.Info("yaja tester started ")
// Wait for INT/TERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
for sig := range sigs {
log.Infoln("received", sig)
switch sig {
case syscall.SIGTERM:
log.Panic("terminated")
2018-02-11 11:15:11 +01:00
quitTester()
os.Exit(0)
case syscall.SIGQUIT:
quitTester()
case syscall.SIGHUP:
quitTester()
}
}
},
}
func quitTester() {
2018-02-10 13:34:42 +01:00
testerWorker.Close()
testerInstance.Close()
2018-12-14 23:46:58 +01:00
//file.SaveJSON(configTester.AccountsPath, db)
}
func init() {
2018-02-14 02:49:41 +01:00
TesterCMD.Flags().StringVarP(&configPath, "config", "c", "yaja-tester.conf", "path to configuration file")
2018-12-14 23:46:58 +01:00
RootCMD.AddCommand(TesterCMD)
}