/// <summary> /// Registers a task through the task management API asynchronously. /// </summary> /// <returns>Returns a RegisterTaskResult object containing information about the registered task.</returns> public static Task <RegisterTaskResult> RegisterTaskAsync(RegisterTaskRequest requestData) { // start the task registration process only if a url is provided var taskManUrl = Url; return(string.IsNullOrEmpty(taskManUrl) ? Task.FromResult <RegisterTaskResult>(null) : Instance.RegisterTaskAsync(taskManUrl, requestData)); }
public Task ExecuteAsync(CancellationToken cancellationToken) { if (!_ADSynchIsAvailable.HasValue) { _ADSynchIsAvailable = Settings.GetSettingsByName <Settings>(ADSyncSettingsName, null) != null; SnLog.WriteInformation("Active Directory synch feature is " + (_ADSynchIsAvailable.Value ? string.Empty : "not ") + "available."); } if (!_ADSynchIsAvailable.Value) { return(Task.CompletedTask); } // skip check if the feature is not enabled if (!Settings.GetValue(ADSyncSettingsName, "Enabled", null, false)) { return(Task.CompletedTask); } if (!AdSyncTimeArrived()) { return(Task.CompletedTask); } #pragma warning disable 618 // fallback for legacy code var appId = _taskManagementOptions.ApplicationId; if (string.IsNullOrEmpty(appId)) { appId = SnTaskManager.Settings.AppId; } var appUrl = _taskManagementOptions.ApplicationUrl; if (string.IsNullOrEmpty(appUrl)) { appUrl = SnTaskManager.Settings.AppUrl; } #pragma warning restore 618 var requestData = new RegisterTaskRequest { Type = "SyncAD2Portal", Title = "SyncAD2Portal", Priority = TaskPriority.Immediately, AppId = appId, TaskData = JsonConvert.SerializeObject(new { SiteUrl = appUrl }), Tag = string.Empty, FinalizeUrl = "/odata.svc/('Root')/Ad2PortalSyncFinalizer" }; // Fire and forget: we do not need the result of the register operation. // (we have to start a task here instead of calling RegisterTaskAsync // directly because the asp.net sync context callback would fail) Task.Run(() => _taskManager.RegisterTaskAsync(requestData, cancellationToken), cancellationToken); return(Task.CompletedTask); }
// ================================================================================== Static API /// <summary> /// Registers a task through the task management API. /// If possible, use the asynchronous version of this method. /// </summary> /// <returns>Returns a RegisterTaskResult object containing information about the registered task.</returns> public static RegisterTaskResult RegisterTask(RegisterTaskRequest requestData) { // start the task registration process only if a url is provided var taskManUrl = Url; if (string.IsNullOrEmpty(taskManUrl)) { return(null); } // make this a synchron call return(Instance.RegisterTaskAsync(taskManUrl, requestData).GetAwaiter().GetResult()); }
public virtual async Task <RegisterTaskResult> RegisterTaskAsync(RegisterTaskRequest requestData, CancellationToken cancellationToken) { var taskManagementUrl = _options.GetUrlOrSetting(); if (string.IsNullOrEmpty(taskManagementUrl) || requestData == null) { return(null); } while (true) { try { return(await _client.RegisterTaskAsync(requestData).ConfigureAwait(false)); } catch (Exception ex) { // look for a special error message about an unknown app id if (ex is TaskManagementException && string.CompareOrdinal(ex.Message, RegisterTaskRequest.ERROR_UNKNOWN_APPID) == 0) { // try to re-register the app if (await RegisterApplicationAsync(cancellationToken)) { // skip error logging and try to register the task again continue; } } SnLog.WriteException(ex, "Error during task registration.", EventId.TaskManagement.General, properties: new Dictionary <string, object> { { "TaskManagementUrl", taskManagementUrl }, { "Type", requestData.Type }, { "Title", requestData.Title }, { "Data", requestData.TaskData } }); // do not retry again after a real error break; } } // no need to throw an exception, we already logged the error return(null); }
private static void StartADSync() { if (!_adsyncAvailable.HasValue) { _adsyncAvailable = Settings.IsSettingsAvailable(ADSyncSettingsName); SnLog.WriteInformation("Active Directory synch feature is " + (_adsyncAvailable.Value ? string.Empty : "not ") + "available."); } if (!_adsyncAvailable.Value) { return; } // skip check if not enough cycles have passed or the feature is not enabled if (!IsTaskExecutable(ADSYNC_CYCLECOUNT) || !Settings.GetValue(ADSyncSettingsName, "Enabled", null, false)) { return; } if (!AdSyncTimeArrived()) { return; } var requestData = new RegisterTaskRequest { Type = "SyncAD2Portal", Title = "SyncAD2Portal", Priority = TaskPriority.Immediately, AppId = Settings.GetValue(SnTaskManager.Settings.SETTINGSNAME, SnTaskManager.Settings.TASKMANAGEMENTAPPID, null, "SenseNet"), TaskData = JsonConvert.SerializeObject(new { SiteUrl = Settings.GetValue <string>(SnTaskManager.Settings.SETTINGSNAME, SnTaskManager.Settings.TASKMANAGEMENTAPPLICATIONURL) }), Tag = string.Empty, FinalizeUrl = "/odata.svc/('Root')/Ad2PortalSyncFinalizer" }; // Fire and forget: we do not need the result of the register operation. // (we have to start a task here instead of calling RegisterTaskAsync // directly because the asp.net sync context callback would fail) Task.Run(() => SnTaskManager.RegisterTaskAsync(requestData)); }
public static RegisterTaskResult RegisterTask(RegisterTaskRequest requestData) { return(SnTaskManager.RegisterTask(requestData)); }
public RegisterTaskResult RegisterTask(RegisterTaskRequest taskRequest) { Application app = null; try { // load the corresponding application to make sure the appid is valid app = ApplicationHandler.GetApplication(taskRequest.AppId); } catch (Exception ex) { SnLog.WriteException(ex, "Error loading app for id " + taskRequest.AppId, EventId.TaskManagement.General); } // If we do not know the appid, we must not register the task. Client applications // must observe this response and try to re-register the application, before // trying to register the task again (this can happen if the TaskManagement Web // was unreachable when the client application tried to register the appid before). if (app == null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, RegisterTaskRequest.ERROR_UNKNOWN_APPID)); } RegisterTaskResult result = null; try { // calculate hash with the default algrithm if not given var hash = taskRequest.Hash == 0 ? ComputeTaskHash(taskRequest.Type + taskRequest.AppId + taskRequest.Tag + taskRequest.TaskData) : taskRequest.Hash; result = TaskDataHandler.RegisterTask( taskRequest.Type, taskRequest.Title, taskRequest.Priority, taskRequest.AppId, taskRequest.Tag, taskRequest.FinalizeUrl, hash, taskRequest.TaskData, taskRequest.MachineName); } catch (Exception ex) { // the client app needs to be notified throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } try { // notify agents AgentHub.BroadcastMessage(result.Task); // notify monitor clients TaskMonitorHub.OnTaskEvent(SnTaskEvent.CreateRegisteredEvent( result.Task.Id, result.Task.Title, string.Empty, result.Task.AppId, result.Task.Tag, null, result.Task.Type, result.Task.Order, result.Task.Hash, result.Task.TaskData)); } catch (Exception ex) { // The task has been created successfully, this error is only about // notification, so client applications should not be notified. SnLog.WriteException(ex, "Error during agent or monitor notification after a task was registered.", EventId.TaskManagement.Communication); } return(result); }
public static Task <RegisterTaskResult> RegisterTaskAsync(RegisterTaskRequest requestData) { return(Instance.RegisterTaskAsync(requestData, CancellationToken.None)); }
public static RegisterTaskResult RegisterTask(RegisterTaskRequest requestData) { return(Instance.RegisterTaskAsync(requestData, CancellationToken.None).GetAwaiter().GetResult()); }
public async Task <RegisterTaskResult> RegisterTask([FromBody] RegisterTaskRequest taskRequest) { Application app = null; try { // load the corresponding application to make sure the appid is valid app = _applicationHandler.GetApplication(taskRequest.AppId); } catch (Exception ex) { SnLog.WriteException(ex, "Error loading app for id " + taskRequest.AppId, EventId.TaskManagement.General); } // If we do not know the appid, we must not register the task. Client applications // must observe this response and try to re-register the application, before // trying to register the task again (this can happen if the TaskManagement Web // was unreachable when the client application tried to register the appid before). if (app == null) { return(new RegisterTaskResult { Error = RegisterTaskRequest.ERROR_UNKNOWN_APPID }); } RegisterTaskResult result; try { // calculate hash with the default algorithm if not given var hash = taskRequest.Hash == 0 ? ComputeTaskHash(taskRequest.Type + taskRequest.AppId + taskRequest.Tag + taskRequest.TaskData) : taskRequest.Hash; result = await _dataHandler.RegisterTaskAsync( taskRequest.Type, taskRequest.Title, taskRequest.Priority, taskRequest.AppId, taskRequest.Tag, taskRequest.FinalizeUrl, hash, taskRequest.TaskData, taskRequest.MachineName, HttpContext.RequestAborted).ConfigureAwait(false); } catch (Exception ex) { var msg = $"Task registration failed. {ex.Message} AppId: {app.AppId}, " + $"Task: {taskRequest.Type}, Title: {taskRequest.Title}"; SnLog.WriteException(ex, msg); return(new RegisterTaskResult { Error = RegisterTaskResult.ErrorTaskRegistrationFailed }); } try { // notify agents await _agentHub.BroadcastNewTask(result.Task).ConfigureAwait(false); // notify monitor clients await _monitorHub.OnTaskEvent(SnTaskEvent.CreateRegisteredEvent( result.Task.Id, result.Task.Title, string.Empty, result.Task.AppId, result.Task.Tag, null, result.Task.Type, result.Task.Order, result.Task.Hash, result.Task.TaskData)).ConfigureAwait(false); } catch (Exception ex) { // The task has been created successfully, this error is only about // notification, so client applications should not be notified. SnLog.WriteException(ex, "Error during agent or monitor notification after a task was registered.", EventId.TaskManagement.Communication); } return(result); }