|
|
|
@ -35,7 +35,7 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 最大队列长度
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int MaxOperatingQueueSize = 5;
|
|
|
|
|
public int MaxOperatingQueueSize = 4;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 同时上传的数量
|
|
|
|
@ -58,8 +58,6 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
throw new System.ArgumentNullException();
|
|
|
|
|
this.mTaskIdentify = ID;
|
|
|
|
|
this.mOperator = op;
|
|
|
|
|
//sLog = LoggerFactory.ForContext("Uploader:" + this.mTaskIdentify);
|
|
|
|
|
// 入口
|
|
|
|
|
mDatasWorkerThread = new System.Threading.Thread(uploadWorker)
|
|
|
|
|
{
|
|
|
|
|
Name = "upload worker",
|
|
|
|
@ -67,7 +65,6 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
};
|
|
|
|
|
mDatasWorkerThread.Start();
|
|
|
|
|
|
|
|
|
|
// Swap 处理: 超过4s保存至数据库 Caches 表
|
|
|
|
|
mSwapClearTimer = new Timer((x) =>
|
|
|
|
|
{
|
|
|
|
|
var toDBs = new List<UploadModel<T>>();
|
|
|
|
@ -160,28 +157,35 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
|
|
|
|
|
if (reqModel == null)
|
|
|
|
|
{
|
|
|
|
|
//sLog.Formf($"no request found:{request.RequestData}");
|
|
|
|
|
Console.WriteLine("no request found:{request.RequestData}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
reqModel.ErrorInfo = reason;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//sLog.Formf($"{reqModel.Request} finished:{success}");
|
|
|
|
|
string cancelRetryReason = null;
|
|
|
|
|
if (success)
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
db.UploadFinishs.Add(new UploadFinish
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
CreateAt = reqModel.CreateAt,
|
|
|
|
|
RetryAt = reqModel.RetryAt,
|
|
|
|
|
RetryCount = reqModel.RetryCount,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(reqModel.Request),
|
|
|
|
|
UploaderID = mTaskIdentify
|
|
|
|
|
});
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
|
|
|
|
|
db.UploadFinishs.Add(new UploadFinish
|
|
|
|
|
{
|
|
|
|
|
CreateAt = reqModel.CreateAt,
|
|
|
|
|
RetryAt = reqModel.RetryAt,
|
|
|
|
|
RetryCount = reqModel.RetryCount,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(reqModel.Request),
|
|
|
|
|
Tag = reqModel.Tag,
|
|
|
|
|
UploaderID = mTaskIdentify
|
|
|
|
|
});
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (mOperator.ShouldRetryRequest(reqModel, out cancelRetryReason))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
@ -196,23 +200,29 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
db.UploadCancels.Add(new UploadCancel
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
CreateAt = reqModel.CreateAt,
|
|
|
|
|
RetryAt = reqModel.RetryAt,
|
|
|
|
|
RetryCount = reqModel.RetryCount,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(reqModel.Request),
|
|
|
|
|
ErrorInfo = "Task cancel Retry:" + cancelRetryReason ?? "No Reason",
|
|
|
|
|
UploaderID = mTaskIdentify
|
|
|
|
|
});;
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
db.UploadCancels.Add(new UploadCancel
|
|
|
|
|
{
|
|
|
|
|
CreateAt = reqModel.CreateAt,
|
|
|
|
|
RetryAt = reqModel.RetryAt,
|
|
|
|
|
RetryCount = reqModel.RetryCount,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(reqModel.Request),
|
|
|
|
|
ErrorInfo = "Task cancel Retry:" + cancelRetryReason ?? "No Reason",
|
|
|
|
|
Tag = reqModel.Tag,
|
|
|
|
|
UploaderID = mTaskIdentify
|
|
|
|
|
});
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"{DateTime.UtcNow.ToString("mm:ss.fff")} task {x} result:{success}");
|
|
|
|
|
//throw new NotImplementedException();
|
|
|
|
|
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} task {x} result:{success}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -228,7 +238,7 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
if (reqCount >= MaxCocurrentTaskCount)
|
|
|
|
|
{
|
|
|
|
|
System.Threading.Thread.Sleep(10);
|
|
|
|
|
Console.WriteLine($"{DateTime.UtcNow.ToString("mm:ss.fff")} task exceed {MaxCocurrentTaskCount}, waiting");
|
|
|
|
|
Console.WriteLine($@"{DateTime.Now.ToString("HH:mm:ss.fff")} task exceed {MaxCocurrentTaskCount}, waiting");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
int newRequestCount = MaxCocurrentTaskCount - reqCount;
|
|
|
|
@ -243,18 +253,26 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
string donotStartReason;
|
|
|
|
|
if (!mOperator.ShouldStartRequest(d, out donotStartReason))
|
|
|
|
|
{
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
db.UploadCancels.Add(new UploadCancel
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
CreateAt = d.CreateAt,
|
|
|
|
|
RetryAt = d.RetryAt,
|
|
|
|
|
RetryCount = d.RetryCount,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(d.Request),
|
|
|
|
|
ErrorInfo = "Task cancel Start:" + donotStartReason ?? "No Reason",
|
|
|
|
|
UploaderID = mTaskIdentify
|
|
|
|
|
}); ;
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
db.UploadCancels.Add(new UploadCancel
|
|
|
|
|
{
|
|
|
|
|
CreateAt = d.CreateAt,
|
|
|
|
|
RetryAt = d.RetryAt,
|
|
|
|
|
RetryCount = d.RetryCount,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(d.Request),
|
|
|
|
|
ErrorInfo = "Task cancel Start:" + donotStartReason ?? "No Reason",
|
|
|
|
|
Tag = d.Tag,
|
|
|
|
|
UploaderID = mTaskIdentify
|
|
|
|
|
}); ;
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
@ -263,8 +281,6 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
var req = mOperator.UploadData(d.Request);
|
|
|
|
|
enqueRequest(req, d);
|
|
|
|
|
ThreadPool.QueueUserWorkItem(new WaitCallback((o) => req.Start()));
|
|
|
|
|
//new System.Threading.Thread(req.Start).Start();
|
|
|
|
|
//req.Start();
|
|
|
|
|
i++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
@ -290,7 +306,7 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
{
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
//sLog.Formf($@"cache requests,{datas.Count()}");
|
|
|
|
|
Console.WriteLine($@"cache requests,{datas.Count()}");
|
|
|
|
|
var caches = datas.Select((data) =>
|
|
|
|
|
new UploadCache
|
|
|
|
|
{
|
|
|
|
@ -307,9 +323,9 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
//sLog.FormErrorf(ex.Message);
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -319,22 +335,29 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
/// <param name="datas"></param>
|
|
|
|
|
private void cacheRequest(List<UploadModel<T>> datas)
|
|
|
|
|
{
|
|
|
|
|
//sLog.Formf($"cache requests count,{datas.Count()}");
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var caches = datas.Select((data) =>
|
|
|
|
|
new UploadCache
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
UploaderID = mTaskIdentify,
|
|
|
|
|
CreateAt = data.CreateAt,
|
|
|
|
|
RetryAt = data.RetryAt,
|
|
|
|
|
RetryCount = data.RetryCount,
|
|
|
|
|
ErrorInfo = data.ErrorInfo,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(data.Request)
|
|
|
|
|
var caches = datas.Select((data) =>
|
|
|
|
|
new UploadCache
|
|
|
|
|
{
|
|
|
|
|
UploaderID = mTaskIdentify,
|
|
|
|
|
CreateAt = data.CreateAt,
|
|
|
|
|
RetryAt = data.RetryAt,
|
|
|
|
|
RetryCount = data.RetryCount,
|
|
|
|
|
ErrorInfo = data.ErrorInfo,
|
|
|
|
|
Tag = data.Tag,
|
|
|
|
|
RequestData = mOperator.ConvertRequestToCachel(data.Request)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
db.UploadCaches.AddRange(caches);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
db.UploadCaches.AddRange(caches);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -355,32 +378,39 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
toGet--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
var caches = from c in db.UploadCaches
|
|
|
|
|
orderby c.RetryAt ascending
|
|
|
|
|
where c.UploaderID == this.mTaskIdentify
|
|
|
|
|
select c;
|
|
|
|
|
var x = caches.Take(toGet).ToList();
|
|
|
|
|
//从数据库中移除
|
|
|
|
|
db.UploadCaches.RemoveRange(x);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
var modelsFromDB = x.Select((data) =>
|
|
|
|
|
new UploadModel<T>
|
|
|
|
|
{
|
|
|
|
|
CreateAt = data.CreateAt,
|
|
|
|
|
RetryAt = data.RetryAt,
|
|
|
|
|
RetryCount = data.RetryCount,
|
|
|
|
|
ErrorInfo = data.ErrorInfo,
|
|
|
|
|
Request = mOperator.ConvertCacheToRequest(data.RequestData)
|
|
|
|
|
}
|
|
|
|
|
).ToList();
|
|
|
|
|
|
|
|
|
|
ret.AddRange(modelsFromDB);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var db = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
var caches = from c in db.UploadCaches
|
|
|
|
|
orderby c.RetryAt ascending
|
|
|
|
|
where c.UploaderID == this.mTaskIdentify
|
|
|
|
|
select c;
|
|
|
|
|
var x = caches.Take(toGet).ToList();
|
|
|
|
|
//从数据库中移除
|
|
|
|
|
db.UploadCaches.RemoveRange(x);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
var modelsFromDB = x.Select((data) =>
|
|
|
|
|
new UploadModel<T>
|
|
|
|
|
{
|
|
|
|
|
CreateAt = data.CreateAt,
|
|
|
|
|
RetryAt = data.RetryAt,
|
|
|
|
|
RetryCount = data.RetryCount,
|
|
|
|
|
ErrorInfo = data.ErrorInfo,
|
|
|
|
|
Tag = data.Tag,
|
|
|
|
|
Request = mOperator.ConvertCacheToRequest(data.RequestData)
|
|
|
|
|
}
|
|
|
|
|
).ToList();
|
|
|
|
|
|
|
|
|
|
ret.AddRange(modelsFromDB);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if (ret.Count() != 0)
|
|
|
|
|
//sLog.Formf($"get {number} cache return {ret.Count()}");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -391,7 +421,6 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
{
|
|
|
|
|
if (!Interlocked.Equals(mIsCanceled, 1))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var requestData = new UploadModel<T>
|
|
|
|
|
{
|
|
|
|
|
CreateAt = DateTime.Now,
|
|
|
|
@ -403,18 +432,18 @@ namespace Ksat.Supplyment.Library.Uploader
|
|
|
|
|
};
|
|
|
|
|
if (mDatas.Count >= MaxOperatingQueueSize)
|
|
|
|
|
{
|
|
|
|
|
//sLog.Formf($"add request to swap,{data}");
|
|
|
|
|
Console.WriteLine("add request to swap");
|
|
|
|
|
mSwapDatas.Enqueue(requestData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//sLog.Formf($"add request to queue,{data}");
|
|
|
|
|
Console.WriteLine("add request to queue");
|
|
|
|
|
mDatas.Enqueue(requestData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//sLog.Formf("task canceled");
|
|
|
|
|
Console.WriteLine("task canceled");
|
|
|
|
|
cacheRequest(new List<Tuple<T, string>>() { new Tuple<T, string>(data, tag) });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|