예제 #1
0
 protected virtual void ProcessPostActions()
 {
     Logger?.LogInformation($"Post action processing is started for '{GetType().Name}'.");
     try
     {
         PostProcessor.Execute();
         Logger?.LogInformation(
             $"Post action processing is completed for '{GetType().Name}' with {PostProcessor.Count} post actions executed.");
     }
     catch (ActionProcessorAggregateException ex)
     {
         var errorCount = ex.InnerExceptions.Count;
         Logger?.LogInformation(
             $@"Post action processing is completed with some errors for '{GetType().Name}' with {PostProcessor.Count - errorCount} of {PostProcessor.Count} post actions executed successfully.");
         throw;
     }
     catch
     {
         Logger?.LogInformation(
             $"Post action processing is completed for '{GetType().Name}' with errors.");
         throw;
     }
 }
예제 #2
0
        private void OnContext(IAsyncResult ar)
        {
            DateTime reqStartTime = DateTime.Now;

            HttpListenerContext context;

            try
            {
                context = listener.EndGetContext(ar);
            }
            catch (HttpListenerException)
            {
                ServerLog.LogInfo("HttpListener has been shut down");
                return;
            }

            if (!stopping)
            {
                listener.BeginGetContext(OnContext, null);
            }

            var processWebRequest = new Task(() =>
            {
                System.Threading.Interlocked.Increment(ref totalRequests);
                byte[] json           = null;
                int httpStatus        = 200;
                string redirectUrl    = null;
                string outContentType = "application/json";
                try
                {
                    string processor;
                    string command;
                    string jsonInput;

                    string module;
                    string contentType;
                    string source;
                    string agent;


                    var args = ParseUrl(context.Request, out processor, out command, out jsonInput, out module, out contentType, out source, out agent);

                    if (module == "test")
                    {
                        if (processor == "util")
                        {
                            if (command == "stats")
                            {
                                StatRespond(context, reqStartTime);
                            }
                            else if (command == "health")
                            {
                                HealthRespond(context, reqStartTime);
                            }
                            else
                            {
                                throw new ApplicationException(string.Format("Unknown util command: {0}", command));
                            }
                            return;
                        }
                    }
                    else if (module == "action")
                    {
                        string jsonString = actionProcessor.Execute(command, args, jsonInput, contentType, source, agent, out outContentType, out redirectUrl, out httpStatus);
                        if (jsonString == null)
                        {
                            jsonString = "";
                        }

                        json = Encoding.UTF8.GetBytes(jsonString);
                    }
                    else if (module == "db")
                    {
                        string jsonString = dataProcessor.Execute(command, args, jsonInput, contentType, source, agent, out outContentType, out redirectUrl, out httpStatus);
                        if (jsonString == null)
                        {
                            jsonString = "";
                        }

                        json = Encoding.UTF8.GetBytes(jsonString);
                    }
                    else
                    {
                        throw new ApplicationException(string.Format("Unknown module: {0}", module));
                    }
                }
                catch (Exception e)
                {
                    ExceptionRespond(context, e, 500, reqStartTime);
                    return;
                }

                if (redirectUrl != null)
                {
                    RespondRedirect(context, redirectUrl);
                }
                else
                {
                    Respond(context, json, httpStatus, reqStartTime, outContentType);
                }
            });

            lock (runningTasks)
            {
                if (stopping)
                {
                    Respond(context,
                            serverCharSet.GetBytes("MatchMe is shutting down.")
                            , 500, reqStartTime);
                    return;
                }

                runningTasks.Add(processWebRequest);
            }

            processWebRequest.ContinueWith(t =>
            {
                lock (runningTasks)
                {
                    runningTasks.Remove(t);
                }

                if (t.IsFaulted)
                {
                    ServerLog.LogException(t.Exception.InnerException, "Exception was unhandled in task");
                }
            });


            processWebRequest.Start();
        }