mirror of
https://github.com/TheNetsky/Microsoft-Rewards-Script.git
synced 2026-01-10 18:36:17 +00:00
Docker with env var support and containerized cron scheduling (#107)
* Script will run on container start, and continue to run daily at 5:00 am. The goal of this update was to add in-built cronjob support so that the script runs immediately on container start, then will schedule the script to run the daily at 5:00 am without requiring the user to manually create a cronjob. After starting the container and completing the first run, the container should remain active (running) but idle until the next scheduled run. Ideally, the next step will be to include support for environmental variables that will allow the user to customize when the script scheduled runs occur within the docker compose.yaml. * added --build to docker compose command instructions building/rebuilding the image (and the script) may reduce problems if folks make changes to their configs * Updated the crontab default to run twice daily by default, 5am and 11am * Update crontab Newline needed * Environmental variable support, custom scheduling This is a massive update that includes docker environmental variable support for all config options. If specified, the custom env var value will supersede the default values in the config.json. You do not need to change the values in the config.json if you set a custom env var. For example, if you have headless=false in the config.json, but set it as true in the compose.yaml, it will run as HEADLESS=true. If you do not specify a particular env var, the script will automatically use the default value. Please test and contribute wherever possible! * Fix scheduled runs, tidied up readme * timezone support Minor adjustment to attempt to fix timezone env var support * Run on start, npm script env Hopefully two fixes here, the container should again, by default, run on start, then continue per schedule. This defaults to true; however, can be optionally over-ridden in the compose.yaml with the env var RUN_ON_START=false. If set to false, the script should only run on the specified cron schedule. I also passed the TZ write into the npm start command, rather than just the container overall so it should show the proper time. * Keep docker running after run_on_start to wait for cron Accidentally set it up like a run_once, this should keep the container active after run_on_start to wait for the next schedule. * improve order of cmd This should (1) set time zone for script, (2) run once if set, then (3) idle for scheduled cron job. It was a little hacky before, this should be tidier. * updated compose to auto-restart container Noticed the container was exited this am, likely after automated updated to system, adding restart: unless-stopped to compose should keep container running in such circumstances. * Update env vars in readme
This commit is contained in:
23
Dockerfile
23
Dockerfile
@@ -4,21 +4,12 @@ FROM node:18
|
||||
# Set the working directory in the container
|
||||
WORKDIR /usr/src/microsoft-rewards-script
|
||||
|
||||
# Install jq
|
||||
RUN apt-get update && apt-get install -y jq
|
||||
|
||||
# Install jq, cron, and gettext-base
|
||||
RUN apt-get update && apt-get install -y jq cron gettext-base
|
||||
|
||||
# Copy all files to the working directory
|
||||
COPY . .
|
||||
|
||||
# Check if "headless" is set to "true" in the config.json file
|
||||
# DELETE BELOW IF YOU WANT TO RUN THE DOCKER SCRIPT HEADFULL!
|
||||
RUN HEADLESS=$(cat src/config.json | jq -r .headless) \
|
||||
&& if [ "$HEADLESS" != "true" ]; then \
|
||||
echo "Error: 'headless' in src/config.json is not true."; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
# Install dependencies including Playwright
|
||||
RUN apt-get install -y \
|
||||
xvfb \
|
||||
@@ -39,5 +30,11 @@ RUN npm run build
|
||||
# Install playwright chromium
|
||||
RUN npx playwright install chromium
|
||||
|
||||
# Define the command to run your application
|
||||
CMD ["npm", "start"]
|
||||
# Copy cron file to cron directory
|
||||
COPY src/crontab.template /etc/cron.d/microsoft-rewards-cron.template
|
||||
|
||||
# Create the log file to be able to run tail
|
||||
RUN touch /var/log/cron.log
|
||||
|
||||
# Define the command to run your application with cron optionally
|
||||
CMD sh -c 'echo "$TZ" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata && if [ "$RUN_ON_START" = "true" ]; then npm start; fi && envsubst < /etc/cron.d/microsoft-rewards-cron.template > /etc/cron.d/microsoft-rewards-cron && crontab /etc/cron.d/microsoft-rewards-cron && cron && tail -f /var/log/cron.log'
|
||||
Reference in New Issue
Block a user