hotfix
This commit is contained in:
+2
-1
@@ -1 +1,2 @@
|
|||||||
.env
|
.env
|
||||||
|
/logs
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DB is the global database connection pool
|
||||||
|
var DB *sql.DB
|
||||||
|
|
||||||
|
func InitDB() (*sql.DB, error) {
|
||||||
|
// Load environment variables from .env file
|
||||||
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error loading .env file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get connection details from environment variables
|
||||||
|
connStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",
|
||||||
|
os.Getenv("DB_USER"),
|
||||||
|
os.Getenv("DB_PASSWORD"),
|
||||||
|
os.Getenv("DB_HOST"),
|
||||||
|
os.Getenv("DB_PORT"),
|
||||||
|
os.Getenv("DB_NAME"),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Open the database connection
|
||||||
|
DB, err = sql.Open("mysql", connStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error opening database: %v", err)
|
||||||
|
}
|
||||||
|
// Set connection pool parameters
|
||||||
|
DB.SetMaxOpenConns(100) // Maximum number of open connections to the database
|
||||||
|
DB.SetMaxIdleConns(100) // Maximum number of connections in the idle connection pool
|
||||||
|
DB.SetConnMaxLifetime(5 * time.Minute) // Maximum amount of time a connection may be reused
|
||||||
|
|
||||||
|
// Check if the database connection is working
|
||||||
|
if err := DB.Ping(); err != nil {
|
||||||
|
log.Printf("Database connection lost: %v. Reconnecting...", err)
|
||||||
|
DB, err = InitDB()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to reconnect to database: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Print("Database connected successfully!")
|
||||||
|
return DB, nil
|
||||||
|
}
|
||||||
@@ -6,3 +6,8 @@ require (
|
|||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/streadway/amqp v1.1.0
|
github.com/streadway/amqp v1.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
|
github.com/go-sql-driver/mysql v1.9.3 // indirect
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
|
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
|
||||||
|
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM=
|
github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM=
|
||||||
|
|||||||
@@ -142,7 +142,6 @@ func consumeJobCompletions() {
|
|||||||
fmt.Println("Shutting down job completion consumer...")
|
fmt.Println("Shutting down job completion consumer...")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
// Try to connect to RabbitMQ
|
|
||||||
conn, err := amqp.Dial(rabbitURL)
|
conn, err := amqp.Dial(rabbitURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to connect to RabbitMQ:", err)
|
fmt.Println("Failed to connect to RabbitMQ:", err)
|
||||||
@@ -162,10 +161,10 @@ func consumeJobCompletions() {
|
|||||||
// Declare the job_completions queue
|
// Declare the job_completions queue
|
||||||
q, err := ch.QueueDeclare(
|
q, err := ch.QueueDeclare(
|
||||||
"job_completions",
|
"job_completions",
|
||||||
true, // durable
|
true,
|
||||||
false, // auto-delete
|
false,
|
||||||
false, // exclusive
|
false,
|
||||||
false, // no-wait
|
false,
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -176,9 +175,9 @@ func consumeJobCompletions() {
|
|||||||
|
|
||||||
// Set QoS to process one message at a time
|
// Set QoS to process one message at a time
|
||||||
err = ch.Qos(
|
err = ch.Qos(
|
||||||
1, // prefetch count
|
1,
|
||||||
0, // prefetch size
|
0,
|
||||||
false, // global
|
false,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to set QoS:", err)
|
fmt.Println("Failed to set QoS:", err)
|
||||||
@@ -188,11 +187,11 @@ func consumeJobCompletions() {
|
|||||||
|
|
||||||
msgs, err := ch.Consume(
|
msgs, err := ch.Consume(
|
||||||
q.Name,
|
q.Name,
|
||||||
"completion-consumer", // consumer tag
|
"completion-consumer",
|
||||||
false, // manual ack
|
false,
|
||||||
false, // not exclusive
|
false,
|
||||||
false, // no-local
|
false,
|
||||||
false, // no-wait
|
false,
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -201,7 +200,6 @@ func consumeJobCompletions() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start processing
|
|
||||||
fmt.Println("Job completion consumer started...")
|
fmt.Println("Job completion consumer started...")
|
||||||
for d := range msgs {
|
for d := range msgs {
|
||||||
var completionMsg JobCompletionMessage
|
var completionMsg JobCompletionMessage
|
||||||
@@ -211,18 +209,15 @@ func consumeJobCompletions() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the completion message
|
|
||||||
if err := processCompletionMessage(completionMsg); err != nil {
|
if err := processCompletionMessage(completionMsg); err != nil {
|
||||||
log.Printf("Failed to process completion message: %v", err)
|
log.Printf("Failed to process completion message: %v", err)
|
||||||
d.Nack(false, true) // requeue the message
|
d.Nack(false, true)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acknowledge successful processing
|
|
||||||
d.Ack(false)
|
d.Ack(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case the channel is closed for any reason, loop will reconnect
|
|
||||||
fmt.Println("Job completion consumer disconnected. Reconnecting...")
|
fmt.Println("Job completion consumer disconnected. Reconnecting...")
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user