Beispiel #1
0
        public async Task Execute(IJobExecutionContext context)
        {
            if (MissfireHelper.IsMissedFire(context))
            {
                return;
            }

            const string             jobName  = "{0}Job";
            Func <int, ConsoleColor> getColor = x => (ConsoleColor)x + 1;

            for (var i = 0; i < NumberOfJobs; i++)
            {
                var utcNow = _now.UtcNow;
                var then   = utcNow.AddSeconds(5);

                // spin a job
                ISelfDescribingJob job;

                if (!TryResolveKeyed(_resolver, JobKeys.ConcurrentJobAutofacKey, out job))
                {
                    throw new Exception("Could not resolve the calendar job");
                }

                var variables = new List <Tuple <string, string> >
                {
                    new Tuple <string, string>(JobKeys.JobDataName, string.Format(jobName, i)),
                    new Tuple <string, string>(JobKeys.JobDataColor, getColor(i).ToString())
                };

                await QuartzHelper.TriggerNow(context.Scheduler, job, then, variables.ToArray());
            }
        }
Beispiel #2
0
        public async Task Execute(IJobExecutionContext context)
        {
            if (MissfireHelper.IsMissedFire(context))
            {
                return;
            }

            var dataMap  = context.MergedJobDataMap;
            var taskName = dataMap[JobKeys.JobDataName] as string;

            if (taskName == null)
            {
                throw new DataMapItemMissingException(JobKeys.JobDataName);
            }

            var colorRaw = dataMap[JobKeys.JobDataColor] as string;

            if (colorRaw == null)
            {
                throw new DataMapItemMissingException(JobKeys.JobDataColor);
            }

            var consoleColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorRaw);

            ColoredConsoleWriteLine(consoleColor, string.Format("Entered {0}. Acquiring lock", taskName));

            for (var j = 0; j < 5; j++)
            {
                ColoredConsoleWriteLine(consoleColor, string.Format("Writing {0} from task {1}", j, taskName));
            }

            using (await CriticalRegionAsyncLock.CreateAsync(TokenHolder.SynchronizationToken))
            {
                ColoredConsoleWriteLine(consoleColor, string.Format("Entered lock from task {0}. Starting critical region", taskName));
                //CRITICAL REGION

                for (var i = 0; i < 10; i++)
                {
                    ColoredConsoleWriteLine(consoleColor, taskName);
                    await Task.Delay(100);
                }

                // END OF CRITICAL REGION
                ColoredConsoleWriteLine(consoleColor, string.Format("Finished critical region. Exiting lock from task {0}", taskName));
            }

            ColoredConsoleWriteLine(consoleColor, string.Format("Exiting task {0}", taskName));
        }