How to check replication snapshot agent status?

最后都变了- 提交于 2019-12-12 15:15:19

问题


I'd like to check the status of the agent after I start it using this statement

EXEC sp_startpublication_snapshot @publication

As I want to do a next step that needs the job to be already started.


回答1:


I do not believe there is a built-in replication stored procedure to check the snapshot agent status, I could be wrong. However, you could query MSsnapshot_history. Something like this should do the trick:

SELECT 
    agent_id, 
    runstatus, 
    start_time, 
    time, 
    duration, 
    comments, 
    delivered_transactions,
    delivered_commands, 
    delivery_rate, 
    error_id, 
    timestamp 
FROM dbo.MSsnapshot_history
WHERE comments = 'Starting agent.'

Likewise, you can check when the snapshot agent is finished:

SELECT 
    agent_id, 
    runstatus, 
    start_time, 
    time, 
    duration, 
    comments, 
    delivered_transactions,
    delivered_commands, 
    delivery_rate, 
    error_id, 
    timestamp 
FROM dbo.MSsnapshot_history
WHERE comments = '[100%] A snapshot of 68 article(s) was generated.'

Alternatively, you could the status of the Snapshot Agent job using sp_help_job.




回答2:


After some research I got a work around way

SELECT snapshot_ready FROM sysmergepublications 

This query returns 0 if not ready and 1 if started

Thanks all for your contribution :)




回答3:


You can also use while loop to check if agent is running or not before executing other scripts:

use [distribution];
declare @status int = 2

 select @status = status
 FROM dbo.MSReplication_monitordata
 WHERE publication = 'PublicationName' and agent_type = 1

 while @status = 3
 begin
 WAITFOR DELAY '00:00:03'
 select @status = status
 FROM dbo.MSReplication_monitordata
 WHERE publication = 'Publication.Name' and agent_type = 1
 end

reference: T-SQL script to wait till status of snap agent is completed




回答4:


I realize this is an older thread. However, as this could still very well be something that needs to be done in current versions of SQL (2017/2019 as of this update), here is a function I use in powershell. Its purpose is to sit until snapshot is available before rebuilding replication in our DEV/QA environment.

# =======================================================================================================================================
# WAIT UNTIL SNAPSHOT CREATED
# =======================================================================================================================================
Function Wait-ForSnapshotCreation {
    [Cmdletbinding()]

    Param (
        [int] $InitialSleepInSec = 60
       ,[int] $IncrementalSleepInSec = 2
       ,[string] $ReplicationPublication
       ,[string] $ReplicationDistributor
    )

    Process {
        $SleepMessage = "Sleeping for {0} seconds at the start..." -f $InitialSleepInSec
        $TotalSleepInSec = $SleepInSec
        $MonitorSQL = "exec sp_replmonitorhelppublication @publication = N'$ReplicationPublication'"
        Write-Message -Message ($SleepMessage)
        Start-Sleep -s $InitialSleepInSec

        Do {
             $MonitorStatus = (Invoke-SQLCmd -Query ($MonitorSQL) -ServerInstance $ReplicationDistributor -Database 'Distribution' -AbortOnError)
             If ($MonitorStatus.status -eq 3) {
                 $SleepMessage = "Replication status is still 'running' (3).   Will sleep for {0} more seconds. Slept {1} seconds so far..." -f $IncrementalSleepInSec, $TotalSleepInSec
                 Write-Message -Message ($SleepMessage)

                 Start-Sleep -s $IncrementalSleepInSec
                 $TotalSleepInSec += $IncrementalSleepInSec

             } # If...

        } Until ($MonitorStatus.status -ne 3)

    } # Process

} # Function

The following is an example call (used just before creating subscribers) :

Wait-ForSnapshotCreation -InitialSleepInSec 15 -IncrementalSleepInSec 1 -ReplicationPublication '<YOUR_PUBLICATION>' -ReplicationDistributor '<YOUR_DISTRIBUTION_SERVER_INSTANCE>'



回答5:


Check its run status. Here are its values: 1 = Start.

2 = Succeed.

3 = In progress.

4 = Idle.

5 = Retry.

6 = Fail.



来源:https://stackoverflow.com/questions/25034968/how-to-check-replication-snapshot-agent-status

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