示例#1
0
        private void StopManagedTask(ManagedTask task)
        {
            task.InternalStatus = InternalTaskStatus.STOPSTART;
            var stopRequest = new QlikRequest
            {
                ManagedTaskId = task.Id.ToString()
            };
            var stopFunction = new StopFunction(runtimeOptions);

            stopFunction.StopReportJobs(stopRequest, true);
        }
示例#2
0
        public override async Task ExecuteFunction(IAsyncStreamReader <BundledRows> requestStream,
                                                   IServerStreamWriter <BundledRows> responseStream,
                                                   ServerCallContext context)
        {
            var response = new QlikResponse();

            try
            {
                logger.Debug("The method 'ExecuteFunction' is called...");

                //Read function header
                var functionHeader = context.RequestHeaders.ParseIMessageFirstOrDefault <FunctionRequestHeader>();

                //Read common header
                var commonHeader = context.RequestHeaders.ParseIMessageFirstOrDefault <CommonRequestHeader>();

                //Set appid
                logger.Info($"The Qlik app id '{commonHeader?.AppId}' in header found.");
                var qlikAppId = commonHeader?.AppId;

                //Set qlik user
                logger.Info($"The Qlik user '{commonHeader?.UserId}' in header found.");
                var domainUser = new DomainUser(commonHeader?.UserId);

                //Very important code line
                await context.WriteResponseHeadersAsync(new Metadata { { "qlik-cache", "no-store" } });

                //Read parameter from qlik
                var row      = GetParameter(requestStream);
                var userJson = GetParameterValue(0, row);

                //Parse request from qlik script
                logger.Debug("Parse user request...");
                var request = QlikRequest.Parse(domainUser, qlikAppId, userJson);

                var functionCall = (ConnectorFunction)functionHeader.FunctionId;
                logger.Debug($"Call Function id: '{functionCall}' from client '{context?.Peer}'.");

                if (functionCall == ConnectorFunction.START)
                {
                    #region Switch qlik user to app owner
                    if (domainUser?.UserId == "sa_scheduler" && domainUser?.UserDirectory == "INTERNAL")
                    {
                        try
                        {
                            var oldUser = domainUser.ToString();
                            domainUser = new DomainUser("INTERNAL\\ser_scheduler");
                            logger.Debug($"Change Qlik user '{oldUser}' to task service user '{domainUser}'.");
                            var connection = RuntimeOptions.Config.Connection;
                            var tmpsession = RuntimeOptions.SessionHelper.Manager.CreateNewSession(connection, domainUser, qlikAppId);
                            if (tmpsession == null)
                            {
                                throw new Exception("No session cookie generated. (Qlik Task)");
                            }
                            var qrsHub = new QlikQrsHub(RuntimeOptions.Config.Connection.ServerUri, tmpsession.Cookie);
                            qrsHub.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
                            domainUser = request.GetAppOwner(qrsHub, qlikAppId);
                            if (domainUser == null)
                            {
                                throw new Exception("The owner of the App could not found.");
                            }
                            logger.Debug($"App owner '{domainUser}' found.");
                            request.QlikUser = domainUser;
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex, "Could not switch the task user to real qlik user.");
                        }
                    }
                    #endregion

                    #region Function call SER.START
                    logger.Debug("Function call SER.START...");
                    var newManagedTask = new ManagedTask()
                    {
                        StartTime    = DateTime.Now,
                        Message      = "Create new report job...",
                        Cancellation = new CancellationTokenSource(),
                        Status       = 0
                    };
                    RuntimeOptions.TaskPool.ManagedTasks.TryAdd(newManagedTask.Id, newManagedTask);
                    var startFunction = new StartFunction(RuntimeOptions);
                    startFunction.StartReportJob(request, newManagedTask);
                    response.TaskId = newManagedTask.Id.ToString();
                    #endregion
                }
                else if (functionCall == ConnectorFunction.STOP)
                {
                    #region Function call SER.STOP
                    logger.Debug("Function call SER.STOP...");
                    var stopFunction = new StopFunction(RuntimeOptions);
                    stopFunction.StopReportJobs(request);
                    if (request.ManagedTaskId == "all")
                    {
                        response.Log = "All report jobs is stopping...";
                    }
                    else
                    {
                        response.Log = $"Report job '{request.ManagedTaskId}' is stopping...";
                    }
                    response.Status = 4;
                    #endregion
                }
                else if (functionCall == ConnectorFunction.RESULT)
                {
                    #region Function call SER.RESULT
                    logger.Debug("Function call SER.RESULT...");
                    var resultFunction = new ResultFunction(RuntimeOptions);
                    response = resultFunction.FormatJobResult(request);
                    #endregion
                }
                else if (functionCall == ConnectorFunction.STATUS)
                {
                    #region Function call SER.STATUS
                    logger.Debug("Function call SER.STATUS...");
                    var statusFunction = new StatusFunction(RuntimeOptions);
                    response = statusFunction.GetStatusResponse(request);
                    #endregion
                }
                else
                {
                    throw new Exception($"The id '{functionCall}' of the function call was unknown.");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"The method 'ExecuteFunction' failed with error '{ex.Message}'.");
                response.Status = -1;
                response.SetErrorMessage(ex);
            }
            finally
            {
                logger.Trace($"Qlik status result: {JsonConvert.SerializeObject(response)}");
                await responseStream.WriteAsync(GetResult(response));

                LogManager.Flush();
            }
        }