コード例 #1
0
        async Task <bool> ForwardMessage <T>(Message <T> message, CancellationToken cancellationToken) where T :  IHaveType
        {
            var payload = message.Payload as IHaveType;

            var jobs = workerRegistry.GetHandlersFor(message.Payload)
                       .Select(handler => new Job(generator.NewReference <Job>(), payload.GetAddress(), handler, message.Id,
                                                  payload.GetQueueId(), message.Created))
                       .ToList();

            using (var dbContextScope = dbContextScopeFactory.Create())
            {
                await jobRepository.SaveAll(jobs);

                message.MarkAsProcessed();

                switch (message)
                {
                case Command command:
                    await commandRepository.Save(command);

                    break;

                case Event @event:
                    await eventRepository.Save(@event);

                    break;
                }

                await dbContextScope.SaveChangesAsync(cancellationToken);
            }

            foreach (var job in jobs)
            {
                jobScheduler.Add(job);
            }

            return(true);
        }
コード例 #2
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            logger.LogInformation("Starting job runner.");

            cancellationToken.Register(Stop);

            var existingJobs = await FetchExistingJobs();

            //TODO Way to ensure that new jobs cannot arrive ahead of existing jobs
            //as we start up.
            foreach (var job in existingJobs)
            {
                logger.LogInformation("Adding job.");
                jobScheduler.Add(job);
            }

            logger.LogInformation("Starting job scheduler");
            await jobScheduler.Start(cancellationToken);

            logger.LogInformation("No more work!");

            await jobScheduler.Stop();
        }