diff --git a/configs/app.go b/configs/app.go index 74dc4c8..a027323 100644 --- a/configs/app.go +++ b/configs/app.go @@ -21,6 +21,7 @@ type ZapMeow struct { Instances map[string]*Instance Config ZapMeowConfig Wg *sync.WaitGroup + Mutex *sync.Mutex StopCh *chan struct{} } @@ -31,6 +32,7 @@ func NewZapMeow( instances map[string]*Instance, config ZapMeowConfig, wg *sync.WaitGroup, + mutex *sync.Mutex, stopCh *chan struct{}, ) *ZapMeow { return &ZapMeow{ @@ -40,6 +42,7 @@ func NewZapMeow( Instances: instances, Config: config, Wg: wg, + Mutex: mutex, StopCh: stopCh, } } diff --git a/main.go b/main.go index e0387db..5b7cffb 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( waLog "go.mau.fi/whatsmeow/util/log" "gorm.io/driver/sqlite" "gorm.io/gorm" + "gorm.io/gorm/logger" ) // @title ZapMeow API @@ -45,7 +46,9 @@ func main() { panic(err) } - databaseClient, err := gorm.Open(sqlite.Open(config.DatabaseURL)) + databaseClient, err := gorm.Open(sqlite.Open(config.DatabaseURL), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Silent), + }) if err != nil { panic("Failed to connect to database") } @@ -75,6 +78,7 @@ func main() { panic(err) } + var mutex sync.Mutex var wg sync.WaitGroup wg.Add(1) stopCh := make(chan struct{}) @@ -87,6 +91,7 @@ func main() { instances, config, &wg, + &mutex, &stopCh, ) diff --git a/services/wpp_service.go b/services/wpp_service.go index 3ba1017..911d6ef 100644 --- a/services/wpp_service.go +++ b/services/wpp_service.go @@ -521,24 +521,31 @@ func (w *wppService) qrcode(instanceID string) { case "success": return case "timeout": - // fmt.Println("[qrcode]: timeout error") - err := w.accountService.UpdateAccount(instanceID, map[string]interface{}{ - "QrCode": "", - "Status": "TIMEOUT", - }) - if err != nil { - fmt.Println("[qrcode]: ", err) + for { + w.app.Mutex.Lock() + err := w.accountService.UpdateAccount(instanceID, map[string]interface{}{ + "QrCode": "", + "Status": "TIMEOUT", + }) + w.app.Mutex.Unlock() + if err != nil { + fmt.Println("[qrcode]: ", err) + } + + delete(w.app.Instances, instanceID) } - - delete(w.app.Instances, instanceID) case "code": - w.accountService.UpdateAccount(instanceID, map[string]interface{}{ - "QrCode": evt.Code, - "Status": "UNPAIRED", - "WasSynced": false, - }) - if err != nil { - fmt.Println("[qrcode]: ", err) + for { + w.app.Mutex.Lock() + w.accountService.UpdateAccount(instanceID, map[string]interface{}{ + "QrCode": evt.Code, + "Status": "UNPAIRED", + "WasSynced": false, + }) + w.app.Mutex.Unlock() + if err != nil { + fmt.Println("[qrcode]: ", err) + } } } }