Exemplo n.º 1
0
        public static void HandleOUs(out long count)
        {
            OrgUnitService service = new OrgUnitService();
            OrgUnitDao     dao     = new OrgUnitDao();

            count = 0;

            var orgUnits = dao.Get4OldestEntries();

            do
            {
                if (orgUnits.Count > 0)
                {
                    int subCounter = 0;

                    Parallel.ForEach(orgUnits, (orgUnit) =>
                    {
                        using (var cancelTokenSource = new CancellationTokenSource())
                        {
                            var cancelToken = cancelTokenSource.Token;
                            var task        = Task.Run(() => HandleOU(orgUnit, service, dao), cancelToken);

                            if (task.Wait(TimeSpan.FromSeconds(300)))
                            {
                                int result = task.Result;

                                switch (result)
                                {
                                case -1:
                                    // we are not increment subcounter here, as this is a temporary failure, and we should sleep
                                    break;

                                case -2:
                                    // bad data, it was logged and then throw away, nothing to see here, move along
                                    lock (orgUnits)
                                    {
                                        subCounter++;
                                    }
                                    break;

                                default:
                                    lock (orgUnits)
                                    {
                                        subCounter++;
                                    }
                                    errorCount = 0;
                                    break;
                                }
                            }
                            else
                            {
                                log.Warn("Timeout happended while waiting for synchronization of OrgUnit: " + orgUnit.Uuid);

                                cancelTokenSource.Cancel();
                            }
                        }
                    });

                    count += subCounter;
                    if (subCounter != orgUnits.Count)
                    {
                        throw new TemporaryFailureException();
                    }

                    orgUnits = dao.Get4OldestEntries();
                }
            } while (orgUnits.Count > 0);
        }