Exemplo n.º 1
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string targetPath = request.DataStore.GetValue("TargetPath") == null
                                ? FileUtility.GetLocalTemplatePath(request.Info.AppName)
                                : request.DataStore.GetValue("TargetPath");

            ActionResponse response = null;

            if (Directory.Exists(targetPath))
            {
                try
                {
                    RetryUtility.Retry(5, () =>
                    {
                        Directory.Delete(targetPath, true);
                        Thread.Sleep(500);
                    });
                    response = new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject());
                }
                catch (Exception ex)
                {
                    response = new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), ex, string.Empty);
                }
            }
            return(response);
        }
Exemplo n.º 2
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string targetPath = request.DataStore.GetValue("TargetPath") == null
                            ? FileUtility.GetLocalTemplatePath(request.Info.AppName)
                            : request.DataStore.GetValue("TargetPath");

            ActionResponse response = new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject());

            if (Directory.Exists(targetPath))
            {
                try
                {
                    RetryUtility.Retry(5, () =>
                    {
                        Directory.Delete(targetPath, true);
                        Thread.Sleep(500);
                    });
                }
                catch (DirectoryNotFoundException)
                {
                    //If the directory is not found return success. Either it's been deleted manually or customer installed in a different directory.
                }
                catch //(Exception ex)
                {
                    //response = new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), ex, string.Empty);
                }
            }
            return(response);
        }
Exemplo n.º 3
0
 public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName)
 {
     // I haven't tried to make this WriteThrough; not sure how to do that while keeping all the
     // clever properties Replace has.
     RetryUtility.Retry(() =>
     {
         try
         {
             File.Replace(sourceFileName, destinationFileName, destinationBackupFileName);
         }
         catch (UnauthorizedAccessException uae)
         {
             // We were getting this while trying to Replace on a JAARS network drive.
             // The network drive is U:\ which maps to \\waxhaw\users\{username}.
             // Both files were in the same directory and there were no permissions issues,
             // but the Replace command was failing with "Access to the path is denied." anyway.
             // I never could figure out why. See http://issues.bloomlibrary.org/youtrack/issue/BL-4436.
             // There is very similar code in FileUtils.ReplaceFileWithUserInteractionIfNeeded.
             try
             {
                 FileUtils.ReplaceByCopyDelete(sourceFileName, destinationFileName, destinationBackupFileName);
             }
             catch
             {
                 // Though it probably doesn't matter, report the original exception since we prefer Replace to CopyDelete.
                 throw uae;
             }
         }
     });
 }
Exemplo n.º 4
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string targetPath = request.DataStore.GetValue("TargetPath") ?? FileUtility.GetLocalTemplatePath(request.Info.AppName);

            if (Directory.Exists(targetPath))
            {
                try
                {
                    RetryUtility.Retry(5, () =>
                    {
                        Directory.Delete(targetPath, true);
                        Thread.Sleep(500);
                    });
                }
                catch (DirectoryNotFoundException)
                {
                    // If the directory is not found return success. Either it's been deleted manually or customer installed in a different directory.
                }
                catch
                {
                    // Do nothing
                }
            }

            return(new ActionResponse(ActionStatus.Success));
        }
Exemplo n.º 5
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var cognitiveServiceKey = request.DataStore.GetValue("CognitiveServiceKey");

            RetryUtility.Retry(10, () =>
            {
                CognitiveServicePayload payloadObj = new CognitiveServicePayload();
                payloadObj.Documents.Add(new Document());

                HttpResponseMessage response;

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", cognitiveServiceKey);
                    HttpContent content = new StringContent(JObject.FromObject(payloadObj).ToString(), System.Text.Encoding.UTF8, "application/json");
                    response            = client.PostAsync("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases", content).Result;
                }

                string result = response.Content.ReadAsStringAsync().Result;
                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception();
                }
            });

            return(new ActionResponse(ActionStatus.Success));
        }
