Example #1
0
        public Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if (!_ADSynchIsAvailable.HasValue)
            {
                _ADSynchIsAvailable = Settings.IsSettingsAvailable(ADSyncSettingsName);
                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);
            }

            var requestData = new RegisterTaskRequest
            {
                Type     = "SyncAD2Portal",
                Title    = "SyncAD2Portal",
                Priority = TaskPriority.Immediately,

                AppId       = _taskManagementOptions.GetApplicationIdOrSetting(),
                TaskData    = JsonConvert.SerializeObject(new { SiteUrl = _taskManagementOptions.GetApplicationUrlOrSetting() }),
                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);
        }
Example #2
0
        public virtual async Task <bool> RegisterApplicationAsync(CancellationToken cancellationToken)
        {
            var taskManagementUrl = _options.GetUrlOrSetting();

            if (string.IsNullOrEmpty(taskManagementUrl))
            {
                SnTrace.TaskManagement.Write("Task management url is empty, application is not registered.");
                return(false);
            }

            var requestData = new RegisterApplicationRequest
            {
                AppId          = _options.GetApplicationIdOrSetting(),
                ApplicationUrl = _options.GetApplicationUrlOrSetting()
            };

            try
            {
                await _client.RegisterApplicationAsync(requestData).ConfigureAwait(false);

                SnLog.WriteInformation("Task management app registration was successful.", EventId.TaskManagement.General, properties: new Dictionary <string, object>
                {
                    { "TaskManagementUrl", taskManagementUrl },
                    { "AppId", requestData.AppId }
                });

                return(true);
            }
            catch (Exception ex)
            {
                SnLog.WriteException(ex, "Error during app registration.", EventId.TaskManagement.General,
                                     properties: new Dictionary <string, object>
                {
                    { "TaskManagementUrl", taskManagementUrl },
                    { "AppId", requestData.AppId },
                    { "ApplicationUrl", requestData.ApplicationUrl }
                });
            }

            // no need to throw an exception, we already logged the error
            return(false);
        }