I'm developing a system that interfaces with a USPS shipping package called Dazzle. Part of this system includes a monitoring daemon whose purpose is to take tab-separated value files, turn them into XML that Dazzle recognizes, and pass them to Dazzle for label generation. And this part works just fine. What I also want to do, however, is to parse the output file that Dazzle generates and import it into a database.
Note here that Dazzle runs on Windows. My monitoring daemon is written in Perl and runs on Linux. My Linux system has Dazzle's input and output directories mounted via Samba.
There is a measurable delay between the time Dazzle starts writing the output file and the time it's finished. What I want to know is how I can wait for Dazzle to finish writing the output file? I've tried opening the file and doing flock($fh, LOCK_SH)
on it, but that didn't seem to do any good.
EDIT: I have an idea based on "mobrule"'s comment below. Dazzle writes an output file in XML. Each package in the shipment is enclosed in tags, and the entire document is enclosed in a tag. So, if I start reading the file before it's complete, I can simply wait for the appropriate closing tag before I take action.
Also, I should mention what I'm doing currently. When I detect that the output XML file has been created, I attempt to parse it. If that parsing fails, I sleep and try again. If that fails, I sleep twice as long, then try again, and so on. This has worked pretty well in testing with a 64 second timeout.