Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add persistence to docker #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM node

ENV PERSISTENCE_LOCATION=/data
EXPOSE 3000

RUN groupadd osweather && useradd --no-log-init -m -g osweather osweather
RUN groupadd osweather && useradd --no-log-init -m -g osweather osweather && \
mkdir /data && \
chown osweather:osweather /data

USER osweather
VOLUME /data

ADD --chown=osweather:osweather . weather

Expand Down
2 changes: 1 addition & 1 deletion routes/geocoders/Geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CodedError, ErrorCode } from "../../errors";

export abstract class Geocoder {

private static cacheFile: string = __dirname + "/../../../geocoderCache.json";
private static cacheFile: string = (process.env.PERSISTENCE_LOCATION || __dirname + "/../../..") + "/geocoderCache.json";

private cache: Map<string, GeoCoordinates>;

Expand Down
11 changes: 6 additions & 5 deletions routes/weatherProviders/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export const captureWUStream = async function( req: express.Request, res: expres
};

export default class LocalWeatherProvider extends WeatherProvider {

public static observationsFile: string = ( process.env.PERSISTENCE_LOCATION ? process.env.PERSISTENCE_LOCATION + "/observations.json" : "observations.json");

public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
queue = queue.filter( obs => moment().unix() - obs.timestamp < 24*60*60 );

Expand Down Expand Up @@ -126,16 +127,16 @@ export default class LocalWeatherProvider extends WeatherProvider {
function saveQueue() {
queue = queue.filter( obs => moment().unix() - obs.timestamp < 24*60*60 );
try {
fs.writeFileSync( "observations.json" , JSON.stringify( queue ), "utf8" );
fs.writeFileSync( LocalWeatherProvider.observationsFile , JSON.stringify( queue ), "utf8" );
} catch ( err ) {
console.error( "Error saving historical observations to local storage.", err );
}
}

if ( process.env.WEATHER_PROVIDER === "local" && process.env.LOCAL_PERSISTENCE ) {
if ( fs.existsSync( "observations.json" ) ) {
if ( process.env.WEATHER_PROVIDER === "local" && (process.env.LOCAL_PERSISTENCE || process.env.PERSISTENCE_LOCATION) ) {
if ( fs.existsSync( LocalWeatherProvider.observationsFile ) ) {
try {
queue = JSON.parse( fs.readFileSync( "observations.json", "utf8" ) );
queue = JSON.parse( fs.readFileSync( LocalWeatherProvider.observationsFile, "utf8" ) );
queue = queue.filter( obs => moment().unix() - obs.timestamp < 24*60*60 );
} catch ( err ) {
console.error( "Error reading historical observations from local storage.", err );
Expand Down