SQL Agent: Set a Max Execution Time

拜拜、爱过 提交于 2019-12-11 00:33:32

问题


Afternoon. I have several SQL Agent jobs running on an MS 2K8 BI server, some of them on a daily basis, others hourly, and one every two minutes (a heartbeat monitor for another process). There is also an app which imports data every few minutes, around the clock. Occasionally some combination of updates and reports collide and one or another hangs for a half hour or more, instead of the usual 60 seconds.

While I need to get to the root of these race conditions, in the meantime I'd like to set certain jobs to automatically die after, say, five minutes. I can do this in SSIS or a Windows scheduled task, but I don't see any way to do so in SQL Agent. Is this possible, or do I need to wrap the task in an SSIS package to get this kind of control?


FYI, here's the SQL Agent job I ended up using:

DECLARE @Cancelled BIT
EXEC dbo.CancelJob @JobName = 'ETL - Daily', @Cancelled = @Cancelled OUT

IF @Cancelled = 1
    BEGIN
    DECLARE @Success INT
    EXEC @Success = msdb..sp_send_dbmail
        @profile_name = 'Reporting',
        @recipients = 'reporting@mycompany.com',
        @subject = 'Cancelled Daily ETL'
    IF @Success <> 0 RAISERROR('An error occurred while attempting to send an e-mail.', 16, @Success)
    END

...and here's the code behind CancelJob:

CREATE PROCEDURE dbo.CancelJob(@JobName VARCHAR(100), @OwnerName VARCHAR(100) = NULL, @Cancelled BIT OUT)
AS BEGIN
    IF @OwnerName IS NULL SET @OwnerName = SUSER_NAME()
    SET @Cancelled = 0

    CREATE TABLE #JobInfo
        ( 
        Job_ID  UNIQUEIDENTIFIER,
        Last_Run_Date   INT,
        Last_Run_Time   INT,
        Next_Run_Date   INT,
        Next_Run_Time   INT,
        Next_Run_Schedule_ID    INT,
        Requested_To_Run    INT,
        Request_Source  INT,
        Request_Source_ID   VARCHAR(100),
        Running INT,  -- This is the only field we want (sigh)
        Current_Step    INT,
        Current_Retry_Attempt   INT,
        State   INT
        )
    INSERT INTO #JobInfo
        EXEC xp_sqlagent_enum_jobs 1, @OwnerName

    DECLARE @Running INT = (SELECT Running FROM #JobInfo AS JI INNER JOIN msdb..sysjobs_view AS J ON JI.Job_ID = J.job_id WHERE J.name = @JobName)
    IF @Running = 1
        BEGIN
        BEGIN TRY
            EXEC msdb..sp_stop_job @job_name = @JobName
            SET @Cancelled = 1
        END TRY
        BEGIN CATCH
            -- If an error occurs, it is *probably* because the job finished before we could cancel it, which is fine
        END CATCH
        END
END
GO

xp_sqlagent_enum_jobs was the trick to avoid the uncatchable error.


回答1:


I have never had to do this frequently, so there may be better long-term solutions, but I have created a second job to stop the first on the rare occasions that I had to perform this task. I just used the sp_stopjob procedure to do this.



来源:https://stackoverflow.com/questions/5278359/sql-agent-set-a-max-execution-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!