Folder Watch

scottclem (2 posts)
August 24, 2018 02:30 PM
Accepted Answer

I am new to adTempus but my company has several jobs running. One of the jobs runs a script to move files from an FTP server onto a folder on a server. Those files are then processed, but all of the files remain in the folder.

I need to create a job that looks to see if any files have been placed in the folder in the last -n- minutes, and if not, send an email message.

Can somebody direct me on how to do this?

 

Thanks!

Scott

 

Bill Staff (604 posts)
August 25, 2018 04:22 PM
Accepted Answer

You can do this by writing a script to scan the folder. (Note: this is scanning your local folder on the server, not the remote FTP server.)

1. Create a new job.

2. On the Variables tab, create the following variables:

  • FolderToWatch. Set this to the complete path of the folder you want to watch.
  • FolderWatchInterval. Change the type to Integer and set it to the number of minutes after which you want to be notified (your n value).
  • NewestFileMessage. Leave this blank; it's a placeholder and the value will be set by your script.

3. Add a Step and choose the Execute a Script type. Select New to create a new script. Change the language to C# and replace the code with the following:

/*
    You MUST retain the class and function definition below. The Run method
    is the entry point to the script and will be called by adTempus. You can
    define additional methods and properties within the class as necessary.
    You can also declare and use additional classes, and reference
    classes in Script Libraries. To use code from external assemblies,
    add references to the assemblies above.

    The global "adTempus" object provides integration with the adTempus server including
    access to environment and job variables for the job.
*/
using System;
using System.IO;
using System.Collections.Generic;
using ArcanaDevelopment.adTempus.ApplicationIntegration;
using ArcanaDevelopment.adTempus.Shared;


namespace UserScript
{
    public class UserScript : ArcanaDevelopment.adTempus.ApplicationIntegration.UserScriptBase
    {
        public override Object Run()
        {
//get the Job Variable values with the settings
var folderToWatch=adTempus.JobVariables["FolderToWatch"];
var folderWatchInterval=adTempus.JobVariables.GetVariable("folderWatchInterval").IntegerValue;

//we want to find files that have been modified since folderWatchInterval minutes ago.
var cutoffTime=DateTime.UtcNow.Subtract(TimeSpan.FromMinutes((double)folderWatchInterval));

//get all the files in the watched folder
var folder=new DirectoryInfo(folderToWatch);
var files=folder.GetFiles("*.*");

FileInfo newestFile=null;

foreach(var file in files)
{
if(file.LastWriteTimeUtc >= cutoffTime || file.CreationTimeUtc >= cutoffTime)
{
//we have found a file that has been created or modified within the specified time.
//no further processing is necessary
        	adTempus.LogMessage(MessageTypeEnum.Informational, 0, string.Format("Found file {0} modified at {1:yyyy-MM-dd HH:mm:ss}",file.Name,file.LastWriteTime));
return 0;
}

if(null==newestFile || newestFile.LastWriteTimeUtc < file.LastWriteTimeUtc)
{
newestFile=file;
}
}

//if we get this far, there were no files created/modified recently enough.

string message;
if(null==newestFile)
{
            	message="No files found in target folder";
}
else
{
            	message=string.Format("No files in target folder created since {0:yyyy-MM-dd HH:mm:ss} ({1})",newestFile.LastWriteTime,newestFile.Name);
}

//log a message to the JobLog
        	adTempus.LogMessage(MessageTypeEnum.Informational, 0, message);

//add the message as a variable we can use in the notification message
adTempus.JobVariables.Add("NewestFileMessage",message);



            return 0;
        }
    }
}

This script does the following:

  • Reads the variables to get the folder and time interval.
  • Scans all files in the folder and looks for any that have been created or modified within the interval.
  • Writes a message to the Job Log describing the results.
  • If no matching files are found, sets the NewestFileMessage variable with the results.

4. Save that step and add a second step. Select task type "Send a notification message".

  • Go to the Conditions page for the step and add a new condition to depend on the value of a variable. Select the "NewestFileMessage" variable and rule "does not equal". Leave the Compare To blank. This means the step will only run if the script has set the variable (meaning no files were found). If files are found, the variable is not set and the notification does not run.
  • Go to the Notification Message page and configure whatever message you want. You can include the folder name and timeout by inserting the appropriate variable tokens. For example:
    No new files found in "%FolderToWatch%
  • To insert the details of the newest file (which we stored in the NewestFileMessage variable), include the token "%NewestFileMessage%" in the notification message body.
  • Add recipients to the message as appropriate.

5. Save the step and add a Schedule Trigger to the job to run it at the appropriate interval (every n minutes, I'd guess).

Notes:

If you're leaving files in the folder after processing, that could make for a lot of files for the script to scan every time it runs. Depending how many files and how often you're running the script, you could see the script host using a lot of CPU time. In that case you'll need to modify your workflow so files get moved out of the folder after they're processed, but then this script won't do you any good.

If you need to monitor several different folders, check the Shared box in the script editor to save the script as a Shared Script. Then you can call it from multiple jobs, with the variables set appropriately in each job.

 

scottclem (2 posts)
August 25, 2018 04:47 PM
Accepted Answer
Thank you Bill! I really appreciate this detailed and complete response.

Replies are disabled for this topic.