/// <summary>
        /// Returns true if the file was written.
        /// Returns false if the in-process lock failed. Throws an exception if any kind of file or processing exception occurs.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="path"></param>
        /// <param name="writeCallback"></param>
        /// <param name="timeoutMs"></param>
        /// <param name="recheckFS"></param>
        /// <returns></returns>
        private bool TryWriteFile(CacheResult result, string path, ResizeImageDelegate writeCallback, int timeoutMs, bool recheckFS)
        {
            bool miss = true;

            if (recheckFS)
            {
                miss = !Index.PathExistInIndex(path);
                if (!miss && !Locks.MayBeLocked(path.ToUpperInvariant()))
                {
                    return(true);
                }
            }

            //Lock execution using relativePath as the sync basis. Ignore casing differences. This locking is process-local, but we also have code to handle file locking.
            return(Locks.TryExecute(path.ToUpperInvariant(), timeoutMs,
                                    delegate() {
                //On the second check, use cached data for speed. The cached data should be updated if another thread updated a file (but not if another process did).
                if (!Index.PathExistInIndex(path))
                {
                    //Open stream
                    //Catch IOException, and if it is a file lock,
                    // - (and hashmodified is true), then it's another process writing to the file, and we can serve the file afterwards
                    // - (and hashmodified is false), then it could either be an IIS read lock or another process writing to the file. Correct behavior is to kill the request here, as we can't guarantee accurate image data.
                    // I.e, hashmodified=true is the only supported setting for multi-process environments.
                    //TODO: Catch UnathorizedAccessException and log issue about file permissions.
                    //... If we can wait for a read handle for a specified timeout.

                    try
                    {
                        var blobRef = container.GetBlockBlobReference(path);

                        // Only write to the blob if it does not exist
                        if (!blobRef.Exists())
                        {
                            CloudBlobStream cloudBlobStream = blobRef.OpenWrite();
                            bool finished = false;
                            try
                            {
                                using (cloudBlobStream)
                                {
                                    //Run callback to write the cached data
                                    writeCallback(cloudBlobStream);     //Can throw any number of exceptions.
                                    cloudBlobStream.Flush();
                                    finished = true;
                                }
                            }
                            finally
                            {
                                //Don't leave half-written files around.
                                if (!finished)
                                {
                                    try
                                    {
                                        blobRef.Delete();
                                    }
                                    catch { }
                                }
                            }

                            DateTime createdUtc = DateTime.UtcNow;
                        }

                        //Update index
                        Index.AddCachedFileToIndex(path);

                        //This was a cache miss
                        if (result != null)
                        {
                            result.Result = CacheQueryResult.Miss;
                        }
                    }
                    catch (IOException ex)
                    {
                        //Somehow in between verifying the file didn't exist and trying to create it, the file was created and locked by someone else.
                        //When hashModifiedDate==true, we don't care what the file contains, we just want it to exist. If the file is available for
                        //reading within timeoutMs, simply do nothing and let the file be returned as a hit.
                        Stopwatch waitForFile = new Stopwatch();
                        bool opened = false;
                        while (!opened && waitForFile.ElapsedMilliseconds < timeoutMs)
                        {
                            waitForFile.Start();
                            try
                            {
                                using (var stream = container.GetBlockBlobReference(path).OpenRead())
                                    opened = true;
                            }
                            catch (IOException iex)
                            {
                                Thread.Sleep((int)Math.Min(30, Math.Round((float)timeoutMs / 3.0)));
                            }
                            waitForFile.Stop();
                        }
                        if (!opened)
                        {
                            throw;         //By not throwing an exception, it is considered a hit by the rest of the code.
                        }
                    }
                }
            }));
        }
