You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB
C#

using DSWeb.Common.DB;
using DSWeb.Common.Helper;
using DSWeb.Common.Model;
using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSWeb.Job.Common
{
public class TaskHttpJob : IJob
{
private CommonDataContext commonData = new CommonDataContext();
private ILog logger = LogManager.GetLogger("TaskHttpJob");
public void Execute(IJobExecutionContext context)
{
var httpTaskList = commonData
.BackgroundTaskCommon
.Where(t => t.Status == BackgroundTaskCommon.StatusCreate && t.Type == BackgroundTaskCommon.TypeHttpFeedback)
.OrderByDescending(t => t.CreateTime)
.Take(10)
.ToList();
//修改状态,防止其他线程重复处理
httpTaskList.ForEach(t =>
{
t.Status = BackgroundTaskCommon.StatusDoing;
t.ExecuteCount++;
t.ExecuteTime = DateTime.Now;
});
commonData.SaveChanges();
foreach (var task in httpTaskList)
{
var paraObj = JsonConvert.DeserializeAnonymousType(task.ParamData, new
{
url = string.Empty,
method = string.Empty,
json = string.Empty
});
try
{
string rtnStr = string.Empty;
if (paraObj.method.ToUpper() == "POST")
{
rtnStr = WebRequestHelper.DoPost(paraObj.url, paraObj.json);
}
else
{
rtnStr = WebRequestHelper.DoGet(paraObj.url);
}
task.Status = BackgroundTaskCommon.StatusSuccess;
task.ResultData = rtnStr;
}
catch (Exception ex)
{
task.Status = BackgroundTaskCommon.StatusFail;
task.ResultData = ex.Message;
logger.Error(ex.Message);
logger.Error(ex.StackTrace);
}
commonData.SaveChanges();
}
}
}
}