361 lines
14 KiB
C#
361 lines
14 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Linq;
|
||
using System.ServiceProcess;
|
||
using System.Text;
|
||
using System.IO;
|
||
using System.Timers;
|
||
//using DeiNiu.Utils;
|
||
|
||
namespace WindowsServiceOut
|
||
{
|
||
public partial class Service1 : ServiceBase
|
||
{//记录到event log中,地址是 C:\Windows\System32\winevt\Logs (双击查看即可,文件名为MyNewLog)
|
||
|
||
private static string logdir = "d:\\wmsJobLog";
|
||
private static EventLog eventLog1;
|
||
private static int eventId = 1;
|
||
private Timer timer1 = new Timer(), timer2 = new Timer(), timer3 = new Timer(),
|
||
timerNewWorld = new Timer(), timer6 = new Timer();
|
||
private int intervalOut = 1, intervalIn = 2, intervalDoc = 3, intervalStockLack =180;
|
||
private WindowsServiceOut.ServiceReferenceScheduledService.enumOutStockRequestStatus
|
||
outStatus = WindowsServiceOut.ServiceReferenceScheduledService.enumOutStockRequestStatus.准备分拣;
|
||
public Service1()
|
||
{
|
||
InitializeComponent();
|
||
|
||
eventLog1 = new System.Diagnostics.EventLog();
|
||
if (!System.Diagnostics.EventLog.SourceExists("ServiceOutRequestSyncSource"))
|
||
{
|
||
System.Diagnostics.EventLog.CreateEventSource(
|
||
"ServiceOutRequestSyncSource", "ServiceOutRequestSyncLog");
|
||
}
|
||
eventLog1.Source = "ServiceOutRequestSyncSource";
|
||
eventLog1.Log = "ServiceOutRequestSyncLog";
|
||
}
|
||
|
||
void initialIntervals()
|
||
{
|
||
using (ServiceReferenceScheduledService.ScheduledServiceClient client = new ServiceReferenceScheduledService.ScheduledServiceClient())
|
||
{
|
||
try
|
||
{
|
||
int[] ins = client.getJobInterVal();
|
||
intervalOut = ins[0] > 0 ? ins[0] * 60000 : intervalOut;
|
||
intervalIn = ins[1] > 0 ? ins[1] * 60000 : intervalIn;
|
||
intervalDoc = ins[2] > 0 ? ins[2] * 60000 : intervalDoc;
|
||
intervalStockLack = ins[3] > 0 ? ins[3] * 60000 : intervalStockLack;
|
||
|
||
log(string.Format("initialIntervals intervalOut:{0} intervalIn:{1} intervalDoc:{2} intervalStockLack:{3}", intervalOut/60000, intervalIn / 60000, intervalDoc / 60000, intervalStockLack / 60000));
|
||
|
||
/* if(interval1>0 & timer1.Interval!= interval1 )
|
||
{
|
||
log(" new interal for out tasks . old is" + timer1.Interval / 60000 + ",...............new interval = " + interval1);
|
||
timer1.Stop();
|
||
timer1.Interval = interval1; ;
|
||
timer1.Start();
|
||
}
|
||
|
||
if (interval2 > 0 & timer2.Interval != interval2)
|
||
{
|
||
log(" new interal for stock in tasks . old is" + timer2.Interval / 60000 + ",...............new interval = " + interval2);
|
||
timer2.Stop();
|
||
timer2.Interval = interval2; ;
|
||
timer2.Start();
|
||
}
|
||
if (intervalDoc > 0 & timer3.Interval != intervalDoc)
|
||
{
|
||
log(" new interal for dictionary tasks . old is" + timer3.Interval / 60000 + ",...............new interval = " + intervalDoc);
|
||
timer3.Stop();
|
||
timer3.Interval = intervalDoc; ;
|
||
timer3.Start();
|
||
}
|
||
|
||
|
||
if (intervalStockLack > 0 & timer4.Interval != intervalStockLack)
|
||
{
|
||
log(" new interal for stock enumOutStockRequestStatus tasks . old is" + timer4.Interval / 60000 + ",...............new interval = " + intervalStockLack/60000);
|
||
timer4.Stop();
|
||
timer4.Interval = intervalStockLack; ;
|
||
timer4.Start();
|
||
|
||
}
|
||
*/
|
||
|
||
|
||
|
||
}
|
||
catch (Exception er)
|
||
{
|
||
log("initialIntervals exception:" + er.Message + "/n" + er.StackTrace);
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
|
||
protected override void OnStart(string[] args)
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(logdir))
|
||
{
|
||
Directory.CreateDirectory(logdir);
|
||
}
|
||
|
||
|
||
// eventLog1.WriteEntry("In OnStart.");
|
||
log("In OnStart.");
|
||
initialIntervals();
|
||
// Set up a timer that triggers every minute. 设置定时器
|
||
|
||
timer1.Interval = intervalOut ; // 1分执行一次
|
||
timer1.Elapsed += new ElapsedEventHandler(this.OnTimer_1);
|
||
timer1.Start();
|
||
|
||
// timer2 = new Timer();
|
||
timer2.Interval = intervalIn ; // 2分执行一次
|
||
timer2.Elapsed += new ElapsedEventHandler(this.OnTimer_2);
|
||
timer2.Start();
|
||
|
||
// timer3 = new Timer();
|
||
timer3.Interval = intervalDoc ; // 3分钟
|
||
timer3.Elapsed += new ElapsedEventHandler(this.OnTimer_3);
|
||
timer3.Start();
|
||
|
||
#if DEBUG
|
||
timerNewWorld.Interval = 18 * 60 * 60000; // 18 小时
|
||
timerNewWorld.Elapsed += new ElapsedEventHandler(this.OnTimer_NewWorld);
|
||
timerNewWorld.Start();
|
||
#endif
|
||
// timer4.Interval = intervalStockLack * 60000; // 3 小时
|
||
/* timer4.Stop();
|
||
timer4.Elapsed += new ElapsedEventHandler(this.OnTimer_4);
|
||
timer4.Start();
|
||
*/
|
||
timer6.Interval = 0.5 * 60000; // 30秒检查一次wave interval
|
||
timer6.Elapsed += new ElapsedEventHandler(this.OnTimer_6);
|
||
timer6.Start();
|
||
}
|
||
catch (Exception er)
|
||
{
|
||
log(er.InnerException.ToString());
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 停止服务
|
||
/// </summary>
|
||
protected override void OnStop()
|
||
{
|
||
// eventLog1.WriteEntry("In OnStop.");
|
||
log("In OnStop.");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 继续服务
|
||
/// </summary>
|
||
protected override void OnContinue()
|
||
{
|
||
//eventLog1.WriteEntry("In OnContinue.");
|
||
log("In OnContinue.");
|
||
}
|
||
|
||
|
||
int normalPrcocessCnt = 0;
|
||
/// <summary>
|
||
/// 出库相关任务处理
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="args"></param>
|
||
public void OnTimer_1(object sender, ElapsedEventArgs args)
|
||
{
|
||
|
||
// TODO: Insert monitoring activities here.
|
||
// eventLog1.WriteEntry("OnTimer_10", EventLogEntryType.Information, eventId++);
|
||
log("OnTimer_1 start");
|
||
timer1.Stop();
|
||
|
||
if (normalPrcocessCnt > intervalStockLack/intervalOut)
|
||
{
|
||
outStatus = WindowsServiceOut.ServiceReferenceScheduledService.enumOutStockRequestStatus.库存不足;
|
||
normalPrcocessCnt = 0;
|
||
|
||
}
|
||
log("outStatus: "+ outStatus);
|
||
normalPrcocessCnt++;
|
||
using (ServiceReferenceScheduledService.ScheduledServiceClient client = new ServiceReferenceScheduledService.ScheduledServiceClient())
|
||
{
|
||
try
|
||
{
|
||
|
||
log("start sync and create pickoutrequest ,status = " +outStatus);
|
||
client.syncAndCreatePickRequest(outStatus);
|
||
log("start createPDAPickTasks ");
|
||
client.createPDAPickTasks();
|
||
log("start createPickWaves ");
|
||
client.createPickWaves();
|
||
|
||
}
|
||
catch (Exception er)
|
||
{
|
||
log("OnTimer_1 exception:" +er.Message +"/n"+ er.StackTrace );
|
||
}
|
||
outStatus = WindowsServiceOut.ServiceReferenceScheduledService.enumOutStockRequestStatus.准备分拣;
|
||
log("OnTimer_1 end ");
|
||
}
|
||
|
||
if (intervalOut > 0 & timer1.Interval != intervalOut)
|
||
{
|
||
log(" new interal for out tasks . old is" + timer1.Interval / 60000 + ",...............new interval = " + intervalOut / 60000);
|
||
|
||
timer1.Interval = intervalOut; ;
|
||
|
||
}
|
||
timer1.Start();
|
||
|
||
}
|
||
/// <summary>
|
||
/// 入库相关的任务
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="args"></param>
|
||
public void OnTimer_2(object sender, ElapsedEventArgs args)
|
||
{
|
||
// TODO: Insert monitoring activities here.
|
||
// eventLog1.WriteEntry("OnTimer_10", EventLogEntryType.Information, eventId++);
|
||
log("OnTimer_2 begin");
|
||
timer2.Stop();
|
||
using (ServiceReferenceScheduledService.ScheduledServiceClient client = new ServiceReferenceScheduledService.ScheduledServiceClient())
|
||
{
|
||
log("start sync in requests ");
|
||
client.syncInRequest();
|
||
log("end sync in requests ");
|
||
|
||
}
|
||
if (intervalIn > 0 & timer2.Interval != intervalIn)
|
||
{
|
||
log(" new interal for stock in tasks . old is" + timer2.Interval / 60000 + ",...............new interval = " + intervalIn / 60000);
|
||
|
||
timer2.Interval = intervalIn; ;
|
||
|
||
}
|
||
timer2.Start();
|
||
log("OnTimer_2 end");
|
||
}
|
||
/// <summary>
|
||
/// 基本资料同步
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="args"></param>
|
||
public void OnTimer_3(object sender, ElapsedEventArgs args)
|
||
{
|
||
// TODO: Insert monitoring activities here.
|
||
//eventLog1.WriteEntry("OnTimer_60", EventLogEntryType.Information, eventId++);
|
||
log("OnTimer_3 begin");
|
||
timer3.Stop();
|
||
using (ServiceReferenceScheduledService.ScheduledServiceClient client = new ServiceReferenceScheduledService.ScheduledServiceClient())
|
||
{
|
||
try
|
||
{
|
||
log("start sync syncGoods ");
|
||
client.syncGoods();
|
||
log("end sync syncGoods ");
|
||
log("start sync syncNewOwners ");
|
||
client.syncNewOwners();
|
||
log("end sync syncNewOwners ");
|
||
log("start syncCustomer ");
|
||
client.syncCustomer();
|
||
log("end syncCustomer ");
|
||
}
|
||
catch (Exception er)
|
||
{
|
||
log("OnTimer_3 exception:" + er.Message + "/n" + er.StackTrace);
|
||
}
|
||
}
|
||
if (intervalDoc > 0 & timer3.Interval != intervalDoc)
|
||
{
|
||
log(" new interal for dictionary tasks . old is" + timer3.Interval / 60000 + ",...............new interval = " + intervalDoc / 60000);
|
||
|
||
timer3.Interval = intervalDoc; ;
|
||
|
||
}
|
||
|
||
|
||
timer3.Start();
|
||
log("OnTimer_3 end");
|
||
|
||
}
|
||
public void OnTimer_NewWorld(object sender, ElapsedEventArgs args)
|
||
{
|
||
log("OnTimer_NewWorld begin");
|
||
timerNewWorld.Stop();
|
||
using (ServiceReferenceScheduledService.ScheduledServiceClient client = new ServiceReferenceScheduledService.ScheduledServiceClient())
|
||
{
|
||
#if DEBUG
|
||
log("start truncate data..................to start new world! ");
|
||
// client.truncateDataForTestOnly();
|
||
log(" new world begin ..... enjoy! ");
|
||
#endif
|
||
}
|
||
timerNewWorld.Start();
|
||
log("OnTimer_NewWorld end");
|
||
|
||
}
|
||
|
||
public void OnTimer_6(object sender, ElapsedEventArgs args)
|
||
{
|
||
log("OnTimer_6 begin");
|
||
|
||
timer6.Stop();
|
||
initialIntervals();
|
||
|
||
/*
|
||
using (ServiceReferenceScheduledService.ScheduledServiceClient client = new ServiceReferenceScheduledService.ScheduledServiceClient())
|
||
{
|
||
int interval = client.getWaveInterval();
|
||
if (interval > 0 && interval * 60000 != timer1.Interval)
|
||
{
|
||
log(" new interal for waves . old is"+ timer1.Interval/60000 +",...............new interval = " + interval);
|
||
timer1.Stop();
|
||
timer1.Interval = interval * 60000; // 1分执行一次
|
||
timer1.Start();
|
||
|
||
log(" timer1 restarted ");
|
||
}
|
||
|
||
}*/
|
||
timer6.Start();
|
||
|
||
log("OnTimer_6 end");
|
||
}
|
||
/// <summary>
|
||
/// 记录到指定路径:D:\log.txt
|
||
/// </summary>
|
||
/// <param name="message"></param>
|
||
private static void log(string message)
|
||
{//LogHelper.debug(typeof(Service1), message);
|
||
try
|
||
{
|
||
eventLog1.WriteEntry(message, EventLogEntryType.Information, eventId++);
|
||
|
||
string logName = string.Format("{0}-{1}-{2}-{3}.txt", logdir + "\\jobLog",
|
||
DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day);
|
||
using (FileStream stream = new FileStream(logName, FileMode.Append))
|
||
using (StreamWriter writer = new StreamWriter(stream))
|
||
{
|
||
writer.WriteLine(String.Format("{0}:{1}", DateTime.Now, message));
|
||
}
|
||
}
|
||
catch (Exception er)
|
||
{
|
||
eventLog1.WriteEntry(er.Message, EventLogEntryType.Information, eventId++);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}
|