@@ -13,7 +13,7 @@ import (
13
13
)
14
14
15
15
func 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 () ))
17
17
if timeout <= 0 {
18
18
timeoutString = "infinity"
19
19
}
@@ -67,21 +67,27 @@ TPI_MACHINE_IDENTITY="$(uuidgen)"
67
67
TPI_LOG_DIRECTORY="$(mktemp --directory)"
68
68
TPI_DATA_DIRECTORY="/opt/task/directory"
69
69
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
+
70
77
source /opt/task/credentials
71
78
72
79
sudo tee /etc/systemd/system/tpi-task.service > /dev/null <<END
73
80
[Unit]
74
81
After=default.target
75
82
[Service]
76
83
Type=simple
77
- ExecStart=-/bin/bash -lc 'exec /usr/bin/tpi-task'
84
+ ExecStart=-$TPI_START_COMMAND
78
85
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"'
79
86
ExecStopPost=/usr/bin/tpi-task-shutdown
80
87
Environment=HOME=/root
81
88
EnvironmentFile=/opt/task/variables
82
89
WorkingDirectory=/opt/task/directory
83
- TimeoutStartSec=%s
84
- TimeoutStopSec=infinity
90
+ RuntimeMaxSec=$TPI_REMAINING_RUN_TIME
85
91
[Install]
86
92
WantedBy=default.target
87
93
END
0 commit comments