Exemplo n.º 1
0
        public async Task BuildAsync_ShouldBuildStaticTasks()
        {
            // arrange
            var sut = new CsissorsBuilder()
                      .AddMockRepository()
                      .AddTaskContainer <MockContainer>();

            var expectedConfiguration = new TaskConfiguration(
                new IntervalSchedule(TimeSpan.FromDays(1), false),
                FailureMode.None,
                ExecutionMode.AtLeastOnce,
                TimeSpan.FromSeconds(60),
                new Dictionary <string, object?>()
                );

            // act
            var context = await sut.BuildAsync(CancellationToken.None);

            // assert
            context.Tasks.StaticTasks.Should().HaveCount(1);

            var task = context.Tasks.StaticTasks[0];

            task.Name.Should().Be("Bar");
            task.Configuration.Should().BeEquivalentTo(expectedConfiguration,
                                                       (config) => config.RespectingRuntimeTypes());
        }
Exemplo n.º 2
0
        public async Task BuildAsync_ShouldBuildStaticTasks_Delegate()
        {
            // arrange
            var expectedConfiguration = new TaskConfiguration(
                new IntervalSchedule(TimeSpan.FromDays(1), false),
                FailureMode.None,
                ExecutionMode.AtLeastOnce,
                TimeSpan.FromSeconds(60),
                new Dictionary <string, object?>()
                );

            var sut = new CsissorsBuilder()
                      .AddMockRepository()
                      .AddTask("mock", expectedConfiguration, async() => throw new IOException());

            // act
            var context = await sut.BuildAsync(CancellationToken.None);

            // assert
            context.Tasks.StaticTasks.Should().HaveCount(1);

            var task = context.Tasks.StaticTasks[0];

            task.Name.Should().Be("mock");
            task.Configuration.Should().BeSameAs(expectedConfiguration);
            await Assert.ThrowsAsync <IOException>(() => task.ExecuteAsync(Mock.Of <ITaskContext>()));
        }
Exemplo n.º 3
0
 public static CsissorsBuilder AddRedisRepository(this CsissorsBuilder builder, Action <RedisOptions> configure)
 {
     builder.Services.AddOptions()
     .Configure(configure)
     .AddSingleton <IRepositoryFactory, RedisRepositoryFactory>();
     return(builder);
 }
Exemplo n.º 4
0
 public static CsissorsBuilder AddPostgresRepository(this CsissorsBuilder builder, Action <PostgresOptions> configure)
 {
     builder.Services.AddOptions()
     .Configure(configure)
     .AddSingleton <IConfigurationSerializer, ConfigurationSerializer>()
     .AddSingleton <IRepositoryFactory, PostgresRepositoryFactory>();
     return(builder);
 }
