|
|
using DSWeb.Common.DB;
|
|
|
using DSWeb.Common.Helper;
|
|
|
using log4net;
|
|
|
using MailKit.Net.Smtp;
|
|
|
using MimeKit;
|
|
|
using Newtonsoft.Json;
|
|
|
using Quartz;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Configuration;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Web;
|
|
|
using FluentFTP;
|
|
|
|
|
|
namespace DSWeb.Job.Common
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// ftp上传任务
|
|
|
/// </summary>
|
|
|
public class FtpUploadJob : IJob
|
|
|
{
|
|
|
private CommonDataContext commonDataContext = new CommonDataContext();
|
|
|
private ILog logger = LogManager.GetLogger("FtpUploadJob");
|
|
|
|
|
|
private const int MaxRetry = 3;
|
|
|
|
|
|
public void Execute(IJobExecutionContext context)
|
|
|
{
|
|
|
var dtLastDoExp = DateTime.Now.AddMinutes(-2);
|
|
|
var backFtp = commonDataContext
|
|
|
.BackgroundTaskFtp
|
|
|
.OrderByDescending(t => t.CreateTime)
|
|
|
.FirstOrDefault(t => t.Status == BackgroundTaskFtp.StatusCreate
|
|
|
&& t.Type == BackgroundTaskFtp.TypeFtpUpload
|
|
|
&& (t.LastDo == null || t.LastDo < dtLastDoExp)
|
|
|
&& t.TryCount < MaxRetry);
|
|
|
|
|
|
if (backFtp != null)
|
|
|
{
|
|
|
backFtp.Status = BackgroundTaskFtp.StatusDoing;
|
|
|
backFtp.LastDo = DateTime.Now;
|
|
|
backFtp.TryCount++;
|
|
|
commonDataContext.SaveChanges();
|
|
|
|
|
|
string upFile = null;
|
|
|
string upFileName = null;
|
|
|
string upDir = null;
|
|
|
try
|
|
|
{
|
|
|
if (FtpTaskHelper.ParseTaskJson(backFtp.FtpData, out upDir, out upFile, out upFileName))
|
|
|
{
|
|
|
if (File.Exists(upFile))
|
|
|
{
|
|
|
var ftpHost = backFtp.FtpHost;
|
|
|
var ftpPort = 21;
|
|
|
if (ftpHost.Contains(":"))
|
|
|
{
|
|
|
var tmpArr = ftpHost.Split(':');
|
|
|
ftpHost = tmpArr[0];
|
|
|
ftpPort = Convert.ToInt32(tmpArr[1]);
|
|
|
}
|
|
|
|
|
|
FtpClient client = new FtpClient(ftpHost);
|
|
|
client.Port = ftpPort;
|
|
|
client.Credentials = new NetworkCredential(backFtp.FtpUser, backFtp.FtpPassword);
|
|
|
client.Connect();
|
|
|
|
|
|
var upDirAndFile = "";
|
|
|
if (!string.IsNullOrEmpty(upFileName))
|
|
|
{
|
|
|
upDirAndFile = upDir + "/" + upFileName;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
upDirAndFile = upDir + "/" + Path.GetFileName(upFile);
|
|
|
}
|
|
|
|
|
|
client.UploadFile(upFile, upDirAndFile);
|
|
|
client.Disconnect();
|
|
|
|
|
|
backFtp.Status = BackgroundTaskFtp.StatusSuccess;
|
|
|
commonDataContext.SaveChanges();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
logger.Error($"本地文件不存在,未能执行:ID:{backFtp.GID},文件:{upFile}");
|
|
|
backFtp.Status = BackgroundTaskFtp.StatusFail;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
logger.Error($"数据参数有误,未能执行:ID:{backFtp.GID},数据:{backFtp.FtpData}");
|
|
|
backFtp.Status = BackgroundTaskFtp.StatusFail;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
logger.Error($"执行时发生错误:ID:{backFtp.GID}");
|
|
|
var excep = ex;
|
|
|
while (excep != null)
|
|
|
{
|
|
|
logger.Error(excep.Message);
|
|
|
logger.Error(excep.StackTrace);
|
|
|
|
|
|
excep = excep.InnerException;
|
|
|
}
|
|
|
|
|
|
if (backFtp.TryCount < MaxRetry)
|
|
|
{
|
|
|
backFtp.Status = BackgroundTaskFtp.StatusCreate; //未达到最大次数,重试
|
|
|
|
|
|
DingTalkMessageHelper.SendGroupMessage($"FTP发送失败,当前还会重试{MaxRetry - backFtp.TryCount}次", $"FTP上传文件【{Path.GetFileName(upFile)}】失败,异常信息:{ex.Message}\r\n。服务器地址【{backFtp.FtpHost}】,账号【{backFtp.FtpUser}】,密码【{backFtp.FtpPassword}】,上传目录【{upDir}】", "FTP发送失败", ConfigurationManager.AppSettings["FtpUploadExceptNotify"]);
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
backFtp.Status = BackgroundTaskFtp.StatusFail;
|
|
|
|
|
|
DingTalkMessageHelper.SendGroupMessage("FTP发送失败,请立即处理", $"FTP上传文件【{Path.GetFileName(upFile)}】失败,异常信息:{ex.Message}\r\n。服务器地址【{backFtp.FtpHost}】,账号【{backFtp.FtpUser}】,密码【{backFtp.FtpPassword}】,上传目录【{upDir}】", "FTP发送失败", ConfigurationManager.AppSettings["FtpUploadExceptNotify"]);
|
|
|
|
|
|
}
|
|
|
|
|
|
backFtp.Remark = ex.Message;
|
|
|
}
|
|
|
commonDataContext.SaveChanges();
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
} |