Skip to content

Commit 3a75a13

Browse files
committed
fix(server): remediate race condition on boot
1 parent d131f92 commit 3a75a13

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

server.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@ const firebaseConfig = {
1919

2020
const fbApp = Firebase.initializeApp(firebaseConfig);
2121

22-
// Authenticate server as a regular user.
23-
if (serverEmail && serverPassword) {
24-
Firebase.auth()
25-
.signInWithEmailAndPassword(serverEmail, serverPassword)
26-
.then(() => {
27-
console.log('Server authenticated with Firebase');
28-
})
29-
.catch(error => {
30-
console.error('Server authentication failed:', error);
31-
});
32-
}
33-
3422
var kongtrackr = require('./server/batch');
3523
// var algoliaIndices = require('./server/algoliaUpdate');
3624

@@ -45,10 +33,30 @@ app.get('/*', function(req, res) {
4533
app.listen(process.env.PORT || 8080);
4634
console.log(`Kongtrackr server started on ${process.env.PORT || 8080}.`);
4735

48-
kongtrackr.runBatch();
49-
// algoliaIndices.watchData();
50-
51-
// Batch schedule
52-
var timer = every(30, 'minutes', function() {
36+
// Function to start batch operations
37+
function startBatchOperations() {
5338
kongtrackr.runBatch();
54-
});
39+
// algoliaIndices.watchData();
40+
41+
// Batch schedule
42+
var timer = every(30, 'minutes', function() {
43+
kongtrackr.runBatch();
44+
});
45+
}
46+
47+
// Authenticate server as a regular user.
48+
if (serverEmail && serverPassword) {
49+
Firebase.auth()
50+
.signInWithEmailAndPassword(serverEmail, serverPassword)
51+
.then(() => {
52+
console.log('Server authenticated with Firebase');
53+
startBatchOperations();
54+
})
55+
.catch(error => {
56+
console.error('Server authentication failed:', error);
57+
// Don't start batch operations if auth fails
58+
});
59+
} else {
60+
// If no credentials, start batch operations anyway (for backward compatibility)
61+
startBatchOperations();
62+
}

0 commit comments

Comments
 (0)