From 948341344f6364ab51e7bf93b08021aa45af026b Mon Sep 17 00:00:00 2001 From: CeciliaKelley33Mm Date: Fri, 11 Oct 2024 09:26:05 +0700 Subject: [PATCH] check time script instead of fixed hours hopefully this should work on date bridges since we use Date::getTime which gives a unix timestamp --- .github/workflows/runglobal.yml | 4 ++++ checkTime.js | 35 +++++++++++++++++++++++++++++++++ loop.sh | 21 +++++++++++++++----- 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 checkTime.js diff --git a/.github/workflows/runglobal.yml b/.github/workflows/runglobal.yml index a615768..a71df08 100644 --- a/.github/workflows/runglobal.yml +++ b/.github/workflows/runglobal.yml @@ -120,6 +120,10 @@ jobs: echo -e '\n====== Setting up and running the long-lived process ======\n' + cd /mnt/globalData + + wget https://raw.githubusercontent.com/CeciliaKelley33Mm/global/refs/heads/master/checkTime.js + wget https://raw.githubusercontent.com/CeciliaKelley33Mm/global/refs/heads/master/loop.sh chmod +x loop.sh bash loop.sh ${{ inputs.runNext }} $GH_TOKEN global runglobal.yml master global $WEBHOOK_URL true ${{ inputs.oldTailscaleHostname }} diff --git a/checkTime.js b/checkTime.js new file mode 100644 index 0000000..ce0cd99 --- /dev/null +++ b/checkTime.js @@ -0,0 +1,35 @@ +const process = require('process') + +if (process.argv.length < 3) { + console.error('Invalid format. Expected HH:MM') + process.exit(69) +} + +const startTimeString = process.argv[2] +const [startHours, startMinutes] = startTimeString.split(':').map(Number) + +if ( + isNaN(startHours) || isNaN(startMinutes) || + startHours > 23 || startMinutes > 59 +) { + console.error('Invalid time format. Expected HH:MM format WITH VALID TIME') + process.exit(69) +} + +const startTime = new Date() +startTime.setHours(startHours, startMinutes, 0, 0) + +const targetTime = new Date(startTime.getTime() + 5 * 60 * 60 * 1000) // 5 hours after startTime +targetTime.setSeconds(0) // fixes the precise time, we don't want them +targetTime.setMilliseconds(0) + +const currentTime = new Date() +currentTime.setSeconds(0) +currentTime.setMilliseconds(0) + +if (currentTime.getTime() === targetTime.getTime()) { + console.log('[checkTime] time matches, now exiting with status 0 to make another runtime') + process.exit(0) +} else { + process.exit(1) +} diff --git a/loop.sh b/loop.sh index d7d90b7..c746d06 100755 --- a/loop.sh +++ b/loop.sh @@ -1,5 +1,7 @@ #!/bin/bash +START_TIME=$(date +"%H:%M:%S") # the checkTime.js script uses local time + GH_TOKEN=$2 REPO=$3 @@ -58,7 +60,10 @@ requestWebhook() { } check() { - currentTime=$(TZ=Etc/UTC date +"%H-%M") + # currentTime=$(TZ=Etc/UTC date +"%H-%M") + + ### the following pattern is now deprecated. i am now using checkTime.js + ### which adds the start time by 5 hours # this is the pattern. the old machine will activate the new machine 1 hour before it terminates @@ -82,11 +87,15 @@ check() { # 5 00:00 ACTIVATE 1 # 5 01:50 TERMINATE - targetTimes=("00-00" "05-50" "10-50" "15-50" "20-50") + # targetTimes=("00-00" "05-50" "10-50" "15-50" "20-50") - for target in "${targetTimes[@]}"; do - if [[ "$currentTime" != "$target" ]]; then continue; fi + # for target in "${targetTimes[@]}"; do + # if [[ "$currentTime" != "$target" ]]; then continue; fi + node checkTime.js $START_TIME + exitCode=$? + + if [ $exitCode -eq 0 ]; then alreadyDone=1 hostname="old-$NAME-$RANDOM" @@ -133,7 +142,9 @@ check() { fi eval "$command" - done + fi + + # done } if [ "$1" == "true" ]; then