@@ -13,7 +13,7 @@ import (
1313)
1414
1515func Script (script string , credentials * map [string ]string , variables common.Variables , timeout time.Duration ) string {
16- timeoutString := strconv .Itoa (int (timeout / time .Second ))
16+ timeoutString := strconv .Itoa (int (time .Now (). Add ( timeout ). Unix () ))
1717 if timeout <= 0 {
1818 timeoutString = "infinity"
1919 }
@@ -67,21 +67,27 @@ TPI_MACHINE_IDENTITY="$(uuidgen)"
6767TPI_LOG_DIRECTORY="$(mktemp --directory)"
6868TPI_DATA_DIRECTORY="/opt/task/directory"
6969
70+ TPI_START_COMMAND="/bin/bash -lc 'exec /usr/bin/tpi-task'"
71+ TPI_REMAINING_RUN_TIME=$((%s-$(date +%%s)))
72+ if (( TPI_REMAINING_RUN_TIME < 1 )); then
73+ TPI_START_COMMAND="/bin/bash -c 'sleep infinity'"
74+ TPI_REMAINING_RUN_TIME=1
75+ fi
76+
7077source /opt/task/credentials
7178
7279sudo tee /etc/systemd/system/tpi-task.service > /dev/null <<END
7380[Unit]
7481 After=default.target
7582[Service]
7683 Type=simple
77- ExecStart=-/bin/bash -lc 'exec /usr/bin/tpi-task'
84+ ExecStart=-$TPI_START_COMMAND
7885 ExecStop=/bin/bash -c 'source /opt/task/credentials; systemctl is-system-running | grep stopping || echo "{\\\\"result\\\\": \\\\"\$SERVICE_RESULT\\\\", \\\\"code\\\\": \\\\"\$EXIT_STATUS\\\\", \\\\"status\\\\": \\\\"\$EXIT_CODE\\\\"}" > "$TPI_LOG_DIRECTORY/status-$TPI_MACHINE_IDENTITY" && RCLONE_CONFIG= rclone copy "$TPI_LOG_DIRECTORY" "\$RCLONE_REMOTE/reports"'
7986 ExecStopPost=/usr/bin/tpi-task-shutdown
8087 Environment=HOME=/root
8188 EnvironmentFile=/opt/task/variables
8289 WorkingDirectory=/opt/task/directory
83- TimeoutStartSec=%s
84- TimeoutStopSec=infinity
90+ RuntimeMaxSec=$TPI_REMAINING_RUN_TIME
8591[Install]
8692 WantedBy=default.target
8793END
0 commit comments