From 37687b64c779595277f71ebee40a12e690deb22f Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Mon, 16 Feb 2026 16:37:35 -0800 Subject: [PATCH] Add in CronJob to update the Financial Data every 2 days --- .gitignore | 1 + WebServer/Program.cs | 48 +++++++++++++++++++++++++------------------- crontab.txt | 1 + dockerfile | 17 +++++++++++++++- entrypoint.sh | 7 +++++++ 5 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 crontab.txt create mode 100644 entrypoint.sh diff --git a/.gitignore b/.gitignore index f19e6673..9060a548 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ appdata.db __pycache__ obj bin +test.db !/WebServer/AIPython/python/bin \ No newline at end of file diff --git a/WebServer/Program.cs b/WebServer/Program.cs index d8626820..867a7961 100644 --- a/WebServer/Program.cs +++ b/WebServer/Program.cs @@ -2,8 +2,6 @@ using WebServer.Components; using PythonInterop; using DataBase; -var builder = WebApplication.CreateBuilder(args); - // Load the module in globally and use correct path for local or docker runners #if DEBUG AIModule interopModule = new AIModule(PythonPathBase: "/usr/", PythonVersion: "python3.11"); @@ -11,29 +9,37 @@ var builder = WebApplication.CreateBuilder(args); AIModule interopModule = new AIModule(); #endif -// Add services to the container. -builder.Services.AddRazorComponents().AddInteractiveServerComponents(); +if (args.Contains("Pull-Stock-Data")) { + interopModule.PullData(); +} else { + // Create the webapp + var builder = WebApplication.CreateBuilder(args); -// Insert the DB driver for Dependency Injection -builder.Services.AddSingleton(); + // Add services to the container. + builder.Services.AddRazorComponents().AddInteractiveServerComponents(); -// Insert the python modlue for Dependency Injection -builder.Services.AddSingleton(interopModule); + // Insert the DB driver for Dependency Injection + builder.Services.AddSingleton(); -var app = builder.Build(); + // Insert the python modlue for Dependency Injection + builder.Services.AddSingleton(interopModule); -// Configure the HTTP request pipeline. -if (!app.Environment.IsDevelopment()) { - app.UseExceptionHandler("/Error", createScopeForErrors: true); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); -} + // Build the app + var app = builder.Build(); -app.UseHttpsRedirection(); + // Configure the HTTP request pipeline. + if (!app.Environment.IsDevelopment()) { + app.UseExceptionHandler("/Error", createScopeForErrors: true); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } -app.UseAntiforgery(); + // Conffigure policies + app.UseHttpsRedirection(); + app.UseAntiforgery(); + app.MapStaticAssets(); + app.MapRazorComponents().AddInteractiveServerRenderMode(); -app.MapStaticAssets(); -app.MapRazorComponents().AddInteractiveServerRenderMode(); - -app.Run(); + // Start the web app + app.Run(); +} \ No newline at end of file diff --git a/crontab.txt b/crontab.txt new file mode 100644 index 00000000..4be9a397 --- /dev/null +++ b/crontab.txt @@ -0,0 +1 @@ +0 0 */2 * * root /app/WebServer Pull-Stock-Data >> /var/log/cron.log 2>&1 diff --git a/dockerfile b/dockerfile index c4f75a28..d9974f10 100644 --- a/dockerfile +++ b/dockerfile @@ -37,6 +37,7 @@ RUN apt update && apt upgrade -y && apt install -y \ libbz2-dev \ wget \ ca-certificates \ + cron \ && rm -rf /var/lib/apt/lists/* # Download, extract, configure, and compile python 3.11 @@ -77,5 +78,19 @@ EXPOSE 5000 # Copy in the webserver COPY --from=build-Webserver /app/publish ./ +# Copy in cronjob +COPY crontab.txt /etc/cron.d/reload-finance-history + +# Copy in EntryPoint +COPY entrypoint.sh /entrypoint.sh + +# Give execution rights on the cronjob and entrypoint +RUN chmod 0644 /etc/cron.d/reload-finance-history && \ + chmod +x /entrypoint.sh && \ + touch /var/log/cron.log + +# Apply the cron job +RUN crontab /etc/cron.d/reload-finance-history + # Start the container -ENTRYPOINT ["dotnet", "WebServer.dll", "--url", "http://localhost:5000"] \ No newline at end of file +ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..b1d7dd4d --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Start the cron daemon in the background +service cron start + +# Run your main application (this keeps the container alive) +exec /app/WebServer \ No newline at end of file