private async Task Init(CMMNExecutionContext executionContext, CaseEltInstance elt, CancellationToken token)
        {
            var currentDateTime   = DateTime.UtcNow;
            var elapsedTime       = ISO8601Parser.ParseTime(elt.GetTimerExpression().Body);
            var repeatingInterval = ISO8601Parser.ParseRepeatingTimeInterval(elt.GetTimerExpression().Body);

            if (repeatingInterval != null)
            {
                if (currentDateTime >= repeatingInterval.Interval.EndDateTime)
                {
                    return;
                }

                var startDate = currentDateTime;
                if (startDate < repeatingInterval.Interval.StartDateTime)
                {
                    startDate = repeatingInterval.Interval.StartDateTime;
                }

                var diff        = repeatingInterval.Interval.EndDateTime.Subtract(startDate);
                var newTimespan = new TimeSpan(diff.Ticks / (repeatingInterval.RecurringTimeInterval));
                for (var i = 0; i < repeatingInterval.RecurringTimeInterval; i++)
                {
                    currentDateTime = currentDateTime.Add(newTimespan);
                    var newInstance = elt;
                    if (i > 0)
                    {
                        newInstance = executionContext.Instance.TryCreateInstance(elt);
                    }

                    await TrySubscribe(executionContext, newInstance, CMMNConstants.ExternalTransitionNames.Occur, token);

                    await _messageScheduler.SchedulePublish(currentDateTime, BuildEvent(executionContext.Instance.AggregateId, newInstance.Id), token);
                }
            }

            if (elapsedTime != null)
            {
                if (currentDateTime >= elapsedTime)
                {
                    return;
                }

                await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Occur, token);

                await _messageScheduler.SchedulePublish(elapsedTime.Value, BuildEvent(executionContext.Instance.AggregateId, elt.Id), token);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Publishes an object as a message, using the message type specified. If the object cannot be cast
        /// to the specified message type, an exception will be thrown.
        /// </summary>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="message">The message object</param>
        /// <param name="messageType">The type of the message (use message.GetType() if desired)</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Publish is acknowledged by the broker</returns>
        public static Task <ScheduledMessage> SchedulePublish(this IMessageScheduler scheduler, TimeSpan delay, object message,
                                                              Type messageType, CancellationToken cancellationToken = default)
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.SchedulePublish(scheduledTime, message, messageType, cancellationToken));
        }
        public async Task Consume(ConsumeContext <TaskCreatedMessage> context)
        {
            var taskId = context.Message.TaskId;

            // Если нулл - предупреждаем, что нулл и ничего не делаем
            if (taskId == null)
            {
                _logger.Log(LogLevel.Warning, "TaskCreatedMessage: message.taskId == null");
                return;
            }

            var task = await _dbContext.Tasks.FirstOrDefaultAsync(t => t.Id == taskId);

            // Если нулл - пишем эррор(какая-то тут очевидная нестыковка), что нулл и ничего не делаем.
            // Если по условию задачи не прописано иное
            if (task == null)
            {
                _logger.Log(LogLevel.Error, $"TaskCreatedMessage: Задача с Id == {taskId} не найдена");
                return;
            }

            // Меняем статус
            task.State     = ETaskState.Running;
            task.TimeStamp = DateTime.UtcNow;
            _dbContext.Update(task);
            await _dbContext.SaveChangesAsync();

            // Оповещаем, что статус поменяли и говорим, в какоем время надо ее закрыть (через 2 мин)
            await _publishEndpoint.SchedulePublish(DateTime.UtcNow + TimeSpan.FromMinutes(2),
                                                   new TaskStartedMessage { TaskId = taskId });
        }
Exemplo n.º 4
0
        /// <summary>
        /// Publish a message
        /// </summary>
        /// <typeparam name="T">The message type</typeparam>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="message">The message</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="pipe"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Publish is acknowledged by the broker</returns>
        public static Task <ScheduledMessage <T> > SchedulePublish <T>(this IMessageScheduler scheduler, TimeSpan delay, T message,
                                                                       IPipe <SendContext> pipe, CancellationToken cancellationToken = default)
            where T : class
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.SchedulePublish(scheduledTime, message, pipe, cancellationToken));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Publishes an interface message, initializing the properties of the interface using the anonymous
        /// object specified
        /// </summary>
        /// <typeparam name="T">The interface type to send</typeparam>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="values">The property values to initialize on the interface</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Publish is acknowledged by the broker</returns>
        public static Task <ScheduledMessage <T> > SchedulePublish <T>(this IMessageScheduler scheduler, TimeSpan delay, object values,
                                                                       CancellationToken cancellationToken = default)
            where T : class
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.SchedulePublish <T>(scheduledTime, values, cancellationToken));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Schedule(int seconds)
        {
            await _scheduler.SchedulePublish(DateTime.Now.AddSeconds(seconds), new MyMessage { Id = NewId.NextGuid(), Timestamp = InVar.Timestamp }, Pipe.Execute <SendContext>(ctx =>
            {
                if (ctx.TryGetPayload <MyDbContext>(out var db))
                {
                    db.Persons.Add(new Person {
                        Name = "TestSchedule"
                    });
                }
            }));

            return(Ok());
        }