示例#2
0
        /// <summary>
        /// Returns true if either (a) the file was written, or (b) the file already existed with a matching modified date.
        /// Returns false if the in-process lock failed. Throws an exception if any kind of file or processing exception occurs.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="physicalPath"></param>
        /// <param name="relativePath"></param>
        /// <param name="writeCallback"></param>
        /// <param name="timeoutMs"></param>
        /// <param name="recheckFS"></param>
        /// <returns></returns>
        private bool TryWriteFile(CacheResult result, string physicalPath, string relativePath, ResizeImageDelegate writeCallback, int timeoutMs, bool recheckFS)
        {
            bool miss = true;

            if (recheckFS)
            {
                miss = !Index.existsCertain(relativePath, physicalPath);
                if (!miss && !Locks.MayBeLocked(relativePath.ToUpperInvariant()))
                {
                    return(true);
                }
            }


            //Lock execution using relativePath as the sync basis. Ignore casing differences. This locking is process-local, but we also have code to handle file locking.
            return(Locks.TryExecute(relativePath.ToUpperInvariant(), timeoutMs,
                                    delegate() {
                //On the second check, use cached data for speed. The cached data should be updated if another thread updated a file (but not if another process did).
                if (!Index.exists(relativePath, physicalPath))
                {
                    //Create subdirectory if needed.
                    if (!Directory.Exists(Path.GetDirectoryName(physicalPath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(physicalPath));
                        if (lp.Logger != null)
                        {
                            lp.Logger.Debug("Creating missing parent directory {0}", Path.GetDirectoryName(physicalPath));
                        }
                    }

                    //Open stream
                    //Catch IOException, and if it is a file lock,
                    // - (and hashmodified is true), then it's another process writing to the file, and we can serve the file afterwards
                    // - (and hashmodified is false), then it could either be an IIS read lock or another process writing to the file. Correct behavior is to kill the request here, as we can't guarantee accurate image data.
                    // I.e, hashmodified=true is the only supported setting for multi-process environments.
                    //TODO: Catch UnauthorizedAccessException and log issue about file permissions.
                    //... If we can wait for a read handle for a specified timeout.

                    try
                    {
                        string tempFile = physicalPath + ".tmp_" + new Random().Next(int.MaxValue).ToString("x") + ".tmp";

                        System.IO.FileStream fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None);
                        bool finished = false;
                        try
                        {
                            using (fs)
                            {
                                //Run callback to write the cached data
                                writeCallback(fs);     //Can throw any number of exceptions.
                                fs.Flush(true);
                                finished = true;
                            }
                        }
                        finally
                        {
                            //Don't leave half-written files around.
                            if (!finished)
                            {
                                try { if (File.Exists(tempFile))
                                      {
                                          File.Delete(tempFile);
                                      }
                                }
                                catch { }
                            }
                        }
                        bool moved = false;
                        if (finished)
                        {
                            try
                            {
                                File.Move(tempFile, physicalPath);
                                moved = true;
                            }
                            catch (IOException)
                            {
                                //Will throw IO exception if already exists. Which we consider a hit, so we delete the tempFile
                                try { if (File.Exists(tempFile))
                                      {
                                          File.Delete(tempFile);
                                      }
                                }
                                catch { }
                            }
                        }
                        if (moved)
                        {
                            DateTime createdUtc = DateTime.UtcNow;
                            //Set the created date, so we know the last time we updated the cache.s
                            System.IO.File.SetCreationTimeUtc(physicalPath, createdUtc);
                            //Update index
                            //TODO: what should sourceModifiedUtc be when there is no modified date?
                            Index.setCachedFileInfo(relativePath, new CachedFileInfo(createdUtc, createdUtc, createdUtc));
                            //This was a cache miss
                            if (result != null)
                            {
                                result.Result = CacheQueryResult.Miss;
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        if (IsFileLocked(ex))
                        {
                            //Somehow in between verifying the file didn't exist and trying to create it, the file was created and locked by someone else.
                            //When hashModifiedDate==true, we don't care what the file contains, we just want it to exist. If the file is available for
                            //reading within timeoutMs, simply do nothing and let the file be returned as a hit.
                            Stopwatch waitForFile = new Stopwatch();
                            bool opened = false;
                            while (!opened && waitForFile.ElapsedMilliseconds < timeoutMs)
                            {
                                waitForFile.Start();
                                try {
                                    using (FileStream temp = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                                        opened = true;
                                } catch (IOException iex) {
                                    if (IsFileLocked(iex))
                                    {
                                        Thread.Sleep((int)Math.Min(30, Math.Round((float)timeoutMs / 3.0)));
                                    }
                                    else
                                    {
                                        throw iex;
                                    }
                                }
                                waitForFile.Stop();
                            }
                            if (!opened)
                            {
                                throw;              //By not throwing an exception, it is considered a hit by the rest of the code.
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }));
        }