管理后台作业的运行时间.如果 x 秒后未完成则超时, [英] Managing the running time of background jobs. Timing out if not completed after x seconds,

查看:70
本文介绍了管理后台作业的运行时间.如果 x 秒后未完成则超时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的后台作业计时(从 start-job 开始)并在 x 秒后超时.然而,我发现很难跟踪每个单独作业的运行时间(我正在运行大约 400 个作业).

我希望有一种方法可以让作业超时并将其设置为 failed 如果在 X 秒内没有 completed,但我没有找到超时参数.>

跟踪作业的单个运行时间的好方法是什么?

我想我可以创建一个包含每个作业的开始时间和作业 ID 的哈希表,并检查 running 状态并进行手动超时,但这听起来有点发明轮子".有什么想法吗?

编辑感谢大家对这个话题的富有成果的讨论和巨大的启发!

解决方案

您可以使用计时器的哈希表:

 $jobtimer = @{}foreach ($jobs 中的 $job){start-job -name $job -ScriptBlock {scriptblock 命令}$jobtimer[$job] = [System.Diagnostics.Stopwatch]::startnew()}

每个作业的运行时间会在 $jobtimer[$job].elapsed

I would like to time my background jobs (started with start-job) and time them out after x seconds. I find it hard however to keep track of the running time on each separate job (I am running aprox 400 jobs).

I wish there was a way to time out the job and set it to failed if not completed in X seconds, but I find no timeout-parameter.

What would be a good way to track the individual run-time of the jobs?

I guess I could create a hashtable with start-time of each job and the job-id and check against the running state and do a manual timeout, but that sounds kinda "inventing the wheel". Any ideas?

Edit Thank you everyone for a fruitful discussion and great inspiration on this topic!

解决方案

You can use a hash table of timers:

 $jobtimer = @{}

 foreach ($job in $jobs){
   start-job -name $job -ScriptBlock {scriptblock commands}
   $jobtimer[$job] = [System.Diagnostics.Stopwatch]::startnew()
   }

The running time of each job will be in $jobtimer[$job].elapsed

这篇关于管理后台作业的运行时间.如果 x 秒后未完成则超时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