Facciamo qualcosa di simile il codice di seguito come parte di un sottosistema di elaborazione di lavoro notturno - è più complicato di questo in realtà nella realtà; per esempio stiamo elaborando più set interdipendenti di posti di lavoro, e leggere nei nomi di lavoro e valori di timeout da tabelle di configurazione - ma questo coglie l'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 @[email protected]
-- 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
+1 Non v'è alcun apparente proprietà "timeout" nell'editor di lavoro. Sono interessato a ciò che le persone inventano. – Tomalak
Penso che dovresti aggiungere un'impostazione tipo WAITFOR nell'SQL. –