in src/WebJobs.Extensions/Extensions/Files/Listener/FileProcessor.cs [337:386]
internal StreamWriter AcquireStatusFileLock(string filePath, WatcherChangeTypes changeType, out StatusFileEntry statusEntry)
{
Stream stream = null;
statusEntry = null;
try
{
// Attempt to create (or update) the companion status file and lock it. The status
// file is the mechanism for handling multi-instance concurrency.
string statusFilePath = GetStatusFile(filePath);
stream = File.Open(statusFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
// Once we've established the lock, we need to check to ensure that another instance
// hasn't already processed the file in the time between our getting the event and
// acquiring the lock.
statusEntry = GetLastStatus(stream);
if (statusEntry != null && statusEntry.State == ProcessingState.Processed)
{
// For file Create, we have no additional checks to perform. However for
// file Change, we need to also check the LastWrite value for the entry
// since there can be multiple Processed entries in the file over time.
if (changeType == WatcherChangeTypes.Created)
{
return null;
}
else if (changeType == WatcherChangeTypes.Changed &&
File.GetLastWriteTimeUtc(filePath) == statusEntry.LastWrite)
{
return null;
}
}
stream.Seek(0, SeekOrigin.End);
StreamWriter streamReader = new StreamWriter(stream);
streamReader.AutoFlush = true;
stream = null;
return streamReader;
}
catch
{
return null;
}
finally
{
if (stream != null)
{
stream.Dispose();
}
}
}