TheNetsky / Microsoft-Rewards-Script

Automated Microsoft Rewards script, using TypeScript, Cheerio and Playwright.
165 stars 38 forks source link

Testing Docker #86

Closed mgrimace closed 3 months ago

mgrimace commented 5 months ago

Hello, I figured I'd contribute by testing out the docker implementation. tl;dr - Initial run has no issues(!), testing scheduled runs next.

Here's the steps I've taken in case anyone more knowledgable can test/contribute/make suggestions.

System

Headless Debian 12 server, running on a Proxmox LXC

Steps

Build and run the docker image

Schedule daily runs

This is where I'm less certain as to best method to do this, presently:

#!/bin/bash

MINWAIT=$((5*60))
MAXWAIT=$((50*60))
SLEEPTIME=$((MINWAIT+RANDOM % (MAXWAIT-MINWAIT)))
echo "sleeping for "$((SLEEPTIME / 60))"m ("$SLEEPTIME"s) ..." 
sleep $SLEEPTIME
echo "starting container"
docker start netsky
30 5 * * * /bin/bash /root/Microsoft-Rewards-Script/daily.sh

Results

I'll report back with results here. I'm more comfortable using docker compose, so this is new territory for me. Suggestions welcome.

mgrimace commented 3 months ago

Please note that this error only occurs during scheduled runs. If I choose to execute immediately when running Docker, this error will not occur.

Strange, no, my scheduled runs are working as expected. It seems like its not keeping playwright in the container cache. If it’s running on a VM or LXC maybe make sure it has enough space (e.g., 8 gb locally). I use a proxmox debian12 LXC with 2 vCPU, and 2048 ram (it idles at 0 cpu, but it’s nice to give it some extra CPU and RAM when it runs).

Troubleshooting steps:

The npx playwright install line is around line 32 in the dockerfile so it should have installed. Maybe try manually checking:

In terminal, with the container netsky running, use docker exec -it netsky bash. This should let you browse within the container using terminal commands. Try cd /root/.cache/ms-playwright then ls and you should see something like chromium-1117 in there.

If you don’t, it isn’t being installed for some reason, or possibly there’s some kind of user/permission thing going on. If you’re on like unraid or something, you likely have to set the proper permissions PUID/GUID in your compose file (e.g., if you’re a non-root user).

This all assumes you’re using the basic docker steps with no major modifications. I haven’t tested volume mapping the config or account files just yet, but I don’t think that would be the problem.

I can’t think of anything else, if you can, try a clean install, check free space assigned, and check permissions.

TheNetsky commented 3 months ago

I'm waiting for you to confirm your PR is done before merging, since I read somewhere that "updateConfig.js" is redundant?

mgrimace commented 3 months ago

I'm waiting for you to confirm your PR is done before merging, since I read somewhere that "updateConfig.js" is redundant?

I don't plan to make any more changes to the PR. IMO updateConfig.js is not redudant.

@OrangeChu suggested using volume mapping in the compose file, so you can put config.json and accounts.json somewhere else on your system. That's good for a static config.json that can stay locally in a persistent data folder. I plan to do this but haven't yet.

updateConfig.js updates the container's dist/config.json with changes specified in the compose.yaml using environmental variables. This is useful (to me) to make quick test-changes. For exampe, I added the line - DO_MORE_PROMOTIONS=false to my compose file re. issue #111 and quickly restarted the container.

IMHO it's good to have both options. They are not mutually exclusive, and it's the only way I can figure out to use docker env vars and have them work properly.

No further changes are needed for volume mapping. If you want to add it to your compose, add the volumes line:

services:
  microsoft-rewards-script:
    build: .
    container_name: netsky
    environment:
      - TZ=America/Toronto #change to your local timezone
      - NODE_ENV=production 
      - HEADLESS=true #do not change
      - DO_MORE_PROMOTIONS=false #if you get sb_form_q errors
      ### Customize your run schedule, default 5:00 am and 11:00 am, use crontab.guru for formatting
      - CRON_START_TIME=0 5,11 * * *
      ### Run on start, set to false to only run the script per the cron schedule
      - RUN_ON_START=true 
    volumes: #optional, add a path to a persistent config.json and account.json on your system
      - /path/to/accounts.json:/usr/src/microsoft-rewards-script/dist/accounts.json
      - /path/to/config.json:/usr/src/microsoft-rewards-script/dist/config.json
    restart: unless-stopped

tl;dr I wouldn't remove updateConfig.js, it adds some nice functionality to be able to make quick changes to the config.

mgrimace commented 3 months ago

Tested volume mapping config and accounts, and working just fine as-is. Clean Debian 12 server, only Docker CE installed. Source code and compose.yaml in /docker/compose/netsky/ and config/accounts in /docker/appdata/netsky/

compose.yaml

services:
  microsoft-rewards-script:
    build: .
    container_name: netsky
    environment:
      - TZ=America/Toronto 
      - NODE_ENV=production 
      - HEADLESS=true 
      - DO_MORE_PROMOTIONS=false
      - CRON_START_TIME=0 5,11 * * *
      - RUN_ON_START=true 
    volumes:
      - /root/docker/appdata/netsky/accounts.json:/usr/src/microsoft-rewards-script/dist/accounts.json
      - /root/docker/appdata/netsky/config.json:/usr/src/microsoft-rewards-script/dist/config.json
    restart: unless-stopped

Note the doMorePromotions over-ride in the compose, which fixed issue #111 for me.

my static config.json

{
  "baseURL": "https://rewards.bing.com",
  "sessionPath": "sessions",
  "headless": true,
  "runOnZeroPoints": false,
  "clusters": 2,
  "saveFingerprint": false,
  "workers": {
    "doDailySet": true,
    "doMorePromotions": true,
    "doPunchCards": true,
    "doDesktopSearch": true,
    "doMobileSearch": true
  },
  "globalTimeout": 30000,
  "searchSettings": {
    "useGeoLocaleQueries": true,
    "scrollRandomResults": true,
    "clickRandomResults": true,
    "searchDelay": {
      "min": 180000,
      "max": 270000
    },
    "retryMobileSearch": true
  },
  "webhook": {
    "enabled": false,
    "url": ""
  },
  "cronStartTime": "0 5,11 * * *"
}

Tested initial run, scheduled runs, all working as expected. No changes required for volume mapping support, works 'out-of-the-box'.

mgrimace commented 2 months ago

FYI I've made a small change to my compose.yaml file to limit the container to 2 gb memory.

I'll tinker around with how much mem to allocate for the script, but this should keep the mem use from potentially spiraling out if the script encounters an issue (e.g., occasional activity error, etc). My understanding is that if the container exceeds the allocated ram, it'll restart per the restart policy (which, we set as automatic). Browser-related issues do tend to manifest in mem-use, so this should more or less function as a sort of 'auto-restart', especially with the script set to RUN_ON_START.

The relevant addition is the line starting deploy below.

services:
  microsoft-rewards-script:
    build: .
    container_name: netsky
    environment:
      - TZ=America/Toronto 
      - NODE_ENV=production 
      - HEADLESS=true 
      - DO_MORE_PROMOTIONS=false
      - CRON_START_TIME=0 5,11 * * *
      - RUN_ON_START=true 
    volumes:
      - /root/docker/appdata/netsky/accounts.json:/usr/src/microsoft-rewards-script/dist/accounts.json
      - /root/docker/appdata/netsky/config.json:/usr/src/microsoft-rewards-script/dist/config.json
    deploy:
      resources:
        limits:
          memory: 2048M    
    restart: unless-stopped