Exemplo n.º 6
0
        internal static int BufferSize = 1024 * 256;         // internal so we can tweak in unit tests

        public static void Copy(string sourceFileName, string destFileName, bool overwrite = false)
        {
            RetryUtility.Retry(() =>
            {
                // This is my own implementation; the .NET library uses a native windows function.
                var bufSize = (int)Math.Min(BufferSize, new FileInfo(sourceFileName).Length);
                var buffer  = new byte[bufSize];
                using (var input = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read))
                {
                    using (
                        // We don't need WriteThrough here, it would just slow things down to force
                        // each write to commit. The final Flush(true) guarantees everything is
                        // written before the method returns.
                        var output = new FileStream(destFileName,
                                                    (overwrite ? FileMode.Create : FileMode.CreateNew),
                                                    FileAccess.Write,
                                                    FileShare.None, bufSize,
                                                    FileOptions.SequentialScan))
                    {
                        int count;
                        while ((count = input.Read(buffer, 0, bufSize)) > 0)
                        {
                            output.Write(buffer, 0, count);
                        }
                        // This is the whole point of replacing File.Copy(); forces a real write all the
                        // way to disk.
                        output.Flush(true);
                    }
                }
                // original
                //File.Copy(sourceFileName, destFileName, overwrite);
            });
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get the content of the specified entry in the specified zip file as a string.
        /// </summary>
        public static string GetZipEntryContent(string zipPath, string entryName)
        {
            string result = "error";

            RetryUtility.Retry(() =>
            {
                using (var zipFile = new ZipFile(zipPath))
                {
                    var entry = zipFile.GetEntry(entryName);
                    if (entry == null)
                    {
                        result = "";
                    }
                    else
                    {
                        using (var instream = zipFile.GetInputStream(entry))
                        {
                            using (var reader = new StreamReader(instream))
                            {
                                // don't just use Return here; it returns from the function being retried, not this whole method.
                                result = reader.ReadToEnd();
                            }
                        }
                    }
                }
            });
            return(result);
        }
Exemplo n.º 8
0
        public static void UnzipDirectory(string destFolder, string zipPath)
        {
            byte[] buffer = new byte[4096];             // 4K is optimum
            RetryUtility.Retry(() =>
            {
                using (var zipFile = new ZipFile(zipPath))
                {
                    foreach (ZipEntry entry in zipFile)
                    {
                        var fullOutputPath = Path.Combine(destFolder, entry.Name);
                        if (entry.IsDirectory)
                        {
                            Directory.CreateDirectory(fullOutputPath);
                            // In the SharpZipLib code, IsFile and IsDirectory are not defined exactly as inverse: a third
                            // (or fourth) type of entry might be possible.  In practice in .bloom files, this should not be
                            // an issue.
                            continue;
                        }

                        var directoryName = Path.GetDirectoryName(fullOutputPath);
                        if (!String.IsNullOrEmpty(directoryName))
                        {
                            Directory.CreateDirectory(directoryName);
                        }
                        using (var instream = zipFile.GetInputStream(entry))
                        {
                            using (var writer = RobustFile.Create(fullOutputPath))
                            {
                                StreamUtils.Copy(instream, writer, buffer);
                            }
                        }
                    }
                }
            });
        }
Exemplo n.º 9
0
        public static async Task RetryFuncTest()
        {
            int retry = 0;

            RetryUtility.Retry(3, () =>
            {
                retry++;
            });
            Assert.Equal(1, retry);

            retry = 0;
            Assert.Throws <Exception>(() =>
            {
                RetryUtility.Retry(3, () =>
                {
                    retry++;
                    throw new Exception();
                });
            });
            Assert.Equal(3, retry);

            retry = 0;
            await RetryUtility.Retry(3, async() =>
            {
                return(Task.FromResult(retry++));
            });

            Assert.Equal(1, retry);
        }
