Skip to content

fix: improve DB retry logic and prevent install loop + OOM handling (…#1610

Open
johnny603 wants to merge 2 commits into
OpenEnergyDashboard:developmentfrom
johnny603:fix/retryLogicHandling/1608
Open

fix: improve DB retry logic and prevent install loop + OOM handling (…#1610
johnny603 wants to merge 2 commits into
OpenEnergyDashboard:developmentfrom
johnny603:fix/retryLogicHandling/1608

Conversation

@johnny603
Copy link
Copy Markdown

@johnny603 johnny603 commented Apr 20, 2026

Description

This change addresses non-deterministic database initialization during Docker startup that results in repeated PromiseRejectionHandledWarning logs, overlapping retry behavior, and inconsistent startup state.

The root issue is caused by two independent retry/initialization systems running concurrently:

installOED.sh retrying npm run createdb
Node.js createDB.js performing async schema initialization without deterministic process termination

Together, these create race conditions, repeated schema execution attempts, and noisy startup logs.

This change improves startup reliability by clarifying DB initialization flow and reducing overlapping execution paths.

Fixes #1608

Type of change

  • This change requires a documentation update
    Checklist
  • I have followed the OED pull request
    ideas
  • I have removed text in ( ) from the issue request
  • You acknowledge that every person contributing to this work has signed the OED Contributing License Agreement
    and each author is listed in the Description section.

Limitations

This fix improves initialization determinism and reduces retry-related race conditions, but does not fully eliminate underlying circular dependency complexity in the model layer (models/database.js).

Further improvements may include:

Refactoring schema initialization into a single orchestration layer
Reducing circular dependencies between model modules
Migrating DB initialization to a fully centralized startup service

@huss
Copy link
Copy Markdown
Member

huss commented Apr 21, 2026

@johnny603 You have these ideas in the description:

Refactoring schema initialization into a single orchestration layer
Reducing circular dependencies between model modules
Migrating DB initialization to a fully centralized startup service

I think it would be valuable for OED to look into these. Would you like to open issue(s) about this? Any additional information on your thoughts are also very welcome.

Copy link
Copy Markdown
Member

@huss huss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnny603 Thank you for thinking about this problem and addressing it. I have not done a complete review but have looked at this some. I probably have other comments but wanted to start here to begin the conversation. I've made some comments on this file to consider. In the bigger picture, where I try to start OED using docker compose up, it fails to start and goes into a loop where it keeps reporting this error:

Warning: Accessing non-existent property 'sqlFile' of module exports inside circular dependency

I'm assuming you don't see this. Do you have thoughts on what is happening?

Please let me know if anything is not clear, any thoughts or questions.

Comment thread docker-compose.yml
context: ./
dockerfile: ./containers/web/Dockerfile

environment:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering about moving the environment variable location in this file. Is this your preference, a good idea or something else. I'm thinking about this because the OED site installation directions talk about these so moving them does have some pain threshold.

Comment thread src/scripts/installOED.sh
@@ -1,198 +1,120 @@
#!/bin/bash
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright notice was removed. OED wants that in every file that is possible.

Also, you removed or modified a comments in this file. I'm wondering why since some may have changed the meaning or removed comments designed to describe what was intended.

Comment thread src/scripts/installOED.sh
exit 1
esac
case "$1" in
--production) production=yes ;;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indenting was changed from tabs to spaces. While OED is not always consistent, esp. in script files, the project does prefer tab indenting when that is possible. Is there are reason not to use tab indenting?

Comment thread src/scripts/installOED.sh
esac
case "$1" in
--production) production=yes ;;
--nostart) dostart=no ;;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While your new formatting is okay, OED prefers each line of code on a separate line and having items inside a loop/logic indented on separate lines (so it is clear the association).

Comment thread src/scripts/installOED.sh
# ----------------------------------------------------
# Load env
# ----------------------------------------------------
[ -f ".env" ] && source .env
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is the same, OED prefers the explicit if/else to make the logic clear. This is esp. true as we work with a lot of student developers who may not be a familiar with scripting syntax.

Comment thread src/scripts/installOED.sh
# ----------------------------------------------------
# NPM install
# ----------------------------------------------------
if [ "$keep_node_modules" = "yes" ]; then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why the logic about changed package files was removed. It was designed only do the install when package files were changed since OED only wants an update then and not every time it is run to keep all the file standard across implementations. This seems to run every time. Thoughts?

Comment thread src/scripts/installOED.sh
exit 2
fi
fi
echo "Running database creation..."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script was using printf throughout to be consistent. It also did extra lines to make it easier to see certain items-steps. Could you explain why this was changed?

Comment thread src/scripts/installOED.sh
# ----------------------------------------------------
if [ "$production" = "yes" ] || [ "$OED_PRODUCTION" = "yes" ]; then
npm run webpack:build
elif [ "$dostart" = "no" ]; then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You systematically changed the == to = for string comparisons. While they are the aliases for each other and = is POSIX compliant (we are tied to bash so less of an issue), the == was used to be explicit it is a string comparison. The reuse of = in other languages has led to many bugs (such as in C++ where you assign instead of just compare) so it was felt that being explicit was better here. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-deterministic database initialization caused by overlapping retry logic and async lifecycle handling

2 participants