Inheritance: MonoBehaviour
Example #1
0
        internal static async Task DownloadMetadataAsync(
            string baseUrl, int startIndex, int count,
            string entryJsonPath, string x265JsonPath, string h264JsonPath, string ytsJsonPath, string libraryJsonPath,
            Action <string>?log = null)
        {
            log ??= message => Trace.WriteLine(message);
            List <string> entryLinks = new();

            using WebClient webClient = new();
            await Enumerable
            .Range(startIndex, count)
            .Select(index => $"{baseUrl}/page/{index}/")
            .ForEachAsync(async url =>
            {
                try
                {
                    string html = await Retry.FixedIntervalAsync(async() => await webClient.DownloadStringTaskAsync(url));
                    CQ listCQ   = html;
                    log($"Done {url}");
                    listCQ
                    .Find("h1.entry-title a")
                    .Select(entryLink => entryLink.GetAttribute("href"))
                    .ForEach(entryLinks.Add);
                }
                catch (Exception exception) when(exception.IsNotCritical())
                {
                    log(exception.ToString());
                }
            });

            ConcurrentDictionary <string, EntryMetadata> entryMetadata = new();
            await entryLinks.ParallelForEachAsync(async entryLink =>
            {
                using WebClient webClient = new();
                try
                {
                    string html  = await Retry.FixedIntervalAsync(async() => await webClient.DownloadStringTaskAsync(entryLink));
                    CQ entryCQ   = html;
                    string title = entryCQ.Find("h1.entry-title").Text().Trim();
                    log($"Done {title} {entryLink}");
                    entryMetadata[entryLink] = new EntryMetadata(
                        title,
                        entryCQ.Find("div.entry-content").Html());
                }
                catch (Exception exception) when(exception.IsNotCritical())
                {
                    log(exception.ToString());
                }
            }, 4);

            string jsonString = JsonSerializer.Serialize(entryMetadata, new() { WriteIndented = true });
            await File.WriteAllTextAsync(entryJsonPath, jsonString);

            Dictionary <string, RarbgMetadata[]> x265Metadata = JsonSerializer.Deserialize <Dictionary <string, RarbgMetadata[]> >(await File.ReadAllTextAsync(x265JsonPath)) !;
            Dictionary <string, RarbgMetadata[]> h264Metadata = JsonSerializer.Deserialize <Dictionary <string, RarbgMetadata[]> >(await File.ReadAllTextAsync(h264JsonPath)) !;
            Dictionary <string, YtsMetadata[]>   ytsMetadata  = JsonSerializer.Deserialize <Dictionary <string, YtsMetadata[]> >(await File.ReadAllTextAsync(ytsJsonPath)) !;
            Dictionary <string, Dictionary <string, VideoMetadata> > libraryMetadata = JsonSerializer.Deserialize <Dictionary <string, Dictionary <string, VideoMetadata> > >(await File.ReadAllTextAsync(libraryJsonPath)) !;

            entryMetadata
            .SelectMany(entry => Regex
                        .Matches(entry.Value.Content, "imdb.com/title/(tt[0-9]+)")
                        .Where(match => match.Success)
                        .Select(match => (Link: entry.Key, match.Groups[1].Value)))
            .Distinct(imdbId => imdbId.Value)
            .ForEach(imdbId =>
            {
                if (libraryMetadata.ContainsKey(imdbId.Value) && libraryMetadata[imdbId.Value].Any())
                {
                    libraryMetadata[imdbId.Value].ForEach(video => log(video.Key));
                    log(string.Empty);
                    return;
                }

                if (x265Metadata.ContainsKey(imdbId.Value))
                {
                    log(imdbId.Link);
                    x265Metadata[imdbId.Value].ForEach(metadata => log($"{metadata.Link} {metadata.Title}"));
                    log(string.Empty);
                    return;
                }

                if (h264Metadata.ContainsKey(imdbId.Value))
                {
                    log(imdbId.Link);
                    h264Metadata[imdbId.Value].ForEach(metadata => log($"{metadata.Link} {metadata.Title}"));
                    log(string.Empty);
                    return;
                }

                if (ytsMetadata.ContainsKey(imdbId.Value))
                {
                    log(imdbId.Link);
                    ytsMetadata[imdbId.Value].ForEach(metadata => log($"{metadata.Link} {metadata.Title}"));
                    log(string.Empty);
                }
            });
        }
Example #2
0
 public void TestOneTimeSetup()
 {
     _mainWindow = Retry.WhileNull(() => Application.GetMainWindow(Automation), TimeSpan.FromSeconds(1)).Result;
     Assert.That(_mainWindow, Is.Not.Null);
 }
