我创建了一个抓取工具,它从我之前为其定义的其他网站获取新闻,因此我使用quartz
在后台运行任务。时间表定义如下:
public class JobBackground : IJob
{
public void Execute(IJobExecutionContext context)
{
for (int j = 1; j <= 920; j++)
{
NewsRepository newsRepository = new NewsRepository();
GoyaAgent Goyaagent = new GoyaAgent();
Task<List<NewsContent>> lst = Goyaagent.parsing("http://www.gooyait.com/page/"+j);
List<NewsContent> enresult = lst.Result;
foreach (NewsContent newsContent in enresult)
{
News newnews = new News();
newnews.Subject = newsContent.Title;
newnews.NewsDate = DateTime.Now;
newnews.NewsBrief = newsContent.abs;
newnews.NewsText = newsContent.Content;
newnews.ShowOnSlide = "Yes";
newnews.GroupId = 1049;
newnews.NewsImageSmall = newsContent.Img;
newnews.NewsImageBig = newsContent.Img;
newnews.Reference = newsContent.Url;
newnews.UserId = "3";
newnews.Visible = "Yes";
newnews.ViewCounter = 0;
newsRepository.Add(newnews);
if (newsRepository.FindBy(i => i.Reference == newsContent.Url).Count() == 0)
newsRepository.Save();
}
}
}
}
parsing
功能:
public async Task<List<NewsContent>> parsing(string newsArchive)
{
List<NewsContent> lstResult = new List<NewsContent>();
try
{
HttpClient http = new HttpClient();
var response = await http.GetByteArrayAsync(newsArchive);
String source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
source = WebUtility.HtmlDecode(source);
HtmlDocument resultat = new HtmlDocument();
resultat.LoadHtml(source);
List<HtmlNode> toftitle = resultat.DocumentNode.Descendants().Where
(x =>
(x.Name == "div" && x.Attributes["class"] != null &&
x.Attributes["class"].Value.Contains("main-col"))).ToList();
var li = toftitle[0].Descendants().Where
(x =>
(x.Name == "div" && x.Attributes["class"] != null &&
x.Attributes["class"].Value.Contains("base-box blog-post"))).ToList();
foreach (var item in li)
{
NewsContent newsContent = new NewsContent();
newsContent.Url = item.Descendants("a").ToList()[0].GetAttributeValue("href", null);
newsContent.Img = item.Descendants("img").ToList()[0].GetAttributeValue("src", null);
newsContent.Title = item.Descendants("h2").ToList()[0].InnerText;
newsContent.abs = item.Descendants("p").ToList()[0].InnerText;
//finding main news content
var response1 = await http.GetByteArrayAsync(newsContent.Url);
String source1 = Encoding.GetEncoding("utf-8").GetString(response1, 0, response1.Length - 1);
source1 = WebUtility.HtmlDecode(source1);
HtmlDocument resultat1 = new HtmlDocument();
resultat1.LoadHtml(source1);
HtmlNode doc = resultat1.DocumentNode.SelectSingleNode("//div[@class='entry-content']");
HtmlNode node = doc.SelectSingleNode("//div[@class='yasr-visitor-votes']");
if (node != null)
node.ParentNode.RemoveChild(node);
HtmlNode node1 = doc.SelectSingleNode("//div[@class='post-tags']");
if (node1 != null)
node1.ParentNode.RemoveChild(node1);
HtmlNode node2 =
doc.SelectSingleNode("//div[@class='mom-social-share ss-horizontal border-box']");
if (node2 != null)
node2.ParentNode.RemoveChild(node2);
HtmlNode node3 = doc.SelectSingleNode("//script|//style");
if (node3 != null)
node3.ParentNode.RemoveChild(node3);
newsContent.Content = doc.InnerHtml;
lstResult.Add(newsContent);
}
}
catch (Exception e)
{
}
return lstResult;
}
时间表以此代码开头:
public class JobScheduler
{
public static void Start()
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
IJobDetail job = JobBuilder.Create<JobBackground>().Build();
ITrigger trigger = TriggerBuilder.Create().StartNow()
.WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInHours(24)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
)
.Build();
scheduler.ScheduleJob(job, trigger);
}
}
有几个问题:
当我运行此代码时,由于ITrigger trigger = TriggerBuilder.Create().StartNow()
代码从未执行过,所以我必须等待24小时才能看到结果?
第二个问题,当我将代码上传到网络服务器时,它不起作用。为什么?
答案 0 :(得分:1)
尝试更新您的方法,如下所示:
public class JobScheduler
{
public static void Start()
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
IJobDetail job = JobBuilder.Create<JobBackground>().Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSchedule(CronScheduleBuilder
.DailyAtHourAndMinute(0,0)
.WithMisfireHandlingInstructionFireAndProceed() //MISFIRE_INSTRUCTION_FIRE_NOW
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("GTB Standard Time")) //(GMT+02:00)
//https://alexandrebrisebois.wordpress.com/2013/01/20/using-quartz-net-to-schedule-jobs-in-windows-azure-worker-roles/
)
.Build();
scheduler.ScheduleJob(job, trigger);
}
}
关于第二个问题,问题与IIS
而不是调度程序Quartz.NET
,Hangfire
等有关。另一方面,有很多解决方法发布在网络,但只有其中一些正在运作。在我看来,没有必要应用大量的配置设置。只需在您发布应用程序并享受的服务器上安装Keep Alive Service For IIS 6.0/7.5即可。然后,在应用程序池回收,IIS /应用程序重新启动等之后,您发布的应用程序将处于活动状态。希望这有助于...
更新 这是我在IIS上使用了几个月的完整工作代码没有任何问题。另一方面,对于基于IIS的触发问题,请查看我在Quartz.net scheduler doesn't fire jobs/triggers once deployed上的答案。
的Global.asax:
protected void Application_Start()
{
JobScheduler.Start();
}
EmailJob.cs:
using Quartz;
public class EmailJob : IJob
{
public void Execute(IJobExecutionContext context)
{
SendEmail();
}
}
JobScheduler.cs:
using Quartz;
using Quartz.Impl;
public class JobScheduler
{
public static void Start()
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
IJobDetail job = JobBuilder.Create<EmailJob>().Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
//.StartAt(new DateTime(2015, 12, 21, 17, 19, 0, 0))
.StartNow()
.WithSchedule(CronScheduleBuilder
.WeeklyOnDayAndHourAndMinute(DayOfWeek.Monday, 10, 00)
//.WithMisfireHandlingInstructionDoNothing() //Do not fire if the firing is missed
.WithMisfireHandlingInstructionFireAndProceed() //MISFIRE_INSTRUCTION_FIRE_NOW
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("GTB Standard Time")) //(GMT+02:00)
)
.Build();
scheduler.ScheduleJob(job, trigger);
}
}