Beispiel #1
0
        /// <summary>
        ///     Serializes the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <byte[]> SerializeAsync(IQuidjiboCommand command, CancellationToken cancellationToken)
        {
            var workPayload = new WorkPayload
            {
                Type    = command.GetQualifiedName(),
                Content = GetContent(command)
            };
            var json    = JsonConvert.SerializeObject(workPayload);
            var payload = Encoding.UTF8.GetBytes(json);

            return(Task.FromResult(payload));
        }
Beispiel #2
0
        public async Task DispatchAsync(IQuidjiboCommand command, IQuidjiboProgress progress, CancellationToken cancellationToken)
        {
            var type     = typeof(IQuidjiboHandler <>).MakeGenericType(command.GetType());
            var resolved = _resolver.Resolve(type);
            var method   = type.GetMethod("ProcessAsync");

            await(Task) method.Invoke(resolved, new object[]
            {
                command,
                progress,
                cancellationToken
            });
        }
Beispiel #3
0
        private static object GetContent(IQuidjiboCommand command)
        {
            if (command is WorkflowCommand workflow)
            {
                return(new WorkflowPayload
                {
                    Step = workflow.Step,
                    CurrentStep = workflow.CurrentStep,
                    Entries = workflow.Entries.ToDictionary(
                        e => e.Key,
                        e => e.Value.Select(x => new WorkPayload
                    {
                        Type = x.GetType().AssemblyQualifiedName,
                        Content = x
                    }))
                });
            }

            return(command);
        }
Beispiel #4
0
        public async Task <PublishInfo> PublishAsync(IQuidjiboCommand command, string queueName, int delay, CancellationToken cancellationToken = default(CancellationToken))
        {
            var payload = await _payloadSerializer.SerializeAsync(command, cancellationToken);

            var protectedPayload = await _payloadProtector.ProtectAsync(payload, cancellationToken);

            var item = new WorkItem
            {
                Id            = Guid.NewGuid(),
                CorrelationId = Guid.NewGuid(),
                Name          = command.GetName(),
                Attempts      = 0,
                Payload       = protectedPayload,
                Queue         = queueName
            };
            var provider = await GetOrCreateWorkProvider(queueName, cancellationToken);

            await provider.SendAsync(item, delay, cancellationToken);

            return(new PublishInfo(item.Id, item.CorrelationId));
        }
Beispiel #5
0
        public async Task ScheduleAsync(string name, string queue, IQuidjiboCommand command, Cron cron, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                _logger.LogWarning("The name argument is required");
                return;
            }

            if (string.IsNullOrWhiteSpace(queue))
            {
                _logger.LogWarning("The queue argument is required");
                return;
            }

            if (command == null)
            {
                _logger.LogWarning("The command argument is required");
                return;
            }

            if (cron == null)
            {
                _logger.LogWarning("The cron argument is required");
                return;
            }

            var now     = DateTime.UtcNow;
            var payload = await _payloadSerializer.SerializeAsync(command, cancellationToken);

            var protectedPayload = await _payloadProtector.ProtectAsync(payload, cancellationToken);

            var item = new ScheduleItem
            {
                CreatedOn      = now,
                CronExpression = cron.Expression,
                EnqueueOn      = _cronProvider.GetNextSchedule(cron.Expression),
                Id             = Guid.NewGuid(),
                Name           = name,
                Payload        = protectedPayload,
                Queue          = queue,
                VisibleOn      = now
            };
            var provider = await GetOrCreateScheduleProvider(queue, cancellationToken);

            var existingItem = await provider.LoadByNameAsync(name, cancellationToken);

            if (existingItem != null)
            {
                if (!item.EquivalentTo(existingItem))
                {
                    _logger.LogDebug("Replace existing schedule for {0}", name);
                    await provider.DeleteAsync(existingItem.Id, cancellationToken);

                    await provider.CreateAsync(item, cancellationToken);
                }
            }
            else
            {
                _logger.LogDebug("Creating new schedule for {0}", name);
                await provider.CreateAsync(item, cancellationToken);
            }
        }
Beispiel #6
0
        public async Task ScheduleAsync(string name, IQuidjiboCommand command, Cron cron, CancellationToken cancellationToken = default(CancellationToken))
        {
            var queueName = command.GetQueueName();

            await ScheduleAsync(name, queueName, command, cron, cancellationToken);
        }
Beispiel #7
0
 public Task <PublishInfo> PublishAsync(IQuidjiboCommand command, string queueName, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(PublishAsync(command, queueName, 0, cancellationToken));
 }
Beispiel #8
0
        public Task <PublishInfo> PublishAsync(IQuidjiboCommand command, int delay, CancellationToken cancellationToken = default(CancellationToken))
        {
            var queueName = command.GetQueueName();

            return(PublishAsync(command, queueName, delay, cancellationToken));
        }
Beispiel #9
0
        public static string GetQueueName(this IQuidjiboCommand command)
        {
            var attr = command.GetType().GetTypeInfo().GetCustomAttribute <QueueNameAttribute>();

            return(attr == null ? Default.Queue : attr.Name);
        }
Beispiel #10
0
 public static string GetName(this IQuidjiboCommand command)
 {
     return(command.GetType().Name);
 }
Beispiel #11
0
 public static string GetQualifiedName(this IQuidjiboCommand command)
 {
     return(command.GetType().AssemblyQualifiedName);
 }