check time script instead of fixed hours

hopefully this should work on date bridges since we use Date::getTime which gives a unix timestamp
This commit is contained in:
CeciliaKelley33Mm 2024-10-11 09:26:05 +07:00
parent e97125d49a
commit 948341344f
3 changed files with 55 additions and 5 deletions

View file

@ -120,6 +120,10 @@ jobs:
echo -e '\n====== Setting up and running the long-lived process ======\n' 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 wget https://raw.githubusercontent.com/CeciliaKelley33Mm/global/refs/heads/master/loop.sh
chmod +x loop.sh chmod +x loop.sh
bash loop.sh ${{ inputs.runNext }} $GH_TOKEN global runglobal.yml master global $WEBHOOK_URL true ${{ inputs.oldTailscaleHostname }} bash loop.sh ${{ inputs.runNext }} $GH_TOKEN global runglobal.yml master global $WEBHOOK_URL true ${{ inputs.oldTailscaleHostname }}

35
checkTime.js Normal file
View file

@ -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)
}

21
loop.sh
View file

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
START_TIME=$(date +"%H:%M:%S") # the checkTime.js script uses local time
GH_TOKEN=$2 GH_TOKEN=$2
REPO=$3 REPO=$3
@ -58,7 +60,10 @@ requestWebhook() {
} }
check() { 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 # 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 00:00 ACTIVATE 1
# 5 01:50 TERMINATE # 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 # for target in "${targetTimes[@]}"; do
if [[ "$currentTime" != "$target" ]]; then continue; fi # if [[ "$currentTime" != "$target" ]]; then continue; fi
node checkTime.js $START_TIME
exitCode=$?
if [ $exitCode -eq 0 ]; then
alreadyDone=1 alreadyDone=1
hostname="old-$NAME-$RANDOM" hostname="old-$NAME-$RANDOM"
@ -133,7 +142,9 @@ check() {
fi fi
eval "$command" eval "$command"
done fi
# done
} }
if [ "$1" == "true" ]; then if [ "$1" == "true" ]; then