Example #3
0
 public override IIdentity GetIdentity(string username)
 {
     return(_bridge.Wrap <WrapperForIdentity, Identity>(Retry.Do(() => GroupSecurityService.ReadIdentity(SearchFactor.AccountName, username, QueryMembership.None))));
 }
        public virtual void UpdateTask(ScheduleTask task)
        {
            Guard.NotNull(task, nameof(task));

            try
            {
                using (var scope = new DbContextScope(_taskRepository.Context, autoCommit: true))
                {
                    Retry.Run(() => _taskRepository.Update(task), 3, TimeSpan.FromMilliseconds(50), (attempt, exception) =>
                    {
                        var ex = exception as DbUpdateConcurrencyException;
                        if (ex == null)
                        {
                            return;
                        }

                        var entry   = ex.Entries.Single();
                        var current = (ScheduleTask)entry.CurrentValues.ToObject();                         // from current scope

                        // When 'StopOnError' is true, the 'Enabled' property could have been set to true on exception.
                        var prop            = entry.Property("Enabled");
                        var enabledModified = !prop.CurrentValue.Equals(prop.OriginalValue);

                        // Save current cron expression
                        var cronExpression = task.CronExpression;

                        // Fetch Name, CronExpression, Enabled & StopOnError from database
                        // (these were possibly edited thru the backend)
                        _taskRepository.Context.ReloadEntity(task);

                        // Do we have to reschedule the task?
                        var cronModified = cronExpression != task.CronExpression;

                        // Copy execution specific data from current to reloaded entity
                        task.LastEndUtc      = current.LastEndUtc;
                        task.LastError       = current.LastError;
                        task.LastStartUtc    = current.LastStartUtc;
                        task.LastSuccessUtc  = current.LastSuccessUtc;
                        task.ProgressMessage = current.ProgressMessage;
                        task.ProgressPercent = current.ProgressPercent;
                        task.NextRunUtc      = current.NextRunUtc;
                        if (enabledModified)
                        {
                            task.Enabled = current.Enabled;
                        }
                        if (task.NextRunUtc.HasValue && cronModified)
                        {
                            // reschedule task
                            task.NextRunUtc = GetNextSchedule(task);
                        }

                        if (attempt == 3)
                        {
                            _taskRepository.Update(task);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }
Example #5
0
        public override async Task <int> Main()
        {
            try
            {
                Environment.CurrentDirectory = PathManager.FindResource();
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Use `nfpm scaffold` to generate a NFive plugin in this directory");

                return(1);
            }

            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                if (args.Name.Contains(".resources"))
                {
                    return(null);
                }

                var fileName = args.Name.Substring(0, args.Name.IndexOf(",", StringComparison.InvariantCultureIgnoreCase)) + ".dll";

                if (File.Exists(fileName))
                {
                    return(Assembly.Load(File.ReadAllBytes(fileName)));
                }

                var path = Directory.EnumerateFiles("plugins", "*.dll", SearchOption.AllDirectories).FirstOrDefault(f => Path.GetFileName(f) == fileName);

                if (string.IsNullOrEmpty(path))
                {
                    throw new FileLoadException(args.Name);
                }

                return(Assembly.Load(File.ReadAllBytes(path)));
            };

            DTE dte = null;

            try
            {
                if (!File.Exists(this.Sln))
                {
                    this.Sln = Directory.EnumerateFiles(Environment.CurrentDirectory, "*.sln", SearchOption.TopDirectoryOnly).FirstOrDefault();
                }
                if (this.Sln == null || !File.Exists(this.Sln))
                {
                    this.Sln = Input.String("Visual Studio SLN solution file");
                }

                Console.Write("Searching for existing Visual Studio instance...");

                dte = VisualStudio.GetInstances().FirstOrDefault(env => env.Solution.FileName == this.Sln);

                if (dte != default)
                {
                    Console.WriteLine(" found");
                }
                else
                {
                    Console.WriteLine(" not found");
                    Console.WriteLine("Starting new Visual Studio instance...");

                    dte = (DTE)Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.DTE", true), true);

                    this.existingInstance = false;
                }

                Console.WriteLine("Opening solution");

                var solution = Retry.Do(() => dte.Solution);

                if (!Retry.Do(() => solution.IsOpen))
                {
                    Retry.Do(() => solution.Open(this.Sln));
                }

                Console.WriteLine("Building solution");

                solution.SolutionBuild.Build(true);

                Console.WriteLine("Searching for projects");

                var pp = Retry.Do(() => solution.Projects.Cast <Project>().ToList());

                var ppp = Retry.Do(() => pp.Where(p => !string.IsNullOrWhiteSpace(p.FullName)).ToList());

                foreach (var project in ppp)
                {
                    Console.WriteLine($"  Analyzing project {Retry.Do(() => project.Name)}...");

                    var projectPath = Path.GetDirectoryName(Retry.Do(() => project.FullName)) ?? string.Empty;
                    var outputPath  = Path.GetFullPath(Path.Combine(projectPath, Retry.Do(() => project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString()), Retry.Do(() => project.Properties.Item("OutputFileName").Value.ToString())));

                    var asm = Assembly.Load(File.ReadAllBytes(outputPath));
                    if (!this.Sdk && asm.GetCustomAttribute <ServerPluginAttribute>() == null)
                    {
                        continue;
                    }

                    var contextType = asm.DefinedTypes.FirstOrDefault(t => t.BaseType != null && t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == typeof(EFContext <>));
                    if (contextType == default)
                    {
                        continue;
                    }

                    Console.WriteLine($"    Loaded {outputPath}");

                    Console.WriteLine($"    Found DB context: {contextType.Name}");

                    var props = contextType
                                .GetProperties()
                                .Where(p =>
                                       p.CanRead &&
                                       p.CanWrite &&
                                       p.PropertyType.IsGenericType &&
                                       p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet <>) &&
                                       p.PropertyType.GenericTypeArguments.Any(t => !string.IsNullOrEmpty(t.Namespace) && t.Namespace.StartsWith("NFive.SDK."))) // TODO
                                .Select(t => $"dbo.{t.Name}")                                                                                                    // TODO
                                .ToArray();

                    if (!this.Sdk)
                    {
                        Console.WriteLine($"    Excluding tables: {string.Join(", ", props)}");
                    }

                    var migrationsPath = "Migrations";

                    if (!Directory.Exists(Path.Combine(projectPath, migrationsPath)))
                    {
                        migrationsPath = Input.String("Migration source code folder", "Migrations");                                                                                   // TODO: Validate
                    }
                    var @namespace = $"{project.Properties.Item("RootNamespace").Value}.{migrationsPath}";

                    if (asm.DefinedTypes.Any(t => t.BaseType != null && t.BaseType == typeof(DbMigration) && t.Namespace == @namespace && t.Name == this.Name))
                    {
                        throw new Exception($"A migration named \"{this.Name}\" already exists at \"{@namespace}.{this.Name}\", please use another migration name.");
                    }

                    Console.WriteLine("    Generating migration...");

                    var migrationsConfiguration = new DbMigrationsConfiguration
                    {
                        AutomaticMigrationDataLossAllowed = false,
                        AutomaticMigrationsEnabled        = false,
                        CodeGenerator       = new NFiveMigrationCodeGenerator(this.Sdk ? new string[] { } : props),
                        ContextType         = contextType,
                        ContextKey          = $"{@namespace}.Configuration",
                        MigrationsAssembly  = asm,
                        MigrationsDirectory = migrationsPath,
                        MigrationsNamespace = @namespace,
                        TargetDatabase      = new DbConnectionInfo(this.Database, "MySql.Data.MySqlClient")
                    };

                    var ms = new MigrationScaffolder(migrationsConfiguration);

                    if (this.RunMigrations)
                    {
                        var migrator = new DbMigrator(migrationsConfiguration);

                        if (migrator.GetPendingMigrations().Any())
                        {
                            Console.WriteLine("    Running existing migrations...");

                            foreach (var migration in migrator.GetPendingMigrations())
                            {
                                Console.WriteLine($"        Running migration: {migration}");

                                migrator.Update(migration);
                            }
                        }
                    }

                    Console.WriteLine("    Scaffolding migration...");

                    var src = ms.Scaffold(this.Name, false);

                    var file = Path.Combine(projectPath, migrationsPath, $"{src.MigrationId}.{src.Language}");

                    Console.WriteLine($"    Writing migration: {file}");

                    File.WriteAllText(file, src.UserCode);

                    Console.WriteLine("    Updating project...");

                    project.ProjectItems.AddFromFile(file);
                    project.Save();
                }

                Console.WriteLine("Building solution...");

                solution.SolutionBuild.Build(true);

                if (!this.existingInstance)
                {
                    Console.WriteLine("Quitting Visual Studio instance");

                    dte.Quit();
                }

                Console.WriteLine("Done");

                return(await Task.FromResult(0));
            }
            catch (ReflectionTypeLoadException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(string.Join(Environment.NewLine, ex.LoaderExceptions.Select(e => e.Message)));

                return(1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return(1);
            }
            finally
            {
                if (!this.existingInstance)
                {
                    dte.Quit();
                }
            }
        }
        public async Task <bool> Login(string login, string password)
        {
            await Retry.Do(async() => await _client.SignIn(login, password), RetryCount, RetryDelay);

            return(await Retry.Do(async() => await _client.IsAuthenticated(), RetryCount, RetryDelay));
        }
Example #7
0
        public async Task ShouldRunWebsite()
        {
            await using var testSolution = await CopyTestAssets("demowebsite");

            var projectPath = Path.Join(testSolution.Value, "demowebsite");

            await using var iisHost = new IISHost(new Dictionary <string, string>());
            using var cancel        = new CancellationTokenSource();

            var port = CreateRandomPort();
            var name = "demosolutionsitename";
            var run  = iisHost.Run(_console, new CommandArguments
            {
                Path = new DirectoryInfo(projectPath),
                Port = port,
                Name = name
            }, cancel.Token);

            try
            {
                var httpClient = new HttpClient();
                var res        = await Retry.Get(() => httpClient.GetAsync($"http://localhost:{port}/DemoApi"), 20, Retry.ConstantTimeBackOff());

                if (!res.IsSuccessStatusCode)
                {
                    _console.Out.WriteLine(await res.Content.ReadAsStringAsync());
                }

                Assert.True(res.IsSuccessStatusCode);

                var json = await JsonSerializer.DeserializeAsync <Dictionary <string, string> >(await res.Content.ReadAsStreamAsync());

                Assert.Equal(name, json["siteName"]);
            }
            finally
            {
                cancel.Cancel();
                await run;
            }
        }
Example #8
0
        public List <Issue> GetAllCommunityIssues(bool pulls, DateTime?since = null)
        {
            var issues = new List <Issue>();

            var pullRequestService = new GitHubService();
            var hqMembers          = pullRequestService.GetHqMembers();
            var teamMembers        = pullRequestService.GetTeamMembers();

            foreach (var directory in Directory.EnumerateDirectories(IssuesBaseDirectory))
            {
                var directoryName   = directory.Split('\\').Last();
                var repositoryName  = directoryName.Substring(directoryName.LastIndexOf("__", StringComparison.Ordinal) + 2);
                var issuesDirectory = directory + "\\issues\\";

                if (pulls)
                {
                    issuesDirectory = issuesDirectory + "\\pulls\\";
                }

                if (Directory.Exists(issuesDirectory) == false)
                {
                    continue;
                }

                var issueFiles = Directory.EnumerateFiles(issuesDirectory, "*.combined.json");

                var reviewers = new List <string>();
                reviewers.AddRange(hqMembers);
                var team = teamMembers.FirstOrDefault(x => x.TeamName == repositoryName);
                if (team != null)
                {
                    reviewers.AddRange(team.Members);
                }

                foreach (var file in issueFiles)
                {
                    // Skip the file if older than the specified timestamp
                    if (since != null && File.GetLastWriteTimeUtc(file) < since.Value)
                    {
                        continue;
                    }

                    Issue item = null;
                    Retry.Do(() =>
                    {
                        var fileContent = File.ReadAllText(file);
                        item            = JsonConvert.DeserializeObject <Issue>(fileContent);
                    }, TimeSpan.FromMilliseconds(200), 3);

                    if (item == null)
                    {
                        continue;
                    }

                    // Exclude issues created by HQ
                    if (hqMembers.Contains(item.User.Login.ToLowerInvariant()))
                    {
                        continue;
                    }

                    item.RepositoryName = repositoryName;

                    foreach (var comment in item.Comments)
                    {
                        var commenter = comment.User.Login.ToLowerInvariant();
                        if (item.FirstPrTeamOrHqComment == null && reviewers.Contains(commenter))
                        {
                            item.FirstPrTeamOrHqComment = comment.CreateDateTime.ToLocalTime();
                        }
                    }

                    issues.Add(item);
                }
            }

            return(issues);
        }
Example #9
0
 public void CleanUp()
 {
     Retry.Do(DoCleanUp, retryInterval: TimeSpan.FromSeconds(2), retryCount: 10);
 }
Example #10
0
 public static async Task <PlayerSource> GetAsync(YoutubeHttpClient httpClient, string url) =>
 await Retry.WrapAsync(async() =>
 {
     var raw = await httpClient.GetStringAsync(url);
     return(Parse(raw));
 });
Example #11
0
 protected virtual Task <T> ExecuteWithRetry <T>(Func <Task <T> > action)
 {
     return(Retry.RunAsync(action, 3, TimeSpan.FromMilliseconds(100), RetryOnTransientException));
 }
Example #12
0
 public RetryContext(Retry retry)
 {
     Retry = retry;
     ConditionHandles = retry.Conditions.Select(condition => new RetryConditionHandle(this, condition)).ToList();
 }
Example #13
0
 public void Configure(IServiceConfigurator configurator)
 {
     configurator.UseRetry(Retry.Interval(5, TimeSpan.FromMilliseconds(100)));
 }
Example #14
0
 public virtual void Execute(Retry retry)
 {
     repository.MakePersistent(retry);
 }
Example #15
0
 public void OneTimeTearDown()
 {
     Application.KillLaunched(ExeFileName);
     Retry.ResetTime();
 }
Example #16
0
 public async Task <T> Execute <T>(Func <Task <T> > func,
                                   CancellationToken cancellationToken = default(CancellationToken))
 {
     return(await Retry.Do <T, Exception>(func, _retryInterval, _retryCountOnException,
                                          cancellationToken : cancellationToken));
 }
Example #17
0
 public RetryCondition(Retry retry, Func<RetryConditionHandle, bool> predicate)
 {
     Retry = retry;
     Retry.Conditions.Add(this);
     FilterCondition = predicate;
 }
Example #18
0
        /// <summary>
        /// Process operations and manage session
        /// </summary>
        /// <returns></returns>
        private async Task RunAsync()
        {
            // Create a static keep alive operation object for the the session
            var keepAlive = (0, new KeepAlive(_keepAlive));

            // We cache the current operation as part of sessions state and the priority
            // here if operation should be retried next loop
            var priority = 0;

            var reconnect  = false;
            var recreate   = false;
            var retryCount = 0;

            // Save identity and certificate to update session if there are changes.
            var identity = _curOperation?.Identity ??
                           new UserIdentity(_connection.User.ToUserIdentityToken());

            try {
                while (!_cts.Token.IsCancellationRequested)
                {
                    Exception ex = null;
                    if (_session == null)
                    {
                        // Try create session
                        recreate  = false;
                        reconnect = false;
                        try {
                            _logger.Debug("Creating new session via {endpoint}...",
                                          _endpointUrl);
                            _session = await CreateSessionAsync(identity);

                            _logger.Debug("Session via {endpoint} created.",
                                          _endpointUrl);
                        }
                        catch (Exception e) {
                            _logger.Information(
                                "{message} creating session via {endpoint} - retry.",
                                e.Message, _endpointUrl);
                            _logger.Debug(e, "Error connecting - retry.");
                            ex = e;
                        }
                    }
                    if (recreate)
                    {
                        // Try recreate session from current one
                        try {
                            _logger.Debug("Recreating session via {endpoint}...",
                                          _endpointUrl);
                            var session = await Task.Run(() => Session.Recreate(_session), _cts.Token);

                            _logger.Debug("Session recreated via {endpoint}.",
                                          _endpointUrl);

                            Try.Op(() => _session.Close());
                            _session = session;
                            recreate = false;
                        }
                        catch (Exception e) {
                            ex = e;
                            _logger.Information("{message} while recreating session " +
                                                "via {endpoint} - create new one.",
                                                e.Message, _endpointUrl);
                            _logger.Debug(e, "Error connecting - create new session.");
                            _session?.Close();
                            _session = null;
                        }
                    }
                    if (reconnect)
                    {
                        // Try reconnect the session
                        try {
                            _logger.Debug("Reconnecting session via {endpoint}...",
                                          _endpointUrl);
#pragma warning disable RECS0002 // Convert anonymous method to method group
                            await Task.Run(() => _session.Reconnect(), _cts.Token);

#pragma warning restore RECS0002 // Convert anonymous method to method group
                            _logger.Debug("Session reconnected via {endpoint}.",
                                          _endpointUrl);
                            reconnect = false;
                        }
                        catch (Exception e) {
                            ex        = e;
                            recreate  = true;
                            reconnect = false;
                            if (e is ServiceResultException sre)
                            {
                                if (sre.StatusCode == StatusCodes.BadTcpEndpointUrlInvalid ||
                                    sre.StatusCode == StatusCodes.BadTcpInternalError ||
                                    sre.StatusCode == StatusCodes.BadCommunicationError ||
                                    sre.StatusCode == StatusCodes.BadNotConnected)
                                {
                                    if (retryCount < kMaxReconnectAttempts && Pending > 0)
                                    {
                                        _logger.Information("{message} while reconnecting session" +
                                                            " via {endpoint} - retry...",
                                                            sre.Message, _endpointUrl);
                                        recreate  = false;
                                        reconnect = true; // Try again
                                    }
                                }
                            }
                            _logger.Debug(e,
                                          "Error reconnecting via {endpoint} - recreating session...",
                                          _endpointUrl);
                        }
                    }

                    // Failed to connect
                    if (recreate || reconnect || _session == null)
                    {
                        await NotifyConnectivityStateChangeAsync(ToConnectivityState(ex));

                        if (ex is ServiceResultException sre)
                        {
                            ex = sre.ToTypedException();
                        }

                        // Compress operations queue
                        var operations = new List <(int, SessionOperation)>();
                        while (_queue.TryDequeue(out var op))
                        {
                            if (op.Item2 == null)
                            {
                                break;
                            }
                            operations.Add(op);
                        }
                        foreach (var op in operations)
                        {
                            if (ex != null && !op.Item2.ShouldRetry(ex))
                            {
                                op.Item2.Fail(ex);
                            }
                            else if (!op.Item2.IsCompleted())
                            {
                                // Re-add the still valid ones...
                                _queue.Enqueue(op);
                            }
                        }

                        ++retryCount;
                        // Try again to connect with an exponential delay
                        var delay = Retry.GetExponentialDelay(retryCount,
                                                              kMaxReconnectDelayWhenPendingOperations / 2, kMaxRetries);
                        if (delay > kMaxReconnectDelayWhenPendingOperations)
                        {
                            //
                            // If pending operations, do not delay longer than 5 seconds
                            // otherwise wait for 2 hours or until new operation is queued.
                            //
                            delay = Pending != 0 ? kMaxReconnectDelayWhenPendingOperations :
                                    kMaxReconnectDelayWhenNoPendingOperations;
                        }
                        _logger.Information("Try to connect via {endpoint} in {delay} ms...",
                                            _endpointUrl, delay);
                        // Wait for either the retry delay to pass or until new operation is added
                        await WaitForNewlyEnqueuedOperationAsync(delay);

                        continue;
                    }

                    // We have a session that should work for us, get next operation and
                    // complete it.
                    retryCount = 0;
                    recreate   = false;
                    reconnect  = false;

                    if (_curOperation == null)
                    {
                        if (!_queue.TryDequeue(out var next))
                        {
                            // Wait for enqueue or keep alive timeout
                            var timeout = await WaitForNewlyEnqueuedOperationAsync(
                                (int)_keepAlive.TotalMilliseconds);

                            // Check cancellation
                            if (_cts.Token.IsCancellationRequested)
                            {
                                continue;
                            }
                            if (timeout || !_queue.TryDequeue(out next))
                            {
                                next = keepAlive;
                            }
                        }
                        (priority, _curOperation) = next;
                        System.Diagnostics.Debug.Assert(_curOperation != null);
                    }
                    if (_curOperation.IsCompleted())
                    {
                        _curOperation = null; // Already completed because timeout or cancellation, get next
                        continue;
                    }
                    if (_curOperation is KeepAlive)
                    {
                        _logger.Verbose("Sending keep alive message...");
                    }
                    try {
                        // Check if the desired user identity is the same as the current one
                        if (!Utils.IsEqual((_curOperation.Identity ?? identity).GetIdentityToken(),
                                           _session.Identity.GetIdentityToken()))
                        {
                            // Try Elevate or de-elevate session
                            try {
                                _logger.Verbose("Updating session user identity...");
                                await Task.Run(() => _session.UpdateSession(_curOperation.Identity,
                                                                            _session.PreferredLocales));

                                _logger.Debug("Updated session user identity.");
                            }
                            catch (ServiceResultException sre) {
                                if (StatusCodeEx.IsSecurityError(sre.StatusCode))
                                {
                                    _logger.Debug(sre, "Failed updating session identity");
                                    await NotifyConnectivityStateChangeAsync(ToConnectivityState(sre));

                                    _curOperation.Fail(sre.ToTypedException());
                                    _curOperation = null;
                                    continue;
                                }
                                throw;
                            }
                        }

                        await Task.Run(() => _curOperation.Complete(_session), _cts.Token);

                        _lastActivity = DateTime.UtcNow;
                        await NotifyConnectivityStateChangeAsync(EndpointConnectivityState.Ready);

                        _logger.Verbose("Session operation completed.");
                        _curOperation = null;
                    }
                    catch (Exception e) {
                        // Process exception - first convert sre into non opc exception
                        var oex = e;
                        if (e is ServiceResultException sre)
                        {
                            e = sre.ToTypedException();
                        }
                        // See which ones we can retry, and which ones we cannot
                        switch (e)
                        {
                        case TimeoutException te:
                        case ServerBusyException sb:
                            _logger.Debug(e, "Server timeout error.");
                            if (_curOperation.ShouldRetry(e))
                            {
                                _logger.Information("Timeout error talking {endpoint} " +
                                                    "- {error} - try again later...",
                                                    _endpointUrl, e.Message);
                                _queue.Enqueue(priority, _curOperation);
                                _curOperation = null;
                            }
                            else
                            {
                                reconnect = _curOperation is KeepAlive;
                                if (!reconnect)
                                {
                                    _logger.Error("Timeout error  talking to {endpoint} " +
                                                  "- {error} - fail user operation.",
                                                  _endpointUrl, e.Message);
                                }
                                _curOperation.Fail(e);
                                _curOperation = null;
                            }
                            break;

                        case ConnectionException cn:
                        case ProtocolException pe:
                        case CommunicationException ce:
                            _logger.Debug(e, "Server communication error.");
                            if (_curOperation.ShouldRetry(e))
                            {
                                _logger.Information("Communication error talking to {endpoint} " +
                                                    "- {error} - Reconnect and try again...",
                                                    _endpointUrl, e.Message);
                                // Reconnect session
                                reconnect = true;
                            }
                            else
                            {
                                reconnect = _curOperation is KeepAlive;
                                if (!reconnect)
                                {
                                    _logger.Error("Communication error talking to {endpoint} " +
                                                  "- {error} - fail user operation.",
                                                  _endpointUrl, e.Message);
                                }
                                _curOperation.Fail(e);
                                _curOperation = null;
                            }
                            break;

                        default:
                            if (!_cts.IsCancellationRequested)
                            {
                                // App error - fail and continue
                                _logger.Debug(e, "Application error occurred talking to {endpoint} " +
                                              "- fail operation.",
                                              _endpointUrl, e.Message);
                                reconnect = _curOperation is KeepAlive;
                            }
                            else
                            {
                                // Session was closed while operation in progress - Set cancelled
                                _logger.Error("Session via {endpoint} was closed " +
                                              "while operation in progress - cancel operation.",
                                              _endpointUrl, e.Message);
                                reconnect = false;
                            }
                            _curOperation.Fail(e);
                            _curOperation = null;
                            break;
                        }
                        if (reconnect)
                        {
                            await NotifyConnectivityStateChangeAsync(ToConnectivityState(oex, false));
                        }
                    }
                } // end while
            }
            catch (OperationCanceledException) {
                // Expected on cancellation
            }
            catch (Exception ex) {
                _logger.Error(ex, "Session operation processor exited with exception");
            }
            finally {
                if (_curOperation != null && _curOperation != keepAlive.Item2)
                {
                    _queue.Enqueue(priority, _curOperation);
                }
                _lastActivity = DateTime.MinValue;

                _logger.Verbose("Closing session...");
                _session?.Close();
                _session = null;
                _logger.Debug("Session closed.");
                keepAlive.Item2?.Dispose();
            }
            _logger.Verbose("Processor stopped.");
        }
Example #19
0
        public async Task ShouldNotWorkIfNotBuilt()
        {
            await using var testSolution = await CopyTestAssets("demowebsite");

            var projectPath = Path.Join(testSolution.Value, "demowebsite");

            await using var iisHost = new IISHost(new Dictionary <string, string>());
            using var cancel        = new CancellationTokenSource();

            var port = CreateRandomPort();
            var name = "demosolutionsitename";
            var run  = iisHost.Run(_console, new CommandArguments
            {
                Path    = new DirectoryInfo(projectPath),
                Port    = port,
                Name    = name,
                NoBuild = true
            }, cancel.Token);

            try
            {
                var httpClient = new HttpClient();
                var res        = await Retry.Get(() => httpClient.GetAsync($"http://localhost:{port}/DemoApi"), 20, Retry.ConstantTimeBackOff());

                Assert.False(res.IsSuccessStatusCode);
            }
            finally
            {
                cancel.Cancel();
                await run;
            }
        }
        public void Test1()
        {
            TimeSpan timeOut = TimeSpan.FromSeconds(20);
            var      app     = Application.Launch("notepad.exe");

            Retry.DefaultTimeout = timeOut;
            using (var automation = new UIA3Automation())
            {
                var window = app.GetMainWindow(automation);
                window.Title.Should().Be("Untitled - Notepad");
                var FileTab = window.FindAllDescendants().Single(x => x.Name == "File").AsMenuItem().WaitUntilClickable();
                FileTab.Click();

                var OpenTab = window.FindAllDescendants().Single(x => x.Name == "Open...").AsMenuItem().WaitUntilClickable();
                OpenTab.Click();


                Retry.WhileTrue(() => window.ModalWindows.Length == 1);
                var openFile = window.ModalWindows[0].AsWindow();
                openFile.Name.Should().Be("Open");

                var edit = openFile.FindFirstChild("1148").WaitUntilClickable();
                edit.Click();

                Retry.WhileTrue(() => edit.FrameworkAutomationElement.HasKeyboardFocus);
                Keyboard.Type(@"C:\Users\yyuur\source\repos\Practice6\NotepadAutomation\Test.txt");
                var fileOpen = openFile.FindFirstChild("1");
                Retry.DefaultTimeout = timeOut;
                fileOpen.WaitUntilClickable(timeOut);
                fileOpen.Click();



                Retry.WhileTrue(() => window.Title.Equals("Test.txt - Notepad"));


                var editText = window.FindAllDescendants().Single(x => x.Name == "Text Editor").AsTextBox();
                editText.Text.Should().Be("Hello");
                editText.Enter("SomeWords");


                FileTab = window.FindAllDescendants().Single(x => x.Name == "File").AsMenuItem().WaitUntilClickable();
                FileTab.Click();

                var SaveTab = window.FindAllDescendants().Single(x => x.Name == "Save As...").AsMenuItem().WaitUntilClickable();
                SaveTab.Click();


                var faker    = new Faker();
                var filename = String.Format("Test{0}", faker.System.FileName("txt"));
                var path     = @"C:\Users\yyuur\source\repos\Practice6\NotepadAutomation\Test.txt";


                Retry.WhileTrue(() => window.ModalWindows.Length == 1);
                var saveFile = window.ModalWindows[0].AsWindow();

                Keyboard.Type(path + filename);
                var fileName1 = saveFile.FindAllDescendants(x => x.ByClassName("AppControlHost")).First(x => x.ClassName == "File filename:");
                fileName1.AsTextBox().Text.Should().Be(path + filename);
                saveFile.FindFirstChild("1").AsButton().Invoke();


                app.Close();

                Retry.WhileFalse(() => File.Exists(path + filename));
                File.ReadAllText(path + filename).Should().Be("SomeWords");
                File.Delete(path + filename);
            }
        }
Example #21
0
 private Window GetWindow(string title)
 {
     return(Retry.For(
                () => application.GetWindows().First(x => x.Title.Contains(title)),
                TimeSpan.FromSeconds(5)));
 }
        public bool Start(HostControl hostControl)
        {
            var saga = new OrderStateMachine();
            var repo = new InMemorySagaRepository <Order>();

            _busObserver = new BusObserver();

            _busControl = BusConfigurator.ConfigureBus((cfg, host) =>
            {
                cfg.AddBusFactorySpecification(new BusObserverSpecification(() => _busObserver));

                cfg.ReceiveEndpoint(host, RabbitMqConstants.SagaQueue, e =>
                {
                    cfg.UseNLog(new LogFactory());

                    cfg.EnablePerformanceCounters();

                    e.UseRetry(Retry.Interval(5, TimeSpan.FromSeconds(5)));


                    e.UseCircuitBreaker(cb =>
                    {
                        cb.TripThreshold = 15;
                        cb.ResetInterval(TimeSpan.FromMinutes(5));
                        cb.TrackingPeriod  = TimeSpan.FromMinutes(1);
                        cb.ActiveThreshold = 10;
                    });

                    //e.UseRetry(Retry.Except(typeof(ArgumentException),
                    //    typeof(NotAcceptedStateMachineException)).Interval(10, TimeSpan.FromSeconds(5)));
                    //TODO: Create a custom filter policy for inner exceptions on Sagas: http://stackoverflow.com/questions/37041293/how-to-use-masstransits-retry-policy-with-sagas

                    e.StateMachineSaga(saga, repo);
                });
            });

            var consumeObserver = new LogConsumeObserver();

            _busControl.ConnectConsumeObserver(consumeObserver);

            //TODO: See how to do versioning of messages (best practices)
            //http://masstransit.readthedocs.io/en/master/overview/versioning.html

            try
            {
                _busHandle = _busControl.Start();
                Console.WriteLine("Saga active.. Press enter to exit");

                GlobalConfiguration.Configuration.UseMongoStorage("mongodb://localhost:27017", "hangfire-masstransit");

                hangfireServer = new BackgroundJobServer();
                Console.WriteLine("Hangfire Server started. Press any key to exit...");

                WebApp.Start <Startup>("http://localhost:1235");
            }
            catch
            {
                hangfireServer.Dispose();
                _busControl.Stop();

                throw;
            }

            return(true);
        }
Example #23
0
        public async Task <Acknowledgement> DispatchAsync <TMessage, TAsyncConsumer>(TMessage message, RetryMessageContext retryMessageContext)
            where TMessage : class
            where TAsyncConsumer : class, IConsumeAsync <TMessage>
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            Dictionary <string, long> stepDic = new Dictionary <string, long>();

            using (var scope = _serviceProvider.CreateScope())
            {
                var consumer = scope.ServiceProvider.GetService <IConsumeAsync <TMessage> >();
                if (consumer == null)
                {
                    _logger.LogWarning($"类型{typeof(TAsyncConsumer).Name} 找不到实现");
                    return(new Ack());
                }
                try
                {
                    stepDic.Add("创建handler", stopwatch.ElapsedMilliseconds);
                    var unitOfWorkAttr = consumer.GetType().GetMethod(nameof(IConsumeAsync <TMessage> .ConsumeAsync)).GetCustomAttribute <UnitOfWorkAttribute>();
                    stepDic.Add("获取UnitOfWorkAttribute", stopwatch.ElapsedMilliseconds);
                    IUnitOfWork unifOfWork = null;
                    if (unitOfWorkAttr != null)
                    {
                        unifOfWork = scope.ServiceProvider.GetService <IUnitOfWork>();
                        if (unitOfWorkAttr.IsTransactional)
                        {
                            unifOfWork.Begin(unitOfWorkAttr.IsolationLevel);
                        }
                    }
                    stepDic.Add("开启工作单元", stopwatch.ElapsedMilliseconds);
                    await consumer.ConsumeAsync(message).ConfigureAwait(false);

                    stepDic.Add("执行handler", stopwatch.ElapsedMilliseconds);
                    if (unifOfWork != null)
                    {
                        await unifOfWork.CompleteAsync();
                    }
                    stepDic.Add("提交工作单元", stopwatch.ElapsedMilliseconds);
                }
                catch (Exception ex)
                {
                    if (_needAckExceptionTypes.Contains(ex.GetType()))
                    {
                        return(new Ack());
                    }
                    if (retryMessageContext.RetryInfo.NumberOfRetries < _retryCount - 1)
                    {
                        var timespan = _rertyIntervalFunc.Invoke(retryMessageContext.RetryInfo.NumberOfRetries);
                        _logger.LogError(ex, $"message:{Newtonsoft.Json.JsonConvert.SerializeObject(message)},类型{typeof(TAsyncConsumer).Name}消息处理发生了异常,当前重试次数{retryMessageContext.RetryInfo.NumberOfRetries},将在{timespan.TotalSeconds}秒后重试");
                        return(Retry.In(timespan));
                    }
                    else
                    {
                        _logger.LogError(ex, $"message:{Newtonsoft.Json.JsonConvert.SerializeObject(message)},类型{typeof(TAsyncConsumer).Name}消息处理失败,重试次数{retryMessageContext.RetryInfo.NumberOfRetries},消息不再重试");
                        return(new Ack());
                    }
                }
            }

            if (stopwatch.ElapsedMilliseconds > _logForMinExecutionDuration && _logger.IsEnabled(LogLevel.Information))
            {
                var log  = new { MessageType = typeof(TMessage).Name, RetryTimes = retryMessageContext.RetryInfo.NumberOfRetries, Context = message, Step = stepDic };
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(log);
                stopwatch.Stop();
                _logger.LogInformation($"{json}  LastStep:{stopwatch.ElapsedMilliseconds}");
            }
            return(new Ack());
        }
Example #24
0
 public Retry ShouldThisBeRetried(Exception exception, TimeSpan totalExecutionTime, int currentRetryCount)
 {
     return(currentRetryCount <= 3 && TransientExceptions.Contains(exception.GetType())
         ? Retry.YesAfter(TimeSpan.FromMilliseconds(25))
         : Retry.No);
 }
Example #25
0
        public void Report_P1100_TemporaryGroup()
        {
            // Set initial conditions
            string reportName = "Individuals with Birthday in X Month (Temporary Group)";
            string reportCode = "P1100";

            base.SQL.ReportLibrary_Queue_DeleteItem(15, 65211, reportName);
            base.SQL.Groups_Group_Delete(15, reportName);

            // Login to report library
            TestBaseWebDriver test = base.TestContainer[Gallio.Framework.TestContext.CurrentContext.Test.Name];

            test.ReportLibrary.Login();

            // Search for and select a report
            test.Driver.FindElementByXPath("//input[@name='q']").SendKeys("P1100");
            test.Driver.FindElementByXPath("//input[@value='Search']").Click();
            test.GeneralMethods.WaitForElement(test.Driver, By.LinkText(reportName));
            test.Driver.FindElementByLinkText(reportName).Click();

            // Populate the report data
            test.GeneralMethods.WaitForElement(test.Driver, By.Id("dd_BirthMonth"));
            new SelectElement(test.Driver.FindElementById("dd_BirthMonth")).SelectByText("Sep");
            new SelectElement(test.Driver.FindElementById("dd_BirthMonthEnd")).SelectByText("Sep");

            test.Driver.FindElementByLinkText("Additional Filters").Click();

            test.Driver.FindElementById("rb_UseBirthDay_add").Click();
            test.Driver.FindElementById("tb_BirthDay_add").SendKeys("2");
            test.Driver.FindElementById("tb_BirthDayEnd_add").SendKeys("2");

            test.Driver.FindElementById("tb_StartAge_add").SendKeys("29");
            test.Driver.FindElementById("tb_EndAge_add").SendKeys("31");
            test.Driver.FindElementByXPath("//input[@id='submitQuery']").Click();

            // Verify the report exists and has completed
            int itemRowQueue = 0;

            Retry.WithPolling(3000).WithTimeout(200000).DoBetween(() => { test.Driver.FindElementByLinkText("Refresh for latest results").Click(); itemRowQueue = test.GeneralMethods.GetTableRowNumberWebDriver(TableIds.ReportLibrary.Queue, reportName, "Name"); }).Until(() => test.Driver.FindElementById(TableIds.ReportLibrary.Queue).FindElements(By.XPath(string.Format("//tr[{0}]/td[1]/img[@alt='Complete']", itemRowQueue + 1))).Count > 0);
            Assert.IsTrue(test.Driver.FindElementById(TableIds.ReportLibrary.Queue).FindElement(By.XPath(string.Format("//tr[{0}]/td[1]/img[contains(@src, '/ReportLibrary/public/images/status_complete.png?') and @alt='Complete']", itemRowQueue + 1))).Displayed);
            Assert.AreEqual(reportName, test.Driver.FindElementById(TableIds.ReportLibrary.Queue).FindElements(By.TagName("tr"))[itemRowQueue].FindElements(By.TagName("td"))[1].Text);
            Assert.IsTrue(test.Driver.FindElementById(TableIds.ReportLibrary.Queue).FindElement(By.XPath(string.Format("//tr[{0}]/td[position()=3 and normalize-space(text())='Temporary Group']", itemRowQueue + 1))).Displayed);
            Assert.AreEqual(reportCode, test.Driver.FindElementById(TableIds.ReportLibrary.Queue).FindElements(By.TagName("tr"))[itemRowQueue].FindElements(By.TagName("td"))[3].Text);
            Assert.IsTrue(test.Driver.FindElementById(TableIds.ReportLibrary.Queue).FindElement(By.XPath(string.Format("//tr[{0}]/td[position()=5 and contains(text(), 'Today at ')]", itemRowQueue + 1))).Displayed);

            // Logout of report library
            test.ReportLibrary.Logout();


            // Login to portal
            test.Portal.LoginWebDriver("msneeden", "Pa$$w0rd", "dc");

            // Verify the temporary group was created
            test.Driver.FindElementByLinkText("Groups").Click();
            test.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(1000));
            IWebElement menuItem = test.Driver.FindElement(By.XPath(string.Format("//div[@style='visibility: visible;']/dl/dt[contains(text(), '{0}')]/following-sibling::dd/a[contains(text(), '{1}')]", "Groups by Group Type", "View All")));

            menuItem.Click();
            test.Driver.FindElementByLinkText("Temporary").Click();
            int itemRowTempGroupTable = test.GeneralMethods.GetTableRowNumberWebDriver(TableIds.Portal.Groups_ViewAll_GroupList_TemporaryTab, string.Format("{0}\r\nCreated from Report Library via Individuals wit...", reportName), "Temporary Group");

            Assert.AreEqual(string.Format("{0}\r\nCreated from Report Library via Individuals wit...", reportName), test.Driver.FindElementById(TableIds.Portal.Groups_ViewAll_GroupList_TemporaryTab).FindElement(By.XPath(string.Format("//tr[{0}]/td[1]", itemRowTempGroupTable + 1))).Text);
            Assert.IsTrue(test.Driver.FindElementById(TableIds.Portal.Groups_ViewAll_GroupList_TemporaryTab).FindElement(By.XPath(string.Format("//tr[{0}]/td[2]", itemRowTempGroupTable + 1))).Text.Contains(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")).AddDays(3).AddHours(6).ToShortDateString()));   // ToString("M/d/yyyy h:mm:ss tt ")

            // Logout of portal
            test.Portal.LogoutWebDriver();
        }
Example #26
0
        private Ticket SearchLead(AuditLogEntity auditLog, string cardNo)
        {
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            Logger.Debug("I:--START--:--COCService.SearchLead--");

            try
            {
                Header profile = GetHeaderByServiceName <Header>(Constants.ServiceName.SearchLead);

                COC.ServiceRequest reqLead = new COC.ServiceRequest();
                reqLead.RequestHeader = new COC.Header
                {
                    ChannelID = profile.channel_id,
                    Username  = profile.user_name,
                    Password  = profile.password,
                    System    = profile.system_code
                };

                DataSet   ds = ReadSearchLeadXml();
                DataTable dt = ds.Tables[0];
                dt.Rows[0]["cid"]            = cardNo;
                dt.Rows[0]["firstname"]      = "";
                dt.Rows[0]["lastname"]       = "";
                dt.Rows[0]["status"]         = "";
                dt.Rows[0]["productGroupId"] = "";
                dt.Rows[0]["productId"]      = "";
                dt.Rows[0]["maxRecord"]      = "30";
                reqLead.RequestXml           = ds.ToXml();

                COC.ServiceResponse resLead = null;

                #region "Call Service"

                string flgCatchErrorCode = string.Empty;

                // Avoid error codes
                _commonFacade = new CommonFacade();
                List <string> exceptionErrorCodes = _commonFacade.GetExceptionErrorCodes(Constants.SystemName.COC, Constants.ServiceName.SearchLead);

                try
                {
                    Retry.Do(() =>
                    {
                        flgCatchErrorCode = string.Empty;
                        Logger.DebugFormat("-- XMLRequest --\n{0}", reqLead.SerializeObject());

                        using (var client = new COC.ServiceClient())
                        {
                            resLead = ((COC.IService)client).SearchLead(reqLead);
                            if (client != null)
                            {
                                ((ICommunicationObject)client).Abort();
                            }
                        }
                    }, TimeSpan.FromSeconds(WebConfig.GetServiceRetryInterval()), WebConfig.GetServiceRetryNo());
                }
                catch (AggregateException aex)
                {
                    aex.Handle((x) =>
                    {
                        if (x is EndpointNotFoundException)
                        {
                            flgCatchErrorCode = Constants.ErrorCode.CSM0002;
                            Logger.Error("EndpointNotFoundException occur:\n", x);
                            return(true);
                        }
                        else if (x is CommunicationException)
                        {
                            flgCatchErrorCode = Constants.ErrorCode.CSM0002;
                            Logger.Error("CommunicationException occur:\n", x);
                            return(true);
                        }
                        else if (x is TimeoutException)
                        {
                            flgCatchErrorCode = Constants.ErrorCode.CSM0001;
                            Logger.Error("TimeoutException occur:\n", x);
                            return(true);
                        }
                        else
                        {
                            flgCatchErrorCode = Constants.ErrorCode.CSM0003;
                            Logger.Error("Exception occur:\n", x);
                            return(true);
                        }
                    });
                }

                if (!string.IsNullOrEmpty(flgCatchErrorCode))
                {
                    AppLog.AuditLog(auditLog, LogStatus.Fail, GetMessageResource(flgCatchErrorCode, true));
                    throw new CustomException(GetMessageResource(flgCatchErrorCode, false));
                }

                #endregion

                if (resLead != null)
                {
                    Logger.DebugFormat("-- XMLResponse --\n{0}", resLead.SerializeObject());

                    var xdoc = XDocument.Parse(resLead.ResponseXml);

                    Ticket ticket = (xdoc.Descendants("ticket")
                                     .Select(x => new Ticket
                    {
                        TicketId = x.Attribute("id").Value,
                        ResponseCode = x.Element("responseCode").Value,
                        ResponseMessage = x.Element("responseMessage").Value,
                        StrResponseDate = x.Element("responseDate").Value,
                        StrResponseTime = x.Element("responseTime").Value,
                        TotalLeads = x.Element("totalLeads") == null ? string.Empty : x.Element("totalLeads").Value
                    })).FirstOrDefault();


                    if (exceptionErrorCodes != null && exceptionErrorCodes.Contains(ticket.ResponseCode))
                    {
                        ticket.ResponseCode = Constants.TicketResponse.COC_Success;
                    }

                    if (Constants.TicketResponse.COC_Success.Equals(ticket.ResponseCode))
                    {
                        List <TicketItem> items = null;

                        if (xdoc.Descendants("result") != null && xdoc.Descendants("result").Count() > 0)
                        {
                            items = xdoc.Descendants("result")
                                    .Select(x => new TicketItem
                            {
                                TicketId           = x.Element("ticketId").Value,
                                CardNo             = x.Element("cid").Value,
                                FirstName          = x.Element("firstname").Value,
                                LastName           = x.Element("lastname").Value,
                                TelNo1             = x.Element("telNo1").Value,
                                Status             = x.Element("status").Value,
                                StatusDesc         = x.Element("statusDesc").Value,
                                CampaignCode       = x.Element("campaign").Value,
                                CampaignDesc       = x.Element("campaignDesc").Value,
                                ProductGroupId     = x.Element("productGroupId").Value,
                                ProductGroupDesc   = x.Element("productGroupDesc").Value,
                                ProductId          = x.Element("productId").Value,
                                ProductDesc        = x.Element("productDesc").Value,
                                ChannelId          = x.Element("channelId").Value,
                                OwnerLead          = x.Element("ownerLead").Value,
                                OwnerLeadName      = x.Element("ownerLeadName").Value,
                                DelegateLead       = x.Element("delegateLead").Value,
                                DelegateLeadName   = x.Element("delegateLeadName").Value,
                                CreatedUser        = x.Element("createdUser").Value,
                                CreatedUserName    = x.Element("createdUserName").Value,
                                StrCreatedDate     = x.Element("createdDate").Value,
                                StrCreatedTime     = x.Element("createdTime").Value,
                                StrAssignedDate    = x.Element("assignedDate").Value,
                                StrAssignedTime    = x.Element("assignedTime").Value,
                                OwnerBranch        = x.Element("ownerBranch").Value,
                                OwnerBranchName    = x.Element("ownerBranchName").Value,
                                DelegateBranch     = x.Element("delegateBranch").Value,
                                DelegateBranchName = x.Element("delegateBranchName").Value,
                                CreatedBranch      = x.Element("createdBranch").Value,
                                CreatedBranchName  = x.Element("createdBranchName").Value
                            }).ToList();
                        }

                        ticket.Items = items;
                        AppLog.AuditLog(auditLog, LogStatus.Success, string.Empty);
                        return(ticket);
                    }

                    // Log DB
                    AppLog.AuditLog(auditLog, LogStatus.Fail, GetMessageResource(Constants.SystemName.COC, Constants.ServiceName.SearchLead,
                                                                                 ticket.ResponseCode, true));
                    throw new CustomException(GetMessageResource(Constants.SystemName.COC, Constants.ServiceName.SearchLead,
                                                                 ticket.ResponseCode, false));
                }
            }
            finally
            {
                stopwatch.Stop();
                Logger.DebugFormat("O:--Finish--:ElapsedMilliseconds/{0}", stopwatch.ElapsedMilliseconds);
            }

            return(null);
        }
        private DownloadStream GetDownloadStreamInternal(File afile, long?start = null, long?end = null)
        {
            bool isLinked = afile.PublicLinks.Any();

            Cached <ServerRequestResult> downServer = null;
            var pendingServers = isLinked
                ? ShardManager.WeblinkDownloadServersPending
                : ShardManager.DownloadServersPending;
            Stopwatch watch = new Stopwatch();

            HttpWebRequest request = null;

            CustomDisposable <HttpWebResponse> ResponseGenerator(long instart, long inend, File file)
            {
                var resp = Retry.Do(() =>
                {
                    downServer = pendingServers.Next(downServer);

                    string url = (isLinked
                            ? $"{downServer.Value.Url}{WebDavPath.EscapeDataString(file.PublicLinks.First().Uri.PathAndQuery)}"
                            : $"{downServer.Value.Url}{Uri.EscapeDataString(file.FullPath.TrimStart('/'))}") +
                                 $"?client_id={HttpSettings.ClientId}&token={Authent.AccessToken}";
                    var uri = new Uri(url);

                    request = (HttpWebRequest)WebRequest.Create(uri.OriginalString);

                    request.AddRange(instart, inend);
                    request.Proxy                     = HttpSettings.Proxy;
                    request.CookieContainer           = Authent.Cookies;
                    request.Method                    = "GET";
                    request.Accept                    = "*/*";
                    request.UserAgent                 = HttpSettings.UserAgent;
                    request.Host                      = uri.Host;
                    request.AllowWriteStreamBuffering = false;

                    if (isLinked)
                    {
                        request.Headers.Add("Accept-Ranges", "bytes");
                        request.ContentType = MediaTypeNames.Application.Octet;
                        request.Referer     = $"{ConstSettings.CloudDomain}/home/{Uri.EscapeDataString(file.Path)}";
                        request.Headers.Add("Origin", ConstSettings.CloudDomain);
                    }

                    request.Timeout          = 15 * 1000;
                    request.ReadWriteTimeout = 15 * 1000;

                    watch.Start();
                    var response = (HttpWebResponse)request.GetResponse();
                    return(new CustomDisposable <HttpWebResponse>
                    {
                        Value = response,
                        OnDispose = () =>
                        {
                            pendingServers.Free(downServer);
                            watch.Stop();
                            Logger.Debug($"HTTP:{request.Method}:{request.RequestUri.AbsoluteUri} ({watch.Elapsed.Milliseconds} ms)");
                        }
                    });
                },
                                    exception =>
                                    ((exception as WebException)?.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound,
                                    exception =>
                {
                    pendingServers.Free(downServer);
                    Logger.Warn($"Retrying HTTP:{request.Method}:{request.RequestUri.AbsoluteUri} on exception {exception.Message}");
                },
                                    TimeSpan.FromSeconds(1), 2);

                return(resp);
            }

            var stream = new DownloadStream(ResponseGenerator, afile, start, end);

            return(stream);
        }
        public RoutingSlipEventPersister(IMongoCollection <RoutingSlipDocument> collection)
        {
            _collection = collection;

            _retryPolicy = Retry.Selected <MongoWriteException>().Interval(10, TimeSpan.FromMilliseconds(20));
        }
 public void WaitWhileBusy()
 {
     Retry.For(ShellIsBusy, isBusy => isBusy, TimeSpan.FromSeconds(10));
 }
Example #30
0
 private void ResumeSession()
 {
     Retry.Do(UpdateWallpaper, TimeSpan.FromSeconds(15));
     MidnightUpdate();
 }
        public async Task WhenICreateTheFollowingMerchants(Table table)
        {
            foreach (TableRow tableRow in table.Rows)
            {
                // lookup the estate id based on the name in the table
                EstateDetails estateDetails = this.TestingContext.GetEstateDetails(tableRow);
                String        token         = this.TestingContext.AccessToken;
                if (String.IsNullOrEmpty(estateDetails.AccessToken) == false)
                {
                    token = estateDetails.AccessToken;
                }

                String merchantName = SpecflowTableHelper.GetStringRowValue(tableRow, "MerchantName");
                CreateMerchantRequest createMerchantRequest = new CreateMerchantRequest
                {
                    Name    = merchantName,
                    Contact = new Contact
                    {
                        ContactName  = SpecflowTableHelper.GetStringRowValue(tableRow, "ContactName"),
                        EmailAddress = SpecflowTableHelper.GetStringRowValue(tableRow, "EmailAddress")
                    },
                    Address = new Address
                    {
                        AddressLine1 = SpecflowTableHelper.GetStringRowValue(tableRow, "AddressLine1"),
                        Town         = SpecflowTableHelper.GetStringRowValue(tableRow, "Town"),
                        Region       = SpecflowTableHelper.GetStringRowValue(tableRow, "Region"),
                        Country      = SpecflowTableHelper.GetStringRowValue(tableRow, "Country")
                    }
                };

                CreateMerchantResponse response = await this.TestingContext.DockerHelper.EstateClient
                                                  .CreateMerchant(token, estateDetails.EstateId, createMerchantRequest, CancellationToken.None).ConfigureAwait(false);

                response.ShouldNotBeNull();
                response.EstateId.ShouldBe(estateDetails.EstateId);
                response.MerchantId.ShouldNotBe(Guid.Empty);

                // Cache the merchant id
                estateDetails.AddMerchant(response.MerchantId, merchantName);

                this.TestingContext.Logger.LogInformation($"Merchant {merchantName} created with Id {response.MerchantId} for Estate {estateDetails.EstateName}");
            }

            foreach (TableRow tableRow in table.Rows)
            {
                EstateDetails estateDetails = this.TestingContext.GetEstateDetails(tableRow);

                String merchantName = SpecflowTableHelper.GetStringRowValue(tableRow, "MerchantName");

                Guid merchantId = estateDetails.GetMerchantId(merchantName);

                String token = this.TestingContext.AccessToken;
                if (String.IsNullOrEmpty(estateDetails.AccessToken) == false)
                {
                    token = estateDetails.AccessToken;
                }

                await Retry.For(async() =>
                {
                    MerchantResponse merchant = await this.TestingContext.DockerHelper.EstateClient
                                                .GetMerchant(token, estateDetails.EstateId, merchantId, CancellationToken.None)
                                                .ConfigureAwait(false);

                    merchant.MerchantName.ShouldBe(merchantName);
                });
            }
        }
Example #32
0
 public Task <bool> CheckAuth()
 {
     return(Retry.Do(async() => await _client.IsAuthenticated(), RetryCount, RetryDelay));
 }
Example #33
0
        public virtual async Task <CacheStream> DownloadAndCacheIfNeededAsync(string url, TaskParameter parameters, Configuration configuration, CancellationToken token)
        {
            var allowCustomKey = !string.IsNullOrWhiteSpace(parameters.CustomCacheKey) &&
                                 (string.IsNullOrWhiteSpace(parameters.LoadingPlaceholderPath) || parameters.LoadingPlaceholderPath != url) &&
                                 (string.IsNullOrWhiteSpace(parameters.ErrorPlaceholderPath) || parameters.ErrorPlaceholderPath != url);

            var    filename         = (allowCustomKey ? MD5Helper.MD5(parameters.CustomCacheKey) : MD5Helper.MD5(url));
            var    allowDiskCaching = AllowDiskCaching(parameters.CacheType);
            var    duration         = parameters.CacheDuration ?? configuration.DiskCacheDuration;
            string filePath         = null;

            if (allowDiskCaching)
            {
                var diskStream = await configuration.DiskCache.TryGetStreamAsync(filename).ConfigureAwait(false);

                if (diskStream != null)
                {
                    token.ThrowIfCancellationRequested();
                    filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);

                    return(new CacheStream(diskStream, true, filePath));
                }
            }

            token.ThrowIfCancellationRequested();

            var downloadInfo = new DownloadInformation(url, parameters.CustomCacheKey, filename, allowDiskCaching, duration);

            parameters.OnDownloadStarted?.Invoke(downloadInfo);

            var responseBytes = await Retry.DoAsync(
                async() => await DownloadAsync(url, token, configuration.HttpClient, parameters, downloadInfo).ConfigureAwait(false),
                parameters.RetryDelayInMs > 0?TimeSpan.FromMilliseconds(parameters.RetryDelayInMs) : DelayBetweenRetry,
                parameters.RetryCount,
                () => configuration.Logger.Debug(string.Format("Retry download: {0}", url))).ConfigureAwait(false);

            if (responseBytes == null)
            {
                throw new HttpRequestException("No Content");
            }

            if (allowDiskCaching)
            {
                Action finishedAction      = null;
                var    onFileWriteFinished = parameters?.OnFileWriteFinished;
                if (onFileWriteFinished != null)
                {
                    finishedAction = new Action(() =>
                    {
                        onFileWriteFinished?.Invoke(new FileWriteInfo(filePath, url));
                    });
                }

                await configuration.DiskCache.AddToSavingQueueIfNotExistsAsync(filename, responseBytes, downloadInfo.CacheValidity, finishedAction).ConfigureAwait(false);
            }

            token.ThrowIfCancellationRequested();
            filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);

            token.ThrowIfCancellationRequested();
            var memoryStream = new MemoryStream(responseBytes, false);

            return(new CacheStream(memoryStream, false, filePath));
        }
Example #34
0
        public virtual void Execute(Retry retry)
        {
            if (!entityById.Exist<Retry>(retry.Id.Value)) throw new EntityNotFoundException(typeof(Retry), retry.Id.Value);

            repository.Update(retry);
        }