Tuesday, May 17, 2011

Cancel A Specific SSRS Job

RS.exe is located at \Program Files\Microsoft SQL Server\100\Tools\Binn. You can run the utility from any folder on your file system.

'=============================================================================
'  File:      CancelSpecificJob.rss
'
'  Summary:  Demonstrates a script that can be used with RS.exe to
'      cancel a specific job that is currently running on a server.
'
'
'---------------------------------------------------------------------
'
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'==============================================================================
'
' 1.0 Documentation
'
' Read the following in order to familiarize yourself with the sample script.
'
' 1.1 Overview
'
' The script lists all jobs that are currently running on the report server and
' then enables the user to cancel a spefic job ID.
'
' 1.2 Script Variables
'
' None.
'
' 1.3 Sample Command Lines
'
'
' 1.3.1 To view and cancel running jobs
'
'       rs -i CancelSpecificJob.rss -s http://myserver/reportserver
'
Public Sub Main()
   Dim jobs As Job() = rs.ListJobs()
   If ListRunningJobs(jobs) Then
     
      Console.Write("Which Job ID to kill? ")
     
      Dim  userJobID As String = Console.ReadLine()
      'If [Char].ToLower(Microsoft.VisualBasic.ChrW(userJobID)) = "XX" Then
       'CancelRunningJobs(jobs)
       'Else
       rs.CancelJob(userJobID)
      'End If
   End If
End Sub
  
  
Public Function ListRunningJobs(jobs() As Job) As Boolean
   Dim runningJobCount As Integer = 0
   Dim message As String
   Console.WriteLine("Current Jobs")
   Console.WriteLine(("================================" + Environment.NewLine))
   Dim job As Job
   For Each job In  jobs
      If job.Status = JobStatusEnum.Running Or job.Status = JobStatusEnum.[New] Then
      ' New goes away soon
         Console.WriteLine("--------------------------------")
         Console.WriteLine("JobID: {0}", job.JobID)
         Console.WriteLine("--------------------------------")
         Console.WriteLine("Action: {0}", job.Action)
         Console.WriteLine("Description: {0}", job.Description)
         Console.WriteLine("Machine: {0}", job.Machine)
         Console.WriteLine("Name: {0}", job.Name)
         Console.WriteLine("Path: {0}", job.Path)
         Console.WriteLine("StartDateTime: {0}", job.StartDateTime)
         Console.WriteLine("Status: {0}", job.Status)
         Console.WriteLine("Type: {0}", job.Type)
         Console.WriteLine("User: {0}" + Environment.NewLine, job.User)
         runningJobCount += 1
      End If
   Next job
   If runningJobCount = 1 Then
      message = "There is 1 running job. "
   Else
      message = String.Format("There are {0} running jobs. ", runningJobCount)
   End If
   Console.Write(message)
   If runningJobCount > 0 Then
      Return True
   Else
      Return False
   End If
End Function
   
Public Sub CancelRunningJobs(jobs() As Job)
   Dim job As Job
   For Each job In  jobs
      If job.Status = JobStatusEnum.Running Or job.Status = JobStatusEnum.[New] Then
         ' New goes away soon
         rs.CancelJob(job.JobID)
      End If
   Next job
   Console.WriteLine("All jobs successfully canceled.")
End Sub

1 comment: