public async Task InvokeAsync(ScheduledTriggerInfo triggerInfo, TriggerContext context, CancellationToken cancellationToken) { while (true) { var synchronizedTime = GetNextTriggerTime(triggerInfo, DateTimeOffset.UtcNow); if (synchronizedTime == null) { return; } var nextTriggerTime = synchronizedTime.Value; if (!triggerInfo.SynchronizeTimeZone) { //ignore offset nextTriggerTime = new DateTimeOffset(nextTriggerTime.DateTime, DateTimeOffset.UtcNow.Offset); } context.ReportNextTrigger(nextTriggerTime); await Task.Delay(nextTriggerTime - DateTimeOffset.UtcNow, cancellationToken); //important: use synchronized time var session = await context.CreateSession(SessionKey.Create("ScheduledTrigger", synchronizedTime.Value)); await session.Invoke(); } }
public async Task InvokeAsync(OnAppStartupTriggerInfo triggerInfo, TriggerContext context, CancellationToken cancellationToken) { var session = await context.CreateSession(SessionKey.Create("OnAppStartup", DateTimeOffset.UtcNow)); await session.Invoke(); await Task.Delay(int.MaxValue, cancellationToken); }
public async Task <TaskSessionsInfo> BizActionAsync(MazeTask inputData) { var builder = new TaskSessionInfoBuilder(inputData.Id); builder.TaskSessionAdded += BuilderOnTaskSessionAdded; builder.TaskExecutionAdded += BuilderOnTaskExecutionAdded; builder.TaskResultAdded += BuilderOnTaskResultAdded; var storage = new MemoryTaskResultStorage(); //trigger locally await _management.TriggerNow(inputData, SessionKey.Create("Execute"), storage); builder.Add(new TaskSessionsInfo { Sessions = storage.Sessions, Executions = storage.Executions, Results = storage.CommandResults }, TargetId.ServerId); //trigger on clients var audienceFilter = new AudienceFilter(inputData.Audience); var clients = (await _dbContext.Clients.Select(x => x.ClientId).ToListAsync()).ToHashSet(); var onlineClients = _connectionManager.ClientConnections.Where(x => clients.Contains(x.Key)); var tasks = new Dictionary <Task <TaskSessionsInfo>, int>(); foreach (var onlineClient in onlineClients) { if (audienceFilter.Invoke(onlineClient.Key)) { //add all at once because the tasks don't do anything except waiting for the completion source anyways var task = TasksResource.ExecuteTask(inputData, _taskComponentResolver, _xmlSerializerCache, onlineClient.Value); tasks.Add(task, onlineClient.Key); } } while (tasks.Any()) { var task = await Task.WhenAny(tasks.Keys); builder.Add(task.Result, new TargetId(tasks[task])); tasks.Remove(task); } return(builder.Build()); }
public async Task InvokeAsync(SystemRestartTriggerInfo triggerInfo, TriggerContext context, CancellationToken cancellationToken) { var now = DateTime.UtcNow; var uptime = TimeSpan.FromMilliseconds(GetTickCount64()); var startupTime = now.Add(-uptime); var session = await context.CreateSession(SessionKey.Create("SystemRestart")); if (!session.Info.Executions.Any(x => x.CreatedOn > startupTime)) { await session.Invoke(); } await Task.Delay(TimeSpan.MaxValue, cancellationToken); }
public async Task TestOpenNonExistingSession() { var fileSystem = new MockFileSystem(); var options = new TasksOptions { SessionsDirectory = "C:\\test" }; var taskId = Guid.Parse("CB49D689-95FD-4A09-A78A-3A4397E9425E"); var sessionManager = new DatabaseTaskStorage(fileSystem, null, new OptionsWrapper <TasksOptions>(options)); var session = await sessionManager.OpenSession(SessionKey.Create("test"), new MazeTask { Id = taskId }, "Test123"); Assert.Empty(fileSystem.AllFiles); Assert.NotNull(session); }
public async Task InvokeAsync(OnAppStartupTriggerInfo triggerInfo, TriggerContext context, CancellationToken cancellationToken) { var session = await context.CreateSession(SessionKey.Create("OnAppStart", DateTime.Now.Date)); if (triggerInfo.OncePerDay) { if (session.Info.Executions.Any()) { await Task.Delay(int.MaxValue, cancellationToken); } } await session.Invoke(); await Task.Delay(int.MaxValue, cancellationToken); }
public async Task <IActionResult> ExecuteTask([FromServices] ITaskComponentResolver taskComponentResolver, [FromServices] IXmlSerializerCache serializerCache) { var mazeTask = new MazeTaskReader(Request.Body, taskComponentResolver, serializerCache); var task = mazeTask.ReadTask(); var memoryStorage = new MemoryTaskStorage(); await _clientTaskManager.TriggerNow(task, SessionKey.Create("Execute"), memoryStorage); return(Ok(new TaskSessionsInfo { Sessions = memoryStorage.Sessions.Select(x => new TaskSessionDto { TaskReferenceId = x.TaskReferenceId, TaskSessionId = x.TaskSessionId, Description = x.Description, CreatedOn = x.CreatedOn }).ToList(), Executions = memoryStorage.Executions.Select(x => new TaskExecutionDto { TaskExecutionId = x.TaskExecutionId, TaskReferenceId = x.TaskReferenceId, TaskSessionId = x.TaskSessionId, CreatedOn = x.CreatedOn }).ToList(), Results = memoryStorage.CommandResults.Select(x => new CommandResultDto { CommandResultId = x.CommandResultId, TaskExecutionId = x.TaskExecutionId, CommandName = x.CommandName, Result = x.Result, Status = x.Status, FinishedAt = x.FinishedAt }).ToList() })); }
public OnJoinTriggerService(IClientEventNotifier clientEvents) { _clientEvents = clientEvents; _clientConnectedLock = new AsyncLock(); _sessionTrigger = new AsyncLazy <TaskSessionTrigger>(() => _context.CreateSession(SessionKey.Create("OnClientJoin"))); }
public async Task InvokeAsync(ImmediatelyTriggerInfo triggerInfo, TriggerContext context, CancellationToken cancellationToken) { var session = await context.CreateSession(SessionKey.Create("Immediate", DateTimeOffset.UtcNow)); await session.Invoke(); }