Exemple #1
0
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                if (!isRun)
                {
                    isRun = true;
                    object objParam = context.JobDetail.JobDataMap.Get("TaskParam");
                    if (objParam != null)
                    {
                        ProxyParam Param = JsonConvert.DeserializeObject <ProxyParam>(objParam.ToString());
                        DateTime   start = DateTime.Now;
                        LogHelper.WriteLog("\r\n\r\n\r\n\r\n------------------爬虫开始执行获取代理ip任务 " + start.ToString("yyyy-MM-dd HH:mm:ss") + " BEGIN-----------------------------\r\n\r\n");


                        //每执行10次任务,换一个代理IP
                        if (NeedChangeIP || ExecuteCount % Speed == 0)
                        {
                            if (NeedChangeIP)
                            {
                                ExecuteCount = (ExecuteCount / Speed + 1) * Speed;
                            }
                            LogHelper.WriteLog("\r\n\r\n\r\n\r\n------------------开始解析使用的代理ip " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " BEGIN-----------------------------\r\n\r\n");
                            ProxyIp = IpProxyGet.GetCorrectIP(Param);
                            LogHelper.WriteLog("------------------保存使用的代理ip:" + ProxyIp + " -----------------------------");
                            SQLHelper.ExecuteNonQuery("INSERT INTO dbo.p_ProxyIPUseHistory(ProxyIP,Type) VALUES (@ProxyIP,'IpProxyJob')", new { ProxyIP = ProxyIp });
                            NeedChangeIP = false;
                        }
                        Param.ProxyIp = ProxyIp;
                        LogHelper.WriteLog("\r\n\r\n\r\n\r\n------------------任务使用的代理ip:" + Param.ProxyIp + "----------------------------\r\n\r\n");

                        List <IPProxy> list = IpProxyGet.ParseProxy(Param);
                        if (list.Count == 0)
                        {
                            //没有返回数据.表示当前IP已经被锁定需要更换
                            NeedChangeIP = true;
                        }

                        DateTime end = DateTime.Now;
                        ExecuteCount++;
                        LogHelper.WriteLog("\r\n\r\n------------------爬虫完成获取代理ip任务:" + end.ToString("yyyy-MM-dd HH:mm:ss") + ",本次共耗时(分):" + (end - start).TotalMinutes + " END------------------------\r\n\r\n\r\n\r\n");
                    }
                    isRun = false;
                }
            }
            catch (Exception ex)
            {
                JobExecutionException e2 = new JobExecutionException(ex);
                LogHelper.WriteLog("爬虫获取代理ip任务异常", ex);
                isRun = false;
                ExecuteCount++;
                //1.立即重新执行任务
                e2.RefireImmediately = true;
                //2 立即停止所有相关这个任务的触发器
                //e2.UnscheduleAllTriggers=true;
            }
        }
Exemple #2
0
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                DateTime start = DateTime.Now;
                TaskLog.IpProxyLogInfo.WriteLogE("\r\n\r\n\r\n\r\n------------------爬虫开始执行获取代理ip任务 " + start.ToString("yyyy-MM-dd HH:mm:ss") + " BEGIN-----------------------------\r\n\r\n");


                //每执行10次任务,换一个代理IP
                if (NeedChangeIP || ExecuteCount % IpProxyConfig.Speed == 0)
                {
                    if (NeedChangeIP)
                    {
                        ExecuteCount = (ExecuteCount / IpProxyConfig.Speed + 1) * IpProxyConfig.Speed;
                    }
                    TaskLog.IpProxyLogInfo.WriteLogE("\r\n\r\n\r\n\r\n------------------开始解析使用的代理ip " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " BEGIN-----------------------------\r\n\r\n");
                    ProxyIp = IpProxyGet.GetCorrectIP();
                    TaskLog.IpProxyLogInfo.WriteLogE("------------------保存使用的代理ip:" + ProxyIp + " -----------------------------");
                    SQLHelper.ExecuteNonQuery("INSERT INTO dbo.p_ProxyIPUseHistory(ProxyIP,Type) VALUES (@ProxyIP,'IpProxyJob')", new { ProxyIP = ProxyIp });
                    NeedChangeIP = false;
                }
                TaskLog.IpProxyLogInfo.WriteLogE("\r\n\r\n\r\n\r\n------------------任务使用的代理ip:" + ProxyIp + "----------------------------\r\n\r\n");

                List <IPProxy> list = IpProxyGet.ParseProxy(ProxyIp);
                if (list.Count == 0)
                {
                    //没有返回数据.表示当前IP已经被锁定需要更换
                    NeedChangeIP = true;
                }

                DateTime end = DateTime.Now;
                ExecuteCount++;
                TaskLog.IpProxyLogInfo.WriteLogE("\r\n\r\n------------------爬虫完成获取代理ip任务:" + end.ToString("yyyy-MM-dd HH:mm:ss") + ",本次共耗时(分):" + (end - start).TotalMinutes + " END------------------------\r\n\r\n\r\n\r\n");
            }
            catch (Exception ex)
            {
                JobExecutionException e2 = new JobExecutionException(ex);
                TaskLog.IpProxyLogError.WriteLogE("爬虫获取代理ip任务异常", ex);
                ExecuteCount++;
                //1.立即重新执行任务
                e2.RefireImmediately = true;
                //2 立即停止所有相关这个任务的触发器
                //e2.UnscheduleAllTriggers=true;
            }
        }