Exemplo n.º 1
0
        public static void Process(IEnumerable <ResponseResultItem> resp)
        {
            if (resp == null || resp.Count() == 0)
            {
                return;
            }

            Task.Factory.StartNew(() => {
                List <string> exceptionMd5s  = new List <string>();
                List <long> exceptionTaskids = new List <long>();

                var buf = new StringBuilder();
                buf.AppendLine("处理结果" + resp.Count());

                //处理相应
                foreach (var item in resp)
                {
                    long?taskId    = null;
                    string taskMd5 = null;

                    try
                    {
                        taskId = TaskIdMaps.GetId(item.url);

                        if (!taskId.HasValue)
                        {
                            taskMd5 = getMd5FromResultUrl(item.url);
                        }

                        if (item.code == 0)//识别成功
                        {
                            if (taskId.HasValue)
                            {
                                FileTaskDAL.GetInstance().UpdateTaskProcessState3(taskId.Value, true, false, 0, "success",
                                                                                  (AppraiseResult)item.data.result, item.data.confidence, item.data.hot_score, item.data.normal_score, item.data.porn_score);

                                buf.AppendLine("task:" + taskId.Value + " code:" + item.code);
                            }
                            else
                            {
                                FileTaskDAL.GetInstance().UpdateTaskProcessState3(taskMd5, true, false, 0, "success",
                                                                                  (AppraiseResult)item.data.result, item.data.confidence, item.data.hot_score, item.data.normal_score, item.data.porn_score);

                                buf.AppendLine("task:" + taskMd5 + " code:" + item.code);
                            }
                        }
                        else//识别错误
                        {
                            if (taskId.HasValue)
                            {
                                //错误有定义
                                if (_errorCode.ContainsKey(item.code))
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskId.Value, false, false, item.code, _errorCode[item.code]);
                                }
                                else//错误未定义
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskId.Value, false, false, -1, "不能识别的错误");
                                }

                                buf.AppendLine("task:" + taskId.Value + " code:" + item.code);
                            }
                            else
                            {
                                //错误有定义
                                if (_errorCode.ContainsKey(item.code))
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskMd5, false, false, item.code, _errorCode[item.code]);
                                }
                                else//错误未定义
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskMd5, false, false, -1, "不能识别的错误");
                                }

                                buf.AppendLine("task:" + taskMd5 + " code:" + item.code);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //将产生异常的ID记录下来

                        if (taskId.HasValue)
                        {
                            exceptionTaskids.Add(taskId.Value);
                            buf.AppendLine("task exception:" + taskId.Value + " code:" + item.code);
                        }
                        if (!String.IsNullOrEmpty(taskMd5))
                        {
                            exceptionMd5s.Add(taskMd5);
                            buf.AppendLine("task exception:" + taskMd5 + " code:" + item.code);
                        }

                        FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                    }
                    finally
                    {
                        try
                        {
                            //使用完成后从缓存中移除ID映射
                            TaskIdMaps.RemoveId(item.url);
                        }
                        catch (Exception ex)
                        {
                            FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                        }
                    }
                }

                if (LogState.Enabled)
                {
                    FaceHand.Common.Core.WxLogProvider.Write(buf.ToString(), "TaskResult_Process");
                }

                //更新异常任务的状态
                try
                {
                    if (exceptionMd5s != null && exceptionMd5s.Count() > 0)
                    {
                        FileTaskDAL.GetInstance().UpdateTaskProcessState4(exceptionMd5s);
                    }
                    if (exceptionTaskids != null && exceptionTaskids.Count() > 0)
                    {
                        FileTaskDAL.GetInstance().UpdateTaskProcessState4(exceptionTaskids);
                    }
                }
                catch (Exception ex)
                {
                    FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                }
            });
        }
Exemplo n.º 2
0
        public static Task Process(IEnumerable <FileTaskInfo> tasks)
        {
            return(Task.Factory.StartNew(() => {
                if (tasks == null || tasks.Count() == 0)
                {
                    return;
                }

                try
                {
                    var urls = new List <string>();
                    var taskIds = new List <long>();
                    foreach (var task in tasks)
                    {
                        if (String.IsNullOrEmpty(task.FileFullPath) || task.IsProcessed)
                        {
                            continue;
                        }

                        if (!System.IO.File.Exists(task.FileFullPath))
                        {
                            //文件已不存在,删除任务
                            FileTask.RemoveFileTask(task.Id);
                            continue;
                        }

                        var url = replaceFileUrl(task.FileFullPath);
                        TaskIdMaps.Set(url, task.Id);//将ID映射记录下来

                        urls.Add(url);
                        taskIds.Add(task.Id);
                    }

                    //再次确认哪些URL要处理
                    if (urls.Count == 0)
                    {
                        return;
                    }

                    var reqdata = new RequestBody()
                    {
                        appid = CLOUD_appid, bucket = CLOUD_bucket, url_list = urls
                    };
                    var str_reqdata = Newtonsoft.Json.JsonConvert.SerializeObject(reqdata);
                    var byt_reqdata = Encoding.UTF8.GetBytes(str_reqdata);
                    var sign_reqdata = GetSign();

                    var client = (HttpWebRequest)WebRequest.Create(CLOUD_url);
                    client.Headers.Add("Authorization", sign_reqdata);
                    client.ContentType = "application/json";
                    client.ContentLength = byt_reqdata.Length;
                    client.Timeout = 20000;//20s
                    client.Method = "POST";
                    //写数据到请求
                    using (var reqStream = client.GetRequestStream())
                    {
                        reqStream.Write(byt_reqdata, 0, byt_reqdata.Length);
                    }

                    //发送请求前记录请求日志
                    if (LogState.Enabled)
                    {
                        string log = String.Format("{0} {1} {2}\n{3}", DateTime.Now, tasks.Count(), sign_reqdata, str_reqdata);
                        FaceHand.Common.Core.WxLogProvider.Write(log, "SexyFilter_Process");
                    }

                    //发送请求
                    try
                    {
                        HttpWebResponse resp = client.GetResponse() as HttpWebResponse;
                        using (var respStream = resp.GetResponseStream())
                        {
                            var sr = new System.IO.StreamReader(respStream, Encoding.UTF8);
                            var resp_data = Newtonsoft.Json.JsonConvert.DeserializeObject <ResponseBody>(sr.ReadToEnd());

                            //处理结果
                            TaskResult.Process(resp_data.result_list);
                        }
                    }
                    catch (WebException ex)
                    {
                        HttpWebResponse resp = ex.Response as HttpWebResponse;

                        var buf = new StringBuilder();
                        buf.AppendLine("Service Interface Call Exception " + ((int)resp.StatusCode).ToString() + " " + resp.StatusDescription);
                        buf.AppendLine("Url:" + CLOUD_url + " Sign:" + sign_reqdata);


                        //在次检测文件的有效性,有问题的直接删除任务
                        foreach (var task in tasks)
                        {
                            if (!System.IO.File.Exists(task.FileFullPath))
                            {
                                FileTask.RemoveFileTask(task.Id);
                            }

                            buf.AppendLine(String.Format("{0} {1}", task.Id, task.FileFullPath));
                        }

                        //再次更新为可处理的状态
                        FileTaskDAL.GetInstance().UpdateTaskProcessState4(taskIds);

                        if (LogState.Enabled)
                        {
                            FaceHand.Common.Core.WxLogProvider.Write(buf.ToString(), "SexyFilter_Process");
                        }
                    }
                }
                catch (Exception ex)
                {
                    FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                }
            }));
        }