Example #1
0
        /// <summary>
        /// Dispatches the specified event.
        /// </summary>
        /// <param name="incomingRow">The incoming row.</param>
        public void Process(WakeUpRow incomingRow)
        {
            var wakeUpEvent = incomingRow as TRow;

            if (wakeUpEvent != null)
            {
                this.DoJobIfNeeded(wakeUpEvent);
            }
        }
Example #2
0
        /// <summary>
        /// Releases the key.
        /// </summary>
        /// <param name="item">The item.</param>
        public void ReleaseItem(WakeUpRow item)
        {
            Check.NotNull(() => item);

            item = this.provider.GetValue <WakeUpRow>(item.DocumentKey);

            if (item == null)
            {
                return;
            }

            item.Processing  = false;
            item.LockEndDate = this.DateTimeProvider.UtcNow;

            provider.UpdateOrInsert(item);
        }
Example #3
0
        private WakeUpRow TryLockItem(WakeUpRow item, int maxExecutingInSeconds)
        {
            if (item.Processing == true && item.LockInitDate.AddSeconds(maxExecutingInSeconds) > DateTimeProvider.UtcNow)
            {
                return(null);
            }

            item.LockInitDate = this.DateTimeProvider.UtcNow;
            item.Processing   = true;

            var insertResult = this.provider.UpdateOrInsert(item);

            if (insertResult != PersistenceResultEnum.Success)
            {
                return(null);
            }

            return(item);
        }
Example #4
0
        private void ProcessIfNeeded(IRecurrentAlarm job)
        {
            try
            {
                log.LogDebug(S.Invariant($"Processing job {job.JobName}"));

                var jobRow = new WakeUpRow
                {
                    SchedulerName  = job.JobName,
                    DateRangeStart = this.DateTimeProvider.UtcNowTicks
                };

                var jobRows = this.provider.GetValuesByKeyPattern <WakeUpRow>(100, jobRow.StartKey, jobRow.DocumentType).Items.Where(x => !x.ReverseRow);

                foreach (var row in jobRows)
                {
                    if (row.AggregateUid != new Guid())
                    {
                        if (this.locker.TryLockKey(row.AggregateUid.ToString(), 300) == null)
                        {
                            this.log.LogDebug(S.Invariant($"Aggregate {row.AggregateUid} is locked so the scheduler cannot procces it right now"));
                            continue;
                        }
                    }
                    else
                    {
                        if (this.TryLockItem(row, 300) == null)
                        {
                            this.log.LogDebug(S.Invariant($"Row {row.DocumentKey} is blocked so the asynchub cannot procces it right now"));
                            continue;
                        }
                    }

                    try
                    {
                        using (var context = new ThreadContextContainer())
                        {
                            log.LogDebug(S.Invariant($"Processing row {row.DocumentKey} for job {job.JobName}"));

                            job.Process(row);

                            this.unitOfWork.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        this.log.LogError(
                            ex,
                            S.Invariant($"Error processing row {row.DocumentKey}"));
                    }

                    try
                    {
                        this.ReleaseItem(row);
                        this.locker.ReleaseKey(row.AggregateUid.ToString());
                    }
                    catch (Exception ex)
                    {
                        this.log.LogError(
                            ex,
                            S.Invariant($"Error releasing row {row.DocumentKey}"));
                    }
                }
            }
            catch (Exception ex)
            {
                this.log.LogError(
                    ex,
                    S.Invariant($"Uncontrolled Exception"));
            }
        }