Exemplo n.º 5
0
        public static CsissorsBuilder AddMockRepository(this CsissorsBuilder builder)
        {
            var repositoryFactory = new Mock <IRepositoryFactory>();

            repositoryFactory.Setup(x => x.CreateRepositoryAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(Mock.Of <IRepository>());

            return(builder.ConfigureServices(services => services.AddSingleton(repositoryFactory.Object)));
        }
Exemplo n.º 6
0
        public async Task BuildAsync_ShouldBuildDynamicTasks()
        {
            // arrange
            var sut = new CsissorsBuilder()
                      .AddMockRepository()
                      .AddTaskContainer <MockContainer>();

            // act
            var context = await sut.BuildAsync(CancellationToken.None);

            // assert
            context.Tasks.DynamicTasks.Should().HaveCount(1);
            context.Tasks.DynamicTasks[0].Name.Should().Be("Foo");
        }
Exemplo n.º 7
0
        static async Task MainAsync(string[] args)
        {
            var csissors = new CsissorsBuilder()
                           .ConfigureServices(services =>
            {
                services.AddLogging(configure => configure.AddConsole(configure =>
                {
                    configure.Format        = ConsoleLoggerFormat.Default;
                    configure.IncludeScopes = true;
                }));
            })
                           .AddTaskContainer <TaskContainer>()
                           .AddPostgresRepository(options =>
            {
                options.ConnectionString = "Host=localhost;Username=postgres;Password=postgres;Database=postgres;Enlist=false";
                options.TableName        = "pyncette" + args[1];
            })
            ;    //.AddInMemoryRepository();
            var rand = new Random();

            await using (var context = await csissors.BuildAsync(CancellationToken.None))
            {
                switch (args[0])
                {
                case "populate":
                    var taskInstance = context.Tasks.DynamicTasks[0];
                    for (int i = 0; i < 100000; ++i)
                    {
                        await context.ScheduleTask(taskInstance,
                                                   Guid.NewGuid().ToString(),
                                                   TaskConfiguration.Default
                                                   .WithSchedule(new IntervalSchedule(TimeSpan.FromSeconds(rand.Next(60, 3600)), false))
                                                   .WithExecutionMode(ExecutionMode.AtMostOnce),
                                                   CancellationToken.None
                                                   );
                    }
                    Console.WriteLine("Scheduled 1000 tasks");
                    break;    //goto case "run";

                case "run":
                    await context.RunAsync(CancellationToken.None);

                    break;
                }
            }
        }
Exemplo n.º 8
0
        public async Task BuildAsync_ShouldBuildDynamicTasks_Delegate()
        {
            // arrange
            var sut = new CsissorsBuilder()
                      .AddMockRepository()
                      .AddDynamicTask("mock", async() => throw new IOException());

            // act
            var context = await sut.BuildAsync(CancellationToken.None);

            // assert
            context.Tasks.DynamicTasks.Should().HaveCount(1);
            var task = context.Tasks.DynamicTasks[0];

            task.Name.Should().Be("mock");
            await Assert.ThrowsAsync <IOException>(() => task.ExecuteAsync(Mock.Of <ITaskContext>()));
        }
Exemplo n.º 9
0
        static async Task MainAsync()
        {
            var conf = new TaskConfiguration(
                new IntervalSchedule(TimeSpan.FromSeconds(1), false),
                FailureMode.None,
                ExecutionMode.AtLeastOnce,
                TimeSpan.FromMinutes(1),
                new Dictionary <string, object?> {
                { "username", "tibor" }
            }
                );

            var csissors = new CsissorsBuilder()
                           .ConfigureServices(services =>
            {
                services.AddLogging(configure => configure.AddConsole(configure =>
                {
                    configure.Format        = ConsoleLoggerFormat.Default;
                    configure.IncludeScopes = true;
                }));
            })
                           //.AddTaskContainer<TaskContainer>()
                           //.AddAssembly()
                           .AddDynamicTask("yupee", async(ITaskContext context, ILoggerFactory logger) =>
            {
                logger.CreateLogger("yuhuhu").LogInformation($"Hello {JsonConvert.SerializeObject(context.Task.Configuration.Data)}");
                await Task.Yield();
            })
                           //.AddTask("whipee", conf, async (ITaskContext context, ILoggerFactory logger) =>
                           //{
                           //    logger.CreateLogger("yuhuhu").LogInformation("Hello1", context.Task.Configuration.Data);
                           //    await Task.Yield();
                           //})
                           //.AddMiddleware<RetryMiddleware>()
                           .AddPostgresRepository(options =>
            {
                options.ConnectionString = "Host=localhost;Username=postgres;Password=postgres;Database=postgres";
                options.TableName        = "pyncette" + Guid.NewGuid().ToString("N");
            })

                           /*.AddRedisRepository(options =>
                            * {
                            *  options.ConfigurationOptions = ConfigurationOptions.Parse("localhost");
                            * })*/
            ;
            var cts = new CancellationTokenSource();

            cts.CancelAfter(50000);
            await using (var context = await csissors.BuildAsync(cts.Token))
            {
                var task = context.Tasks.DynamicTasks[0];
                await context.ScheduleTask(task, "hello1", new TaskConfiguration(
                                               new IntervalSchedule(TimeSpan.FromSeconds(2), false),
                                               FailureMode.None,
                                               ExecutionMode.AtLeastOnce,
                                               TimeSpan.FromMinutes(1),
                                               new Dictionary <string, object?> {
                    { "username", "tibor" }
                }
                                               ), cts.Token);

                await context.ScheduleTask(task, "hello2", new TaskConfiguration(
                                               new IntervalSchedule(TimeSpan.FromSeconds(4), false),
                                               FailureMode.None,
                                               ExecutionMode.AtLeastOnce,
                                               TimeSpan.FromMinutes(1),
                                               new Dictionary <string, object?> {
                    { "username", "liza" }, { "nonExistent", "liza" }
                }
                                               ), cts.Token);

                await context.ScheduleTask(task, "hello3", new TaskConfiguration(
                                               new CronSchedule(Cronos.CronExpression.Parse("* * * * *"), TimeZoneInfo.Utc, false),
                                               FailureMode.None,
                                               ExecutionMode.AtLeastOnce,
                                               TimeSpan.FromMinutes(1),
                                               new Dictionary <string, object?> {
                    { "username", "liza" }, { "nonExistent", "liza" }
                }
                                               ), cts.Token);

                await context.RunAsync(cts.Token);
            }
        }