Folder Watcher using Healthshare

Primary tabs

Hi 

I've got a new piece of work to do which the goal is to send an email out if files start queuing up in a specific folder.

Is there any way of achieving this?

Any help would be appreciated.

Kind Regards,

Joao Palma

Replies

Hi Joao.

I have a task that runs every x mins to check a folder for files older than y mins, and it then sends an alert to Ens.Alert to then trigger an alert to our team. The logic is, if the file has been sat in the folder for more than the defined period, it is effectively "queued".

However, as I went to dig it out, I have realised I made a mistake with subtracting the seconds from the Horolog near midnight...

I'll try clean it up, and then see if it safe to submit here :)

Hi Joao.

Here is the working version of the code I referred to - I'm sure someone here can probably look at it and refactor it into 4 lines, but it does the job smiley

Class DEV.Monitoring.FolderMonitor Extends %SYS.Task.Definition
{ Parameter TaskName = "Folder Queue Monitor"; Property MonitoredFolder As %String; Property AgeToAlert As %Integer(VALUELIST = ",5,10,15,20,25,30,35,40,45,50,55,60") [ InitialExpression = "30" ]; Property SMTPServer As %String; Property SMTPPort As %String [ InitialExpression = "25" ]; Property SMTPUsername As %String; Property SMTPPassword As %String; Property Recipient As %String(MAXLEN = 256); Method OnTask() As %Status
{
Set tsc = ..CheckFileAgeInFolder(..MonitoredFolder,..AgeToAlert,"*.*")
Quit tsc
} Method CheckFileAgeInFolder(Directory As %String, AgeToAlert As %Integer, Extention As %String) As %Status
{
set tSC = $$$OK
// Calculate the file age that we want to trigger the alert // $h is Horolog. HOROLOG contains a character string that consists of two integer values
// separated by a comma. These two integers represent the current local date and time in 
// Caché storage format. These integers are counters, not user-readable dates and times.
// The format is ddddd,sssss //First, take the HOROLOG into a variable so that it isn't different everytime we need to use the HOROLOG.
Set pHorolog = $H //Now break down each part of the Horolog into the days and seconds
set currentdate = $PIECE(pHorolog,",",1)
set currenttime = $PIECE(pHorolog,",",2) // We multiply the AgeToAlert by 60 to convert the AgeToAlert to Seconds so we can use it easily with the HOROLOG.
set pAgeToAlertSeconds = AgeToAlert*60 // At this point, we want to subtract the age to alert from the curent time in seconds. HOWEVER, we can't go negative without going back a day.
// So, to get around this, I have attempted to bodge the checks. If the age to alert in seconds is greater that the 
// current time, we then go back to before midnight... I dunno, I'm just making this up and hoping it works.
If pAgeToAlertSeconds>currenttime {
Set currentdate = currentdate-1
Set timediff = pAgeToAlertSeconds-currenttime
set adjustedtime = 86400-timediff
}
Else{
set adjustedtime = currenttime - pAgeToAlertSeconds
} // We create the value "triggered" with the value of 0 now, so that we can check to see if we need to alert.
set triggered = 0 // We then build a new Horolog by concatinating the currentdate and adjustedtime values
// within the $zdt function. The $zdt function then converts the Horolog to a date/time 
// which can be used for comparing against the datemodified value of the files being checked.
set BeforeThisDate = $zdt(currentdate_","_adjustedtime,3) // Gather the list of files in the specified directory
set rs=##class(%ResultSet).%New("%File:FileSet")
do rs.Execute(Directory,Extention,"DateModified") // Step through the files in DateModified order and compare their date modified 
// with the BeforeThisDate Value to see if any files are old enough to active the trigger while rs.Next() {
set DateModified=rs.Get("DateModified")
if BeforeThisDate]DateModified {
// trigger the trigger
set triggered = 1
} // Stop when we get to files with last modified dates on or after our fileage adjusted date to 
// avoid checking files that don't need to be checked at this time. if DateModified]BeforeThisDate 
set tSC = $$$OK
} // Evaluate if trigger has been triggered if triggered = 1 { //Create and send an email
Set newSMTP=##class(%Net.SMTP).%New()
Set newSMTP.smtpserver=..SMTPServer
Set newSMTP.port=..SMTPPort Set newAuthenticator=##class(%Net.Authenticator).%New()
Set newAuthenticator.UserName=..SMTPUsername
Set newAuthenticator.Password=..SMTPPassword Set newSMTP.authenticator=newAuthenticator Set newEmailMessage=##class(%Net.MailMessage).%New() Set EmailCount = $LENGTH(..Recipient,",")
For i=1:1:EmailCount
{
Do newEmailMessage.To.Insert($PIECE(..Recipient,",",i))
} Set newEmailMessage.From="DoNotReply@imadethisemailupandlackcreativity.co.uk"
Set newEmailMessage.Subject="Queue Building in folder:"_..MonitoredFolder #Dim MessageText
Set MessageText = "<html><head><style>th#la {padding-left: 10px; padding-right:20px; padding-bottom:15px; text-align: left;}td#la {padding-left: 10px; padding-right:20px; padding-bottom:5px; text-align: left;}</style></head><body><table border=""0"" cellpadding=""0"" cellspacing=""0"" height=""100%"" width=""100%"" id=""bodyTable""><tr><td align=""center"" valign=""top""><table border=""0"" cellpadding=""20"" cellspacing=""0"" width=""1000"" id=""emailContainer""><tr><td align=""center"" valign=""top""><table border=""0"" cellpadding=""10"" cellspacing=""0"" width=""100%"" id=""emailHeader""><tr><td bgcolor=""#0072CE"" align=""center"" valign=""top"">" // This is the Header of the email.
Set MessageText = MessageText _ "<h1 style=""color: #ffffff; font-family: Arial, sans-serif; font-size: 28px;"" >Queue Building in folder "_..MonitoredFolder_"</h1>" // This is the HTML for the start of the body which is built up within a table for structure
Set MessageText = MessageText _ "</td></tr></table></td></tr><tr><td align=""center"" valign=""top""><table border=""0"" cellpadding=""20"" cellspacing=""0"" width=""100%"" id=""emailBody""><tr><td align=""left"" valign=""top"">" // This is the Body of the email. These lines can be repeated 
// as many times as needed to act as the lines of text in the email.
Set MessageText = MessageText _ "<p style=""color: #153643; font-family: Arial, sans-serif; font-size: 18px;"">Files are queueing in the folder "_..MonitoredFolder_".</p>" // This line closes off the table secion created above
Set MessageText = MessageText _ "</td></tr></table></td></tr><tr><td align=""center"" valign=""top""><table border=""0"" cellpadding=""10"" cellspacing=""0"" width=""100%"" id=""emailFooter""><tr><td bgcolor=""#0072CE"" align=""center"" valign=""top"">" // This is the Footer of the email
Set MessageText = MessageText _ "<p style=""color: #ffffff; font-family: Arial, sans-serif; font-size: 12px;"">This email was generated automatically by xyz corp.</p>" // This is the end of the HTML.
Set MessageText = MessageText _ "</td></tr></table></td></tr></table></td></tr></table></body></html>" Set newEmailMessage.IsHTML = 1
Do newEmailMessage.TextData.Write(MessageText) ///Send the Email
Set tSC=newSMTP.Send(newEmailMessage)
Quit tSC
}
else{
Quit tSC
}
} }