Using File Triggers to Submit Jobs

Follow

A common requirement is to create a Trigger that fires when a file arrives and Submits a Job that processes the file and then resets the trigger. This is simple, straight forward and reliable when there is only a single file arriving. However, in cases where there are multiple files arriving, the recommended approach is slightly different.

The following code snippet shows how a JAMS Trigger and PowerShell can be combined to handle multiple files taken in by a single Trigger. This code Snippet is the Job that the Trigger itself should Submit upon Firing.

#
#  Import the JAMS PowerShell module
#
Import-Module JAMS

#
#  Get all of the files that match what the trigger file event is looking for
#
$fileList = Get-ChildItem C:\Incoming\*.dat

##  Create an array where we will save the JAMS Entry number of job we submit#$entryList = @()
#
#  Submit a JAMS Job to process each of the files
#
foreach ($file in $fileList)
{
    #
    # get the filename from the file object
    #
    $filename = $file.FullName

    #
    #  Submit the ProcessFile job.  This job must have a parameter named "filename"
    #  The "-UseVariables" parameter tells Submit-JAMSEntry to look for a PowerShell
    #  Variable that matches each parameter in the job and use the variable value
    #  as the value for the parameter
    #
    $lastSubmit = Submit-JAMSEntry ProcessFile -UseVariables

    #
    #  Write some information to the log file
    #
    write-host "Entry " $lastSubmit.JAMSEntry " submitted to process $filename"    # Save the Entry number    #    $entryList += $lastSubmit.JAMSEntry
}

#
#  We have submitted a job to process each file,
#  now we will wait for all of them to complete.
#
Wait-JAMSEntry $entryList -verbose
Have more questions? Submit a request

Comments