public async Task GivenARunningTask_WhenGetNextTask_ThenNoTaskShouldBeReturned() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = SqlServerTaskConsumerTestsTypeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, false, CancellationToken.None); _ = await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).FirstOrDefault(); Assert.Null(taskInfo); }
public SqlServerTaskConsumer( IOptions <TaskHostingConfiguration> taskHostingConfiguration, SqlConnectionWrapperFactory sqlConnectionWrapperFactory, ILogger <SqlServerTaskConsumer> logger) { EnsureArg.IsNotNull(taskHostingConfiguration, nameof(taskHostingConfiguration)); EnsureArg.IsNotNull(sqlConnectionWrapperFactory, nameof(sqlConnectionWrapperFactory)); EnsureArg.IsNotNull(logger, nameof(logger)); _taskHostingConfiguration = taskHostingConfiguration.Value; _sqlConnectionWrapperFactory = sqlConnectionWrapperFactory; _logger = logger; }
public TaskHostingBackgroundService( Func <IScoped <TaskHosting> > taskHostingFactory, IOptions <TaskHostingConfiguration> taskHostingConfiguration, ILogger <TaskHostingBackgroundService> logger) { EnsureArg.IsNotNull(taskHostingFactory, nameof(taskHostingFactory)); EnsureArg.IsNotNull(taskHostingConfiguration?.Value, nameof(taskHostingConfiguration)); EnsureArg.IsNotNull(logger, nameof(logger)); _taskHostingFactory = taskHostingFactory; _taskHostingConfiguration = taskHostingConfiguration.Value; _logger = logger; }
public async Task GivenARunningTask_WhenReachMaxRetryCount_ThenResetShouldFail() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, MaxRetryCount = 1, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); TaskResultData result = new TaskResultData(TaskResult.Fail, "Result"); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); taskInfo = await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, taskInfo.RunId, CancellationToken.None); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); await Assert.ThrowsAsync <TaskAlreadyCompletedException>(async() => await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, taskInfo.RunId, CancellationToken.None)); taskInfo = await sqlServerTaskManager.GetTaskAsync(taskInfo.TaskId, CancellationToken.None); Assert.Equal(TaskStatus.Completed, taskInfo.Status); Assert.Equal(JsonConvert.SerializeObject(result), taskInfo.Result); }
public CreateImportRequestHandler( ITaskManager taskManager, IOptions <OperationsConfiguration> operationsConfig, IOptions <TaskHostingConfiguration> taskHostingConfiguration, ILogger <CreateImportRequestHandler> logger, IAuthorizationService <DataActions> authorizationService) { EnsureArg.IsNotNull(taskManager, nameof(taskManager)); EnsureArg.IsNotNull(operationsConfig.Value, nameof(operationsConfig)); EnsureArg.IsNotNull(taskHostingConfiguration.Value, nameof(taskHostingConfiguration)); EnsureArg.IsNotNull(authorizationService, nameof(authorizationService)); EnsureArg.IsNotNull(logger, nameof(logger)); _taskManager = taskManager; _importTaskConfiguration = operationsConfig.Value.Import; _taskHostingConfiguration = taskHostingConfiguration.Value; _authorizationService = authorizationService; _logger = logger; }
public async Task GivenARunningTask_WhenResetTask_ThenTaskShouldBeReturned() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); string firstRunId = taskInfo.RunId; TaskResultData result = new TaskResultData(TaskResult.Success, "Result"); taskInfo = await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, taskInfo.RunId, CancellationToken.None); Assert.Equal(1, taskInfo.RetryCount); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); Assert.NotNull(taskInfo); Assert.NotEqual(firstRunId, taskInfo.RunId); }
public async Task GivenActiveTasks_WhenCreateWithSameTypeRunningTask_ThenConflictExceptionShouldBeThrow() { string queueId = Guid.NewGuid().ToString(); string taskId1 = Guid.NewGuid().ToString(); string taskId2 = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; short conflictTestTypeId = 1000; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); TaskInfo taskInfo1 = new TaskInfo() { TaskId = taskId1, QueueId = queueId, TaskTypeId = conflictTestTypeId, InputData = inputData, }; TaskInfo taskInfo2 = new TaskInfo() { TaskId = taskId2, QueueId = queueId, TaskTypeId = conflictTestTypeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo1, true, CancellationToken.None); await Assert.ThrowsAnyAsync <TaskConflictException>(() => sqlServerTaskManager.CreateTaskAsync(taskInfo2, true, CancellationToken.None)); }
public async Task GivenARunningTask_WhenUpdateWithWrongRunid_ThenExceptionShouldBeThrew() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, MaxRetryCount = 1, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); TaskResultData result = new TaskResultData(TaskResult.Fail, "Result"); taskInfo = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); await Assert.ThrowsAsync <TaskNotExistException>(async() => await sqlServerTaskConsumer.KeepAliveAsync(taskInfo.TaskId, "invalid", CancellationToken.None)); await Assert.ThrowsAsync <TaskNotExistException>(async() => await sqlServerTaskConsumer.CompleteAsync(taskInfo.TaskId, result, "invalid", CancellationToken.None)); await Assert.ThrowsAsync <TaskNotExistException>(async() => await sqlServerTaskConsumer.ResetAsync(taskInfo.TaskId, result, "invalid", CancellationToken.None)); }
public async Task GivenListOfTasksInQueue_WhenGetNextTask_ThenAvailableTasksShouldBeReturned() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); for (int i = 0; i < 5; ++i) { string taskId = Guid.NewGuid().ToString(); short typeId = 1; string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = typeId, InputData = inputData, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, CancellationToken.None); } var result = (await sqlServerTaskConsumer.GetNextMessagesAsync(3, 60, CancellationToken.None)).ToList(); Assert.Equal(3, result.Count()); }
public async Task GivenATaskCreated_WhenTaskCanceled_ThenCanBePickedUp() { string queueId = Guid.NewGuid().ToString(); TaskHostingConfiguration config = new TaskHostingConfiguration() { Enabled = true, QueueId = queueId, TaskHeartbeatTimeoutThresholdInSeconds = 60, }; IOptions <TaskHostingConfiguration> taskHostingConfig = Substitute.For <IOptions <TaskHostingConfiguration> >(); taskHostingConfig.Value.Returns(config); SqlServerTaskManager sqlServerTaskManager = new SqlServerTaskManager(_fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskManager> .Instance); SqlServerTaskConsumer sqlServerTaskConsumer = new SqlServerTaskConsumer(taskHostingConfig, _fixture.SqlConnectionWrapperFactory, NullLogger <SqlServerTaskConsumer> .Instance); string taskId = Guid.NewGuid().ToString(); string inputData = "inputData"; TaskInfo taskInfo = new TaskInfo() { TaskId = taskId, QueueId = queueId, TaskTypeId = SqlServerTaskConsumerTestsTypeId, InputData = inputData, MaxRetryCount = 1, }; _ = await sqlServerTaskManager.CreateTaskAsync(taskInfo, false, CancellationToken.None); _ = await sqlServerTaskManager.CancelTaskAsync(taskInfo.TaskId, CancellationToken.None); var taskInfoResult = (await sqlServerTaskConsumer.GetNextMessagesAsync(1, 60, CancellationToken.None)).First(); Assert.Equal(taskInfo.TaskId, taskInfoResult.TaskId); }