Exemplo n.º 10
0
        public static void RetryTest()
        {
            int  retry  = 0;
            bool result = RetryUtility.Retry(3, () =>
            {
                retry++;

                return(true);
            });

            Assert.Equal(1, retry);
            Assert.True(result);

            retry = 0;
            Assert.Throws <Exception>(() =>
            {
                result = RetryUtility.Retry(3, () =>
                {
                    retry++;
                    throw new Exception();

                    return(true);
                });
            });
            Assert.Equal(3, retry);
        }
Exemplo n.º 11
0
 public static void UploadFileToServer(string ftpserver, string user, string password, string path, string file)
 {
     RetryUtility.Retry(10, () =>
     {
         byte[] data = File.ReadAllBytes(file);
         CreateFolder(ftpserver, user, password, path);
         UploadFile(ftpserver, user, password, path, data);
     });
 }
Exemplo n.º 12
0
 public static string GetComment(string zipPath)
 {
     return(RetryUtility.Retry(() =>
     {
         using (var zipFile = new ZipFile(zipPath))
         {
             return zipFile.ZipFileComment;
         }
     }));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Save a PalasoImage to a file, trying several times if needed.
 /// </summary>
 /// <remarks>
 /// This would logically belong in SIL.Core.IO.RobustIO except that PalasoImage is in SIL.Windows.Forms.
 /// </remarks>
 public static void SaveImageRobustly(PalasoImage image, string fileName)
 {
     RetryUtility.Retry(() => image.Save(fileName),
                        RetryUtility.kDefaultMaxRetryAttempts,
                        RetryUtility.kDefaultRetryDelay,
                        new HashSet <Type>
     {
         Type.GetType("System.IO.IOException"),
         Type.GetType("System.Runtime.InteropServices.ExternalException")
     });
 }
Exemplo n.º 14
0
 public static void SaveImage(Image image, string fileName, ImageCodecInfo jpgEncoder, EncoderParameters parameters)
 {
     RetryUtility.Retry(() => image.Save(fileName, jpgEncoder, parameters),
                        RetryUtility.kDefaultMaxRetryAttempts,
                        RetryUtility.kDefaultRetryDelay,
                        new HashSet <Type>
     {
         Type.GetType("System.IO.IOException"),
         Type.GetType("System.Runtime.InteropServices.ExternalException")
     });
 }
Exemplo n.º 15
0
 public static void SaveImage(Image image, Stream stream, ImageFormat format)
 {
     RetryUtility.Retry(() => image.Save(stream, format),
                        RetryUtility.kDefaultMaxRetryAttempts,
                        RetryUtility.kDefaultRetryDelay,
                        new HashSet <Type>
     {
         Type.GetType("System.IO.IOException"),
         Type.GetType("System.Runtime.InteropServices.ExternalException")
     });
 }
Exemplo n.º 16
0
 /// <summary>
 /// Save an Xml document. This should be equivalent to doc.Save(path) except for extra robustness (and slowness).
 /// </summary>
 /// <param name="doc"></param>
 /// <param name="path"></param>
 public static void SaveXml(XmlDocument doc, string path)
 {
     RetryUtility.Retry(() =>
     {
         using (var stream = RobustFile.Create(path))
         {
             doc.Save(stream);
             stream.Close();
         }
     });
 }
Exemplo n.º 17
0
 public static void WriteZipComment(string comment, string zipPath)
 {
     RetryUtility.Retry(() =>
     {
         using (var zipFile = new ZipFile(zipPath))
         {
             zipFile.BeginUpdate();
             zipFile.SetComment(comment);
             zipFile.CommitUpdate();
         }
     });
 }
Exemplo n.º 18
0
 public static void WriteAllBytes(string path, byte[] bytes)
 {
     RetryUtility.Retry(() =>
     {
         // This forces it to block until the data is really safely on disk.
         using (var f = File.Create(path, FileStreamBufferSize, FileOptions.WriteThrough))
         {
             f.Write(bytes, 0, bytes.Length);
             f.Close();
         }
     });
 }
Exemplo n.º 19
0
        protected void MakeSamplePngImageWithMetadata(string path, int width = 10, int height = 10)
        {
            var x = new Bitmap(width, height);

            RobustImageIO.SaveImage(x, path, ImageFormat.Png);
            x.Dispose();
            using (var img = PalasoImage.FromFileRobustly(path))
            {
                img.Metadata.Creator         = "joe";
                img.Metadata.CopyrightNotice = "Copyright 1999 by me";
                RetryUtility.Retry(() => img.SaveUpdatedMetadataIfItMakesSense());
            }
        }
Exemplo n.º 20
0
        public static void WriteAllTopLevelFilesToZip(string destPath, string sourceDir)
        {
            RetryUtility.Retry(() =>
            {
                Directory.CreateDirectory(Path.GetDirectoryName(destPath));
                var zipFile = new BloomZipFile(destPath);
                foreach (var path in Directory.EnumerateFiles(sourceDir))
                {
                    zipFile.AddTopLevelFile(path);
                }

                zipFile.Save();
            });
        }
Exemplo n.º 21
0
 /// <summary>
 /// Load a PalasoImage from a file, trying several times if needed.
 /// </summary>
 /// <remarks>
 /// This would logically belong in SIL.Core.IO.RobustIO except that PalasoImage is in SIL.Windows.Forms.
 /// </remarks>
 public static PalasoImage FromFileRobustly(string path)
 {
     return(RetryUtility.Retry(() => PalasoImage.FromFile(path),
                               RetryUtility.kDefaultMaxRetryAttempts,
                               RetryUtility.kDefaultRetryDelay,
                               new HashSet <Type>
     {
         Type.GetType("System.IO.IOException"),
         // Odd type to catch... but it seems that Image.FromFile (which is called in the bowels of PalasoImage.FromFile)
         // throws OutOfMemoryException when the file is inaccessible.
         // See http://stackoverflow.com/questions/2610416/is-there-a-reason-image-fromfile-throws-an-outofmemoryexception-for-an-invalid-i
         Type.GetType("System.OutOfMemoryException")
     }));
 }
Exemplo n.º 22
0
        public void WriteMessage(TeamCollectionMessage message)
        {
            Messages.Add(message);
            SIL.Reporting.Logger.WriteEvent(message.TextForDisplay);
            TeamCollectionManager.RaiseTeamCollectionStatusChanged();
            // Using Environment.NewLine here means the format of the file will be appropriate for the
            // computer we are running on. It's possible a shared collection might be used by both
            // Linux and Windows. But that's OK, because .NET line reading accepts either line
            // break on either platform.
            var toPersist = message.ToPersistedForm + Environment.NewLine;

            // There ought to be a RobustFile.AppendAllText, but there isn't.
            // However, as this promises to close the file each call, it should be pretty reliable.
            RetryUtility.Retry(() => File.AppendAllText(_logFilePath, toPersist));
        }
Exemplo n.º 23
0
 public static void DeleteDirectory(string path, bool recursive = false)
 {
     if (recursive)
     {
         var succeeded = DeleteDirectoryAndContents(path);
         if (!succeeded)
         {
             throw new IOException("Could not delete directory " + path);
         }
     }
     else
     {
         RetryUtility.Retry(() => Directory.Delete(path, false));
     }
 }
Exemplo n.º 24
0
        private ConfigItem InternalAddConfigItem(string appName, string[] parentPathItemNames, string name, string friendlyName, string desc, string val, string sourceId, string valType, string valTypeEnum, bool isCompositeValue)
        {
            ConfigItem configItem = null;

            try
            {
                configItem = RetryUtility.RetryAction(() => ConfigServer.AddConfigItem(appName, parentPathItemNames, name, friendlyName, desc, val, sourceId, valType, valTypeEnum, isCompositeValue), _actionNumRetries, _actionRetryTimeout, true, null);
            }
            catch (Exception ex)
            {
                LocalLoggingService.Warning("配置项添加失败!AppName:{0},PathItemNames:{1},{2},异常信息:{3}", appName, string.Join(",", parentPathItemNames.ToArray()), name, ex);
                throw;
            }
            UpdateConfigItemCache(configItem);
            return(configItem);
        }
Exemplo n.º 25
0
        // Log in directly to parse server with name and password.
        // Some unit tests need it, since we haven't been able to get the new firebase login to work
        // except when Bloom is actually running.
        public bool TestOnly_LegacyLogIn(string account, string password)
        {
            _sessionToken = string.Empty;
            Account       = string.Empty;
            var request = MakeGetRequest("login");

            request.AddParameter("username", account.ToLowerInvariant());
            request.AddParameter("password", password);

            bool result = false;

            RetryUtility.Retry(() => {
                var response = Client.Execute(request);
                var dy       = JsonConvert.DeserializeObject <dynamic>(response.Content);
                try
                {
                    _sessionToken = dy.sessionToken;                     //there's also an "error" in there if it fails, but a null sessionToken tells us all we need to know
                }
                catch (RuntimeBinderException)
                {
                    // We are seeing this sometimes while running unit tests.
                    // This is simply an attempt to diagnose what is happening.
                    Console.WriteLine("Attempt to deserialize response content session token failed while attempting log in to parse (BL-4099).");
                    Console.WriteLine($"username: {request.Parameters.Where(p => p.Name == "username")}");
                    Console.WriteLine($"request.Resource: {request.Resource}");
                    Console.WriteLine($"response.IsSuccessful: {response.IsSuccessful}");
                    Console.WriteLine($"response.Content: {response.Content}");
                    Console.WriteLine($"response.ContentLength: {response.ContentLength}");
                    Console.WriteLine($"response.ContentType: {response.ContentType}");
                    Console.WriteLine($"response.ResponseStatus: {response.ResponseStatus}");
                    Console.WriteLine($"response.StatusCode: {response.StatusCode}");
                    Console.WriteLine($"response.StatusDescription: {response.StatusDescription}");
                    Console.WriteLine($"response.ErrorMessage: {response.ErrorMessage}");
                    Console.WriteLine($"response.ErrorException: {response.ErrorException}");
                    Console.WriteLine($"deserialized response.Content: {dy}");
                    throw;
                }
                _userId = dy.objectId;
                Account = account;
                result  = LoggedIn;
            }, 3, 2000, new HashSet <Type> {
                typeof(RuntimeBinderException).Assembly.GetType("Microsoft.CSharp.RuntimeBinder.RuntimeBinderException")
            });
            return(result);
        }
Exemplo n.º 26
0
        /// <summary>
        /// Write the named files in the specified source folder to the specified zip file.
        /// </summary>
        public static void WriteFilesToZip(string[] names, string sourceFolder, string destPath)
        {
            RetryUtility.Retry(() =>
            {
                Directory.CreateDirectory(Path.GetDirectoryName(destPath));
                var zipFile = new BloomZipFile(destPath);
                foreach (var name in names)
                {
                    var path = Path.Combine(sourceFolder, name);
                    if (!RobustFile.Exists(path))
                    {
                        continue;
                    }
                    zipFile.AddTopLevelFile(path, true);
                }

                zipFile.Save();
            });
        }
Exemplo n.º 27
0
    private static SQLiteConnection GetConnection(string folderPath)
    {
        SQLiteConnection db = null;
        var path            = GetDatabasePath(folderPath);

        RetryUtility.Retry(
            () => db = new SQLiteConnection(path));
        if (db == null)
        {
            throw new ApplicationException("Could not open history db for" + path);
        }

        // For now, we're keeping things simple by just not assuming *anything*, even that the tables are there.
        // If we find that something is slow or sqllite dislikes this approach, we can do something more complicated.

        db.CreateTable <BookHistoryBook>();
        db.CreateTable <BookHistoryEvent>();
        return(db);
    }
        /// <summary>
        /// Runs the specified <see cref="ISessionProxyBackend"/> action against the backend proxy service.
        /// </summary>
        /// <param name="action">The action.</param>
        public static void Run(Action <ISessionProxyBackend> action)
        {
            var retryAction = new Action(() =>
            {
                using (var proxy = SessionProxyBackendClient.Create(GlobalDataStore.Manifest.Dispatcher, GlobalDataStore.Manifest.SessionId))
                {
                    action(proxy.Channel);
                }
            });

            try
            {
                RetryUtility.RetryAction(retryAction, 2, TimeSpan.FromSeconds(1), new List <Type>()
                {
                    typeof(Exception)
                });
            }
            catch (Exception ex)
            {
                TraceFactory.Logger.Error("Unable to communicate with Session Proxy backend: {0}".FormatWith(ex.Message));
            }
        }
Exemplo n.º 29
0
        /// <summary>
        /// Extract the files in the zip to the specified destination. Then, call filesToDeleteIfNotInZip
        /// and if any of the files it returned were not in the zip, delete them from the destFolder.
        /// </summary>
        public static void ExtractFolderFromZip(string destFolder, string zipPath,
                                                Func <HashSet <string> > filesToDeleteIfNotInZip)
        {
            byte[] buffer     = new byte[4096];         // 4K is optimum
            var    filesInZip = new HashSet <string>();

            RetryUtility.Retry(() =>
            {
                using (var zipFile = new ZipFile(zipPath))
                {
                    foreach (ZipEntry entry in zipFile)
                    {
                        filesInZip.Add(entry.Name);
                        var fullOutputPath = Path.Combine(destFolder, entry.Name);

                        var directoryName = Path.GetDirectoryName(fullOutputPath);
                        if (!String.IsNullOrEmpty(directoryName))
                        {
                            Directory.CreateDirectory(directoryName);
                        }
                        using (var instream = zipFile.GetInputStream(entry))
                            using (var writer = RobustFile.Create(fullOutputPath))
                            {
                                StreamUtils.Copy(instream, writer, buffer);
                            }
                    }
                }
            });

            // Remove any sharing-eligible files that are NOT in the zip
            var filesToDelete = filesToDeleteIfNotInZip();

            filesToDelete.ExceptWith(filesInZip);
            foreach (var discard in filesToDelete)
            {
                RobustFile.Delete(Path.Combine(destFolder, discard));
            }
        }
Exemplo n.º 30
0
 /// <summary>
 /// As in Windows, this version inserts a BOM at the start of the file. Thus,
 /// in particular, the file produced by WriteString(x,y, Encoding.UTF8) is not
 /// the same as that produced by WriteString(x, y), though both are encoded
 /// using UTF8.
 /// On Windows, the BOM is inserted even if contents is an empty string.
 /// As of Mono 3.4, Linux instead writes an empty file. We think this is a bug.
 /// Accordingly, this version is consistent and writes a BOM on both platforms,
 /// even for empty strings.
 /// </summary>
 /// <param name="path"></param>
 /// <param name="contents"></param>
 /// <param name="encoding"></param>
 public static void WriteAllText(string path, string contents, Encoding encoding)
 {
     // It's helpful to check for these first so we don't actaully create the file.
     if (contents == null)
     {
         throw new ArgumentNullException(@"contents", @"contents must not be null");
     }
     if (encoding == null)
     {
         throw new ArgumentNullException(@"encoding", @"encoding must not be null");
     }
     RetryUtility.Retry(() =>
     {
         // This forces it to block until the data is really safely on disk.
         using (var f = File.Create(path, FileStreamBufferSize, FileOptions.WriteThrough))
         {
             var preamble = encoding.GetPreamble();
             f.Write(preamble, 0, preamble.Length);
             var content = encoding.GetBytes(contents);
             f.Write(content, 0, content.Length);
             f.Close();
         }
     });
 }