Finding multiple jobs based on the same trigger

Paul Watson (101 posts)
January 11, 2018 04:31 PM
Accepted Answer

We had an experience this week where multiple jobs that were triggered by a file started concurrently. I am sure this is a valid thing to do, but it was not in this situation.

Schedule A forked to schedule B for some good reasons. The problem is that A.jobx and B.jobx were both triggered by the presence of a file. When they both started and were writing over each other in the same directories, bad things happened.

The question is, what would be a good way of finding all of the jobs which trigger on the same event?

I realize this might be non-trivial since triggering on file "\\JOSEPH\C$\data\trigger.txt" does not exactly look like "C:\data\trigger.txt".

Bill Staff (599 posts)
January 12, 2018 09:17 AM
Accepted Answer

Here's a script (you can run with LINQPad) that will list all the file triggers on your server. To address your last point, it sorts them by the file spec ignoring the drive, so hopefully that will get duplicates sorted near enough to spot when you scan the list.

'if the adTempus server is on a different computer, replace . with the name
'for example,
'Dim ServerName as String = "remoteservername"
Dim ServerName As String = "."

Dim foundTriggers As New List(Of JobTriggerInfo)

Sub Main
	
	
	Using session = Scheduler.Connect(ServerName, LoginAuthenticationType.Windows, "", "")
		Dim context=session.ReadOnlyDataContext
		
		'find all the file triggers and make a list
		Dim jobs=context.GetJobs("*")
		For Each job In jobs
			ProcessJob(job)
		Next
			
		'sort them by file spec, ignoring drive
		foundTriggers.Sort(New JobTriggerInfoComparer())
		
		'write out the sorted list
		For Each trigger In foundTriggers
			Console.WriteLine(trigger.JobName & Microsoft.VisualBasic.ControlChars.Tab & trigger.Drive & Microsoft.VisualBasic.ControlChars.Tab & trigger.FileSpecificationWithoutDrive )
		Next
	End Using
End Sub

Sub ProcessJob(job As Job)
	For Each trigger In job.Triggers
		If trigger.ClassID=ClassID.FileTrigger Then
			Dim fileTrigger=CType(trigger,FileTrigger)
			For Each target In fileTrigger.Files
				foundTriggers.Add(New JobTriggerInfo(job.FullyQualifiedName,target.FileSpecification))
			Next
		End If
	Next
End Sub

Class JobTriggerInfo
	Public JobName As String
	Public FileSpecification As String
	Public FileSpecificationWithoutDrive As String
	Public Drive As String
	
	Public Sub New(name As String, spec As String)
		JobName = name
		FileSpecification = spec

		Dim match = System.Text.RegularExpressions.Regex.Match(spec, "([a-z]:|\\\\[^\\]+\\[^\\]+)\\(.+)", RegexOptions.IgnoreCase)
		If match.Success Then
			Drive=match.Groups(1).Value 
			FileSpecificationWithoutDrive = match.Groups(2).Value
		Else
			Drive=""
			FileSpecificationWithoutDrive = FileSpecification
		End If
	End Sub

End Class

'sort JobTriggerInfo by file spec, ignoring drive
Class JobTriggerInfoComparer
	Implements IComparer(Of JobTriggerInfo)
	
	Public Function Compare(x As JobTriggerInfo,y As JobTriggerInfo) As Integer Implements IComparer(Of JobTriggerInfo).Compare
		Dim result = String.Compare(x.FileSpecificationWithoutDrive, y.FileSpecificationWithoutDrive,True)
		If result <> 0 Then
			Return result
		End If

		result = String.Compare(x.Drive, y.Drive, True)
		If result <> 0 Then
			Return result
		End If

		Return String.Compare(x.JobName,y.JobName,True)
	End Function
	
End Class
Bill Staff (599 posts)
January 12, 2018 09:21 AM
Accepted Answer
PS the output is tab-delimited so you can copy/paste or export to Excel to get nice columns or resort things.
Paul Watson (101 posts)
January 12, 2018 12:04 PM
Accepted Answer

I copied and pasted the example into LINQPad5, but got an error when trying to execute.

I thought I could paste a graphic into this message, but could not figure out how to do it. See attached.

 

 

 

 

Attachments

Bill Staff (599 posts)
January 12, 2018 12:14 PM
Accepted Answer
You need to change the Language to "VB Program," and also make sure you've added the references and imports as described in steps 2-4 of this article. (I just inserted a new step 4 because this step was missing from the old version of the article.)

Replies are disabled for this topic.