[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] 24x7 Script Archive


Implementing true "file watch"

24x7 Scheduler allows to setup a job that starts when one or more specified files (semaphores) exist. However, in some cases it is not enough to simply detect if a file exist. Some programs create files first then continue writing to the files so such files are "visible" to other programs (can be detected) before writing is complete. To overcome this limitation, you can code a job that check file sizes before performing main action. If the file size is less than average then the job can "sleep" awaiting completion. A more reliable but not always acceptable way is to try to open the file for the read operation by using FileOpen statement. If the file is being used by the originating process (still locked), the FileOpen fails and the job continues waiting, otherwise it releases the file and performs main action.

Example 1:

Dim file_size, number
Dim too_small, boolean
Dim wait_time, number
Dim timeout, boolean

CHECK_FILE_SIZE:
FileSize "myfile.txt", file_size

// check is file size is less than 1Mb ( 1048576 bytes )
isLess file_size, 1048576, too_small
IfThen too_small, TRY_LATER
     // check if we are waiting already too long (more than 10 minutes)
     isGreater wait_time, 600, timeout
     IfThen timeout, TIMEOUT
           // Notify system admin.
           MailSend "Exchange Settings", "pass", "sysadmin@mycompany.com", &
                             "File watch job failed", "Giving up while waiting for myfile.txt"
           exit
     TIMEOUT:

     // wait 30 seconds then try again
     Wait 30
     Add wait_time, 30, wait_time
     GoTo CHECK_FILE_SIZE
TRY_LATER:

// OK, file size is above average, wait extra 10 seconds then try again
Wait 10
// ...
// perform main operation here
// ...

Example 2:

Dim file_handle, number
Dim wait_time, number
Dim timeout, boolean

FileOpen "myfile.txt", "StreamMode", "Read", True, file_handle
LoopUntil file_handle, END_LOOP
     // check if we are waiting already too long (more than 10 minutes)
     isGreater wait_time, 600, timeout
     IfThen timeout, TIMEOUT
           // Notify system admin.
           MailSend "Exchange Settings", "pass", "sysadmin@mycompany.com", &
                             "File watch job failed", "Giving up while waiting for myfile.txt"
           exit
     TIMEOUT:

     // wait 30 seconds then try again
     Wait 30
     Add wait_time, 30, wait_time

     // try opening file
    FileOpen "myfile.txt", "StreamMode", "Read", True, file_handle
END_LOOP:

// Release file
FileClose file_handle
// ...
// perform main operation here
// ...