SQL Server 代理作业超时

发布于 2024-08-08 09:40:17 字数 125 浏览 4 评论 0原文

我刚刚有一个计划的 SQL Server 作业运行时间比正常情况要长,我真的可以通过设置超时来在一定时间长度后停止它。

我可能对此有点盲目,但我似乎找不到一种为作业设置超时的方法。有谁知道该怎么做?

谢谢

I have just had a scheduled SQL Server job run for longer than normal, and I could really have done with having set a timeout to stop it after a certain length of time.

I might be being a bit blind on this, but I can't seem to find a way of setting a timeout for a job. Does anyone know the way to do it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2024-08-15 09:40:17

我们做了类似下面的代码作为夜间作业处理子系统的一部分 - 它比现实中的实际情况更复杂;例如,我们正在处理多个相互依赖的作业集,并从配置表中读取作业名称和超时值 - 但这捕获了这个想法:

    DECLARE @JobToRun NVARCHAR(128) = 'My Agent Job'
DECLARE @dtStart DATETIME = GETDATE(), @dtCurr DATETIME
DECLARE @ExecutionStatus INT, @LastRunOutcome INT, @MaxTimeExceeded BIT = 0
DECLARE @TimeoutMinutes INT = 180 

EXEC msdb.dbo.sp_start_job @JobToRun
SET @dtCurr = GETDATE()
WHILE 1=1
BEGIN
    WAITFOR DELAY '00:00:10'
    SELECT @ExecutionStatus=current_execution_status, @LastRunOutcome=last_run_outcome 
    FROM OPENQUERY(LocalServer, 'set fmtonly off; exec msdb.dbo.sp_help_job') where [name] = @JobToRun
    IF @ExecutionStatus <> 4
    BEGIN -- job is running or finishing (not idle)
        SET @dtCurr=GETDATE()
        IF DATEDIFF(mi, @dtStart, @dtCurr) > @TimeoutMinutes
        BEGIN   
            EXEC msdb.dbo.sp_stop_job @job_name=@JobToRun                   
            -- could log info, raise error, send email etc here
        END
        ELSE
        BEGIN
            CONTINUE
        END
    END
    IF @LastRunOutcome = 1  -- the job just finished with success flag
    BEGIN
        -- job succeeded, do whatever is needed here
        print 'job succeeded'                                   
    END

END

We do something like the code below as part of a nightly job processing subsystem - it is more complicated than this actually in reality; for example we are processing multiple interdependent sets of jobs, and read in job names and timeout values from configuration tables - but this captures the idea:

    DECLARE @JobToRun NVARCHAR(128) = 'My Agent Job'
DECLARE @dtStart DATETIME = GETDATE(), @dtCurr DATETIME
DECLARE @ExecutionStatus INT, @LastRunOutcome INT, @MaxTimeExceeded BIT = 0
DECLARE @TimeoutMinutes INT = 180 

EXEC msdb.dbo.sp_start_job @JobToRun
SET @dtCurr = GETDATE()
WHILE 1=1
BEGIN
    WAITFOR DELAY '00:00:10'
    SELECT @ExecutionStatus=current_execution_status, @LastRunOutcome=last_run_outcome 
    FROM OPENQUERY(LocalServer, 'set fmtonly off; exec msdb.dbo.sp_help_job') where [name] = @JobToRun
    IF @ExecutionStatus <> 4
    BEGIN -- job is running or finishing (not idle)
        SET @dtCurr=GETDATE()
        IF DATEDIFF(mi, @dtStart, @dtCurr) > @TimeoutMinutes
        BEGIN   
            EXEC msdb.dbo.sp_stop_job @job_name=@JobToRun                   
            -- could log info, raise error, send email etc here
        END
        ELSE
        BEGIN
            CONTINUE
        END
    END
    IF @LastRunOutcome = 1  -- the job just finished with success flag
    BEGIN
        -- job succeeded, do whatever is needed here
        print 'job succeeded'                                   
    END

END
圈圈圆圆圈圈 2024-08-15 09:40:17

这是一份什么样的工作?您可能需要考虑将整个作业放入 While 循环内的 TSQL 脚本中。要检查的条件显然是当前时间和作业开始时间之间的时间差。

拉吉

What kind of a job is this? You may want to consider putting the whole job in a TSQL script within a While loop. The condition to check would obviously be the time difference between current time and job start time.

Raj

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文