I'm trying to set up my Azure pipeline to automatically fail a job if it remains in the queue for more than 5 minutes. I attempted to use the `timeoutInMinutes` argument, but it didn't work as expected. Does anyone have a solution for implementing this logic? I'd appreciate any advice!
1 Answer
Azure Pipelines doesn't directly support failing a job based on how long it's in the queue. The `timeoutInMinutes` setting only applies once the job starts running. Instead, you could create a pre-job check using a script in the initial stage. This script would compare the current time to when the job entered the queue. You might use a setup like this:
```yaml
jobs:
- job: check_queue_time
pool:
vmImage: 'ubuntu-latest'
steps:
- bash: |
queuedTime=$(System.PullRequest.SourceCommitTimestamp)
now=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
diff=$(( $(date -d "$now" +%s) - $(date -d "$queuedTime" +%s) ))
echo "Queue duration: $diff seconds"
if [ $diff -gt 300 ]; then
echo "##vso[task.logissue type=error]Queued for too long. Failing."
exit 1
fi
displayName: 'Check Queue Time'
```
This assumes you can reliably access the queued time from an environment variable or the API. If that doesn't work, you might need to retrieve build metadata via Azure DevOps REST API. Let me know if you need help with that!
I appreciate the suggestion! Unfortunately, the problem is that my Job_1 runs on a self-hosted VM and can sometimes sit in the queue for upwards of 30 minutes. What I'm really hoping to do is fail Job_1 if it's queued for more than 5 minutes and then trigger Job_2 on Azure-hosted VMs. Is there a way to set this up?