oldmoe / litestack

MIT License
1.11k stars 60 forks source link

Race condition between env and defaults when using LITESTACK_DATA_PATH with dotenv gem #91

Open silva96 opened 8 months ago

silva96 commented 8 months ago

If I add LITESTACK_DATA_PATH="./storage" to my .env it is picked up after the litestack initializes some classes so it uses the default path to create the databases, but later on, it tries to find databases files in LITESTACK_DATA_PATH (i think)

See this screenshot where I paused execution.

CleanShot 2024-01-29 at 23 37 12@2x

This leads to the app having sqlite3 files in 2 different parts:

CleanShot 2024-01-29 at 23 59 45@2x

rossta commented 8 months ago

I have experienced the same behavior. Litestack should probably provide a runtime configuration code path; currently, the LITESTACK_DATA_PATH and other configuration settings are loaded at boot time.

My workaround in Rails is to load dotenv at the top of application.rb, per the dotenv README.

silva96 commented 8 months ago

That's a good one @rossta , with that comment in the readme at least, it would work for most users in the future.

christopher-b commented 3 months ago

@rossta's suggestion seems to work for some modules (database), but not all (queue, metrics). It seems that a more reliable solution is to provide hard-coded paths in the configs for these modules:

# config/litemetric.yml:
development:
  path: ./storage/litestack/development/metrics.sqlite3
test:
  path: ./storage/litestack/test/metrics.sqlite3
production:
  path: ./storage/litestack/production/metrics.sqlite3
# config/litequeue.yml
development:
  path: ./storage/litestack/development/queue.sqlite3
test:
  path: ./storage/litestack/test/queue.sqlite3
production:
  path: ./storage/litestack/production/queue.sqlite3

It also seems that these database files are being initialized even if the modules are not being used in the application. It would be nice to only have them initialized if they are needed.