From 6c95c6e61744badb0c9930f0ad4f7a9904fdad6d Mon Sep 17 00:00:00 2001 From: elraphty Date: Sat, 14 Dec 2024 20:54:13 +0100 Subject: [PATCH 1/3] feat: added logs to crons --- handlers/v2_payments_cron.go | 33 +++++++++++++++++++-------------- main.go | 2 +- routes/index.go | 2 +- websocket/pool.go | 2 -- websocket/websocket.go | 1 + 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/handlers/v2_payments_cron.go b/handlers/v2_payments_cron.go index ae2d6be81..869227513 100644 --- a/handlers/v2_payments_cron.go +++ b/handlers/v2_payments_cron.go @@ -14,14 +14,14 @@ import ( ) func InitV2PaymentsCron() { + log.Println("Pending Invoice Cron Job Started") paymentHistories := db.DB.GetPendingPaymentHistory() for _, payment := range paymentHistories { - tag := payment.Tag - tagResult := GetInvoiceStatusByTag(tag) - bounty := db.DB.GetBounty(payment.BountyId) if bounty.ID > 0 { + tag := payment.Tag + tagResult := GetInvoiceStatusByTag(tag) if tagResult.Status == db.PaymentComplete { db.DB.SetPaymentAsComplete(tag) @@ -37,32 +37,37 @@ func InitV2PaymentsCron() { bounty.CompletionDate = &now db.DB.UpdateBountyPaymentStatuses(bounty) - - } else if tagResult.Status == db.PaymentFailed { - // Handle failed payments - - err := db.DB.ProcessReversePayments(payment.ID) - if err != nil { - log.Printf("Could not reverse bounty payment : Bounty ID - %d, Payment ID - %d, Error - %s", bounty.ID, payment.ID, err) - } - } else if tagResult.Status == db.PaymentPending { + log.Println("Payment Status From V2 BOT IS Pending", payment) if payment.PaymentStatus == db.PaymentPending { + log.Println("Payment Status From DB IS Pending", payment) created := utils.ConvertTimeToTimestamp(payment.Created.String()) now := time.Now() daysDiff := utils.GetDateDaysDifference(int64(created), &now) + log.Println("Payment Date Difference Is", daysDiff) + if daysDiff >= 7 { + log.Println("Payment Date Difference Is Greater Or Equals 7 Days", payment) + err := db.DB.ProcessReversePayments(payment.ID) if err != nil { log.Printf("Could not reverse bounty payment after 7 days : Bounty ID - %d, Payment ID - %d, Error - %s", bounty.ID, payment.ID, err) } } } - } + } else if tagResult.Status == db.PaymentFailed { + // Handle failed payments + err := db.DB.ProcessReversePayments(payment.ID) + if err != nil { + log.Printf("Could not reverse bounty payment : Bounty ID - %d, Payment ID - %d, Error - %s", bounty.ID, payment.ID, err) + } + } else { + log.Println("Payment Status From V2 BOT IS Unknown", payment, tagResult) + } } } } @@ -99,7 +104,7 @@ func GetInvoiceStatusByTag(tag string) db.V2TagRes { err = json.Unmarshal(body, &tagRes) if err != nil { - log.Printf("Could not unmarshall get tag result: %s", err) + log.Printf("Could not unmarshal get tag result: %s", err) } resultLength := len(tagRes) diff --git a/main.go b/main.go index 793e6ae6c..3475d25cb 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ func main() { func runCron() { c := cron.New() - c.AddFunc("@every 00h30m00s", handlers.InitV2PaymentsCron) + c.AddFunc("@every 0h3m0s", handlers.InitV2PaymentsCron) c.Start() } diff --git a/routes/index.go b/routes/index.go index 27d4931c8..f2945ec18 100644 --- a/routes/index.go +++ b/routes/index.go @@ -62,8 +62,8 @@ func NewRouter() *http.Server { r.Get("/poll/{challenge}", db.Poll) r.Post("/save", db.PostSave) r.Get("/save/{key}", db.PollSave) - r.Get("/websocket", handlers.HandleWebSocket) r.Get("/migrate_bounties", handlers.MigrateBounties) + r.Get("/websocket", handlers.HandleWebSocket) }) r.Group(func(r chi.Router) { diff --git a/websocket/pool.go b/websocket/pool.go index 3270e98ae..39e03c1dc 100644 --- a/websocket/pool.go +++ b/websocket/pool.go @@ -41,12 +41,10 @@ func (pool *Pool) Start() { } else { fmt.Println("Websocket pool client save error") } - break case client := <-pool.Unregister: pool.Clients[client.Host].Client.Conn.WriteJSON(Message{Type: 1, Body: "User Disconnected..."}) delete(pool.Clients, client.Host) fmt.Println("Size of Connection Pool: ", len(pool.Clients)) - break case message := <-pool.Broadcast: fmt.Println("Sending message to all clients in Pool") for client, _ := range pool.Clients { diff --git a/websocket/websocket.go b/websocket/websocket.go index 0431c906b..5ee4d2c1f 100644 --- a/websocket/websocket.go +++ b/websocket/websocket.go @@ -40,6 +40,7 @@ func ServeWs(pool *Pool, w http.ResponseWriter, r *http.Request) { conn, err := Upgrade(w, r) if err != nil { + fmt.Println("Error in ServeWs", err) fmt.Fprintf(w, "%+v\n", err) } From fe36d90767fcb3f69c39cd73acd19377e5dde729 Mon Sep 17 00:00:00 2001 From: elraphty Date: Sat, 14 Dec 2024 20:54:55 +0100 Subject: [PATCH 2/3] feat: changed cron to happen every 30 mins --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 3475d25cb..58843ddf5 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ func main() { func runCron() { c := cron.New() - c.AddFunc("@every 0h3m0s", handlers.InitV2PaymentsCron) + c.AddFunc("@every 0h30m0s", handlers.InitV2PaymentsCron) c.Start() } From 7c1fbe0cc00dd975445327db52e984819f39bb8f Mon Sep 17 00:00:00 2001 From: elraphty Date: Mon, 16 Dec 2024 17:12:24 +0100 Subject: [PATCH 3/3] feat: commented out breaking code --- routes/index.go | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/routes/index.go b/routes/index.go index f2945ec18..030a04db8 100644 --- a/routes/index.go +++ b/routes/index.go @@ -142,35 +142,35 @@ func getFromAuth(path string) (*extractResponse, error) { } // Middleware to handle InternalServerError -func internalServerErrorHandler(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - rr := &responseRecorder{ResponseWriter: w, statusCode: http.StatusOK} - next.ServeHTTP(rr, r) - - if rr.statusCode == http.StatusInternalServerError { - fmt.Printf("Internal Server Error: %s %s\n", r.Method, r.URL.Path) - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) - } - }) -} +// func internalServerErrorHandler(next http.Handler) http.Handler { +// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { +// rr := &responseRecorder{ResponseWriter: w, statusCode: http.StatusOK} +// next.ServeHTTP(rr, r) + +// if rr.statusCode == http.StatusInternalServerError { +// fmt.Printf("Internal Server Error: %s %s\n", r.Method, r.URL.Path) +// http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) +// } +// }) +// } // Custom ResponseWriter to capture status codes -type responseRecorder struct { - http.ResponseWriter - statusCode int -} +// type responseRecorder struct { +// http.ResponseWriter +// statusCode int +// } -func (rr *responseRecorder) WriteHeader(code int) { - rr.statusCode = code - rr.ResponseWriter.WriteHeader(code) -} +// func (rr *responseRecorder) WriteHeader(code int) { +// rr.statusCode = code +// rr.ResponseWriter.WriteHeader(code) +// } func initChi() *chi.Mux { r := chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.Logger) r.Use(middleware.Recoverer) - r.Use(internalServerErrorHandler) + // r.Use(internalServerErrorHandler) cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},