Skip to content

Commit

Permalink
Fix History Service Database Connection Issue
Browse files Browse the repository at this point in the history
- As mySQL Database Container takes time to initially load
  • Loading branch information
Jai2501 committed Nov 13, 2023
1 parent fa95b17 commit d448088
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
49 changes: 37 additions & 12 deletions HistoryService/Controllers/HistoryDatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,41 @@ const TABLE_NAME = config.tableName;
var pool = null;

async function connectToDatabase() {
// Waiting for SQL Container to be up
await new Promise((r) => setTimeout(r, 10000));

pool = mySql
.createPool({
host: config.databaseUrl,
port: config.databasePort,
user: "root",
password: "",
// database: DATABASE_NAME,
})
.promise();
let connected = false;
const maxRetries = 10;
const retryInterval = 3000; // 3 seconds

for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
pool = mySql
.createPool({
host: config.databaseUrl,
port: config.databasePort,
user: "root",
password: "",
})
.promise();

console.log(`Attempting to connect to database (Attempt ${attempt})...`);
await pool.query(`USE ${DATABASE_NAME};`); // Simple query to test connection
console.log("Connected to database.");
connected = true;
break;
} catch (error) {
console.error(
`Database connection failed (Attempt ${attempt}):`,
error.message
);
if (attempt < maxRetries) {
console.log(`Retrying in ${retryInterval / 1000} seconds...`);
await new Promise((resolve) => setTimeout(resolve, retryInterval));
}
}
}

if (!connected) {
throw new Error("Failed to connect to database after multiple attempts.");
}

await setUpDatabase();
}
Expand Down Expand Up @@ -53,6 +76,8 @@ async function setUpDatabase() {
);

console.log(createTableResult);

console.log("History Database Set Up!");
}

async function getAttemptDetailsFromDatabase(userId) {
Expand Down
2 changes: 1 addition & 1 deletion HistoryService/Controllers/HistoryRoutesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getAttemptDetails = async (req, res, next) => {
if (result[0].length === 0) {
res.status(404).json({
message: "NO HISTORY DATA FOUND: GET USER ATTEMPT HISTORY",
result: [0],
result: result[0],
});

return;
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
## GPT Service Build and Test

![Communication Service Build and Test](https://github.com/CS3219-AY2324S1/ay2324s1-course-assessment-g16/actions/workflows/build_and_test_master_gpt_service.yaml/badge.svg)

## History Service Build and Test

![Communication Service Build and Test](https://github.com/CS3219-AY2324S1/ay2324s1-course-assessment-g16/actions/workflows/build_and_test_master_history_service.yaml/badge.svg)

0 comments on commit d448088

Please sign in to comment.