diff --git a/cmd/main/main.go b/cmd/main/main.go index e4d9621..128c99d 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -26,6 +26,8 @@ import ( "syscall" "time" + cron "github.com/robfig/cron/v3" + "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -54,6 +56,11 @@ func (ev *EchoValidator) Validate(i interface{}) error { func main() { var err error + // Intialize Cron + c := cron.New(cron.WithChain( + cron.Recover(cron.DiscardLogger), + ), cron.WithSeconds()) + // Initialize Echo e := echo.New() @@ -115,6 +122,9 @@ func main() { // Running Startup Tasks internal.Startup() + // Running Routines Tasks + internal.Routines(c) + // Get Server Configuration var serverConfig Server @@ -138,17 +148,19 @@ func main() { // Watch for Shutdown Signal sigShutdown := make(chan os.Signal, 1) - signal.Notify(sigShutdown, os.Interrupt) - signal.Notify(sigShutdown, syscall.SIGTERM) + signal.Notify(sigShutdown, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) <-sigShutdown // Wait 5 Seconds Before Graceful Shutdown - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), 5*time.Second) + defer cancelShutdown() // Try To Shutdown Server - err = e.Shutdown(ctx) + err = e.Shutdown(ctxShutdown) if err != nil { log.Print(nil).Fatal(err.Error()) } + + // Try To Shutdown Cron + c.Stop() } diff --git a/go.mod b/go.mod index 7d983e9..5b2bcb5 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/labstack/echo/v4 v4.7.2 github.com/lib/pq v1.10.6 github.com/nickalie/go-webpbin v0.0.0-20220110095747-f10016bf2dc1 + github.com/robfig/cron/v3 v3.0.1 github.com/sirupsen/logrus v1.8.1 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/sunshineplan/imgconv v1.0.5 diff --git a/go.sum b/go.sum index 116b957..c9005bd 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= diff --git a/internal/routines.go b/internal/routines.go new file mode 100644 index 0000000..8353c79 --- /dev/null +++ b/internal/routines.go @@ -0,0 +1,37 @@ +package internal + +import ( + "github.com/robfig/cron/v3" + + "github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/log" + pkgWhatsApp "github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/whatsapp" +) + +func Routines(cron *cron.Cron) { + log.Print(nil).Info("Running Routine Tasks") + + cron.AddFunc("0 * * * * *", func() { + // If WhatsAppClient Connection is more than 0 + if len(pkgWhatsApp.WhatsAppClient) > 0 { + // Check Every Authenticated MSISDN + for jid, client := range pkgWhatsApp.WhatsAppClient { + // Get Real JID from Datastore + realJID := client.Store.ID.User + + // Check WhatsAppClient Registered JID with Authenticated MSISDN + if jid != realJID { + // Mask JID for Logging Information + maskJID := realJID[0:len(realJID)-4] + "xxxx" + + log.Print(nil).Info("Logging out WhatsApp Client for " + maskJID + " Due to Missmatch Authentication") + + // Logout WhatsAppClient Device + _ = pkgWhatsApp.WhatsAppLogout(jid) + delete(pkgWhatsApp.WhatsAppClient, jid) + } + } + } + }) + + cron.Start() +}