2023-05-23 16:13:17 +08:00
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)
2023-09-04 22:41:19 +08:00
static log4net . ILog log = log4net . LogManager . GetLogger ( "jobSvr" ) ;
2023-05-23 16:13:17 +08:00
2023-09-04 22:41:19 +08:00
private static string logdir = "c:\\wmsJobLog" ;
2023-05-23 16:13:17 +08:00
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 ( ) ;
2023-11-21 19:18:23 +08:00
private int intervalOut = 1 * 60000 , intervalTest = 2 * 60000 , intervalDoc = 3 * 60000 , intervalStockLack = 3 * 60 * 60000 , intervalNoticeErp = 5 * 60 * 60000 ;
2023-05-23 16:13:17 +08:00
private WindowsServiceOut . ServiceReferenceScheduledService . enumOutStockRequestStatus
outStatus = WindowsServiceOut . ServiceReferenceScheduledService . enumOutStockRequestStatus . 准 备 分 拣 ;
public Service1 ( )
{
InitializeComponent ( ) ;
2023-09-04 22:41:19 +08:00
/ *
2023-05-23 16:13:17 +08:00
eventLog1 = new System . Diagnostics . EventLog ( ) ;
if ( ! System . Diagnostics . EventLog . SourceExists ( "ServiceOutRequestSyncSource" ) )
{
System . Diagnostics . EventLog . CreateEventSource (
"ServiceOutRequestSyncSource" , "ServiceOutRequestSyncLog" ) ;
}
eventLog1 . Source = "ServiceOutRequestSyncSource" ;
eventLog1 . Log = "ServiceOutRequestSyncLog" ;
2023-09-04 22:41:19 +08:00
* /
2023-05-23 16:13:17 +08:00
}
void initialIntervals ( )
{
2023-11-21 19:18:23 +08:00
debugLog ( " start to get job intervals...... " ) ;
2023-05-23 16:13:17 +08:00
using ( ServiceReferenceScheduledService . ScheduledServiceClient client = new ServiceReferenceScheduledService . ScheduledServiceClient ( ) )
{
2023-11-21 19:18:23 +08:00
debugLog ( " get job intervals as " ) ;
int [ ] ins = client . getJobInterVal ( ) ;
2023-05-23 16:13:17 +08:00
intervalOut = ins [ 0 ] > 0 ? ins [ 0 ] * 60000 : intervalOut ;
2023-09-04 22:41:19 +08:00
intervalTest = ins [ 1 ] > 0 ? ins [ 1 ] * 60000 : intervalTest ;
2023-05-23 16:13:17 +08:00
intervalDoc = ins [ 2 ] > 0 ? ins [ 2 ] * 60000 : intervalDoc ;
2023-11-21 19:18:23 +08:00
intervalStockLack = ins [ 3 ] > 0 ? ins [ 3 ] * 60000 : intervalStockLack ;
intervalNoticeErp = ins [ 4 ] > 0 ? ins [ 4 ] * 60000 : intervalNoticeErp ;
2023-05-23 16:13:17 +08:00
2023-11-21 19:18:23 +08:00
debugLog ( string . Format ( "initialIntervals intervalOut:{0} intervalTest:{1} intervalDoc:{2} intervalStockLack:{3}" , intervalOut / 60000 , intervalTest / 60000 , intervalDoc / 60000 , intervalStockLack / 60000 ) ) ;
2023-05-23 16:13:17 +08:00
2023-09-04 22:41:19 +08:00
if ( intervalOut > 0 & timer1 . Interval ! = intervalOut )
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
debugLog ( " new interal for out tasks . old is " + timer1 . Interval / 60000 + " ,...............new interval = " + intervalOut / 60000 ) ;
2023-05-23 16:13:17 +08:00
timer1 . Stop ( ) ;
2023-09-04 22:41:19 +08:00
timer1 . Interval = intervalOut ; ;
2023-05-23 16:13:17 +08:00
timer1 . Start ( ) ;
}
2023-11-21 19:18:23 +08:00
if ( intervalNoticeErp > 0 & timer2 . Interval ! = intervalNoticeErp )
{
debugLog ( " new interal for i NoticeErp tasks . old is " + timer2 . Interval / 60000 + " ,...............new interval = " + intervalNoticeErp / 60000 ) ;
timer2 . Stop ( ) ;
timer2 . Interval = intervalNoticeErp ;
timer2 . Start ( ) ;
}
2023-09-04 22:41:19 +08:00
#if DEBUG
2023-11-21 19:18:23 +08:00
if ( intervalTest > 0 & timerNewWorld . Interval ! = intervalTest )
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
debugLog ( " new interal for test tasks . old is " + timerNewWorld . Interval / 60000 + " ...............new interval = " + intervalTest / 60000 ) ;
timerNewWorld . Stop ( ) ;
timerNewWorld . Interval = intervalTest ; ;
timerNewWorld . Start ( ) ;
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
#endif
2023-05-23 16:13:17 +08:00
if ( intervalDoc > 0 & timer3 . Interval ! = intervalDoc )
{
2023-09-04 22:41:19 +08:00
debugLog ( " new interal for dictionary tasks . old is " + timer3 . Interval / 60000 + " ...............new interval = " + intervalDoc / 60000 ) ;
2023-05-23 16:13:17 +08:00
timer3 . Stop ( ) ;
timer3 . Interval = intervalDoc ; ;
timer3 . Start ( ) ;
}
2023-09-04 22:41:19 +08:00
/ *
2023-05-23 16:13:17 +08:00
if ( intervalStockLack > 0 & timer4 . Interval ! = intervalStockLack )
{
2023-09-04 22:41:19 +08:00
debugLog ( " new interal for stock enumOutStockRequestStatus tasks . old is" + timer4 . Interval / 60000 + ",...............new interval = " + intervalStockLack / 60000 ) ;
2023-05-23 16:13:17 +08:00
timer4 . Stop ( ) ;
timer4 . Interval = intervalStockLack ; ;
timer4 . Start ( ) ;
}
2023-09-04 22:41:19 +08:00
* /
2023-05-23 16:13:17 +08:00
}
2023-11-21 19:18:23 +08:00
debugLog ( " done.... get job intervals...... " ) ;
2023-05-23 16:13:17 +08:00
}
protected override void OnStart ( string [ ] args )
2023-09-04 22:41:19 +08:00
{
// log.Debug("get interval failed ,use default for time intervals, to start timers......");
timer1 . Interval = intervalOut ; // 1分执行一次
timer1 . Elapsed + = new ElapsedEventHandler ( this . OnTimer_1 ) ;
timer1 . Start ( ) ;
2023-11-21 19:18:23 +08:00
timer2 . Interval = intervalNoticeErp ; //
timer2 . Elapsed + = new ElapsedEventHandler ( this . OnTimer_2 ) ;
timer2 . Start ( ) ;
2023-05-23 16:13:17 +08:00
2023-11-21 19:18:23 +08:00
timer3 . Interval = intervalDoc ; // 3分钟
2023-09-04 22:41:19 +08:00
timer3 . Elapsed + = new ElapsedEventHandler ( this . OnTimer_3 ) ;
timer3 . Start ( ) ;
2023-05-23 16:13:17 +08:00
#if DEBUG
2023-09-04 22:41:19 +08:00
timerNewWorld . Interval = intervalTest ; // 2 * 60000; //18 * 60 * 60000; // 18 小时
timerNewWorld . Elapsed + = new ElapsedEventHandler ( this . OnTimer_NewWorld ) ;
timerNewWorld . Start ( ) ;
2023-05-23 16:13:17 +08:00
#endif
2023-09-04 22:41:19 +08:00
timer6 . Interval = 0.5 * 60000 ; // 30秒检查一次wave interval
timer6 . Elapsed + = new ElapsedEventHandler ( this . OnTimer_6 ) ;
timer6 . Start ( ) ;
try
{
debugLog ( "In OnStart." ) ;
initialIntervals ( ) ;
2023-05-23 16:13:17 +08:00
}
catch ( Exception er )
{
2023-09-04 22:41:19 +08:00
debugLog ( er . InnerException . ToString ( ) ) ;
log . Error ( er ) ;
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
2023-05-23 16:13:17 +08:00
}
/// <summary>
/// 停止服务
/// </summary>
protected override void OnStop ( )
{
// eventLog1.WriteEntry("In OnStop.");
2023-09-04 22:41:19 +08:00
debugLog ( "In OnStop." ) ;
2023-05-23 16:13:17 +08:00
}
/// <summary>
/// 继续服务
/// </summary>
protected override void OnContinue ( )
{
//eventLog1.WriteEntry("In OnContinue.");
2023-09-04 22:41:19 +08:00
debugLog ( "In OnContinue." ) ;
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
private void processOut ( )
{
using ( ServiceReferenceScheduledService . ScheduledServiceClient client = new ServiceReferenceScheduledService . ScheduledServiceClient ( ) )
{
log . Debug ( " to process out..." ) ;
string result = client . processOut ( outStatus ) ;
lastOut = DateTime . Now ;
log . Debug ( result ) ;
}
}
bool isCanStart = true ;
int time1Cnt = 0 ;
DateTime lastOut = DateTime . Now ;
// int normalPrcocessCnt = 0;
2023-05-23 16:13:17 +08:00
/// <summary>
/// 出库相关任务处理
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
public void OnTimer_1 ( object sender , ElapsedEventArgs args )
{
2023-09-04 22:41:19 +08:00
time1Cnt + + ;
debugLog ( "OnTimer_1 is bussy? " + ! isCanStart ) ;
if ( ! isCanStart )
{
return ;
}
isCanStart = false ;
2023-05-23 16:13:17 +08:00
timer1 . Stop ( ) ;
2023-09-04 22:41:19 +08:00
/ *
if ( normalPrcocessCnt > intervalStockLack / intervalOut )
{
outStatus = WindowsServiceOut . ServiceReferenceScheduledService . enumOutStockRequestStatus . 库 存 不 足 ;
normalPrcocessCnt = 0 ;
2023-05-23 16:13:17 +08:00
2023-09-04 22:41:19 +08:00
}
debugLog ( "outStatus: " + outStatus ) ;
normalPrcocessCnt + + ;
* /
debugLog ( "OnTimer_1 start process out ,,,\n,,,,,,,,,,,,,,,,,,,,,,,,/n,,,,,,,,,,,,,,,,,,,,>>> " + time1Cnt ) ; log . Debug ( "" ) ;
try
{
processOut ( ) ;
isCanStart = true ;
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
catch ( Exception er )
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
log . Error ( er ) ;
}
finally
{
isCanStart = true ;
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
outStatus = WindowsServiceOut . ServiceReferenceScheduledService . enumOutStockRequestStatus . 准 备 分 拣 ;
log . Debug ( "" ) ;
debugLog ( "OnTimer_1 end process out ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,<<< " + time1Cnt ) ;
2023-05-23 16:13:17 +08:00
2023-09-04 22:41:19 +08:00
isCanStart = true ;
2023-05-23 16:13:17 +08:00
if ( intervalOut > 0 & timer1 . Interval ! = intervalOut )
{
2023-09-04 22:41:19 +08:00
debugLog ( " new interal for out tasks . old is" + timer1 . Interval / 60000 + ",...............new interval = " + intervalOut / 60000 ) ;
2023-05-23 16:13:17 +08:00
timer1 . Interval = intervalOut ; ;
2023-09-04 22:41:19 +08:00
2023-05-23 16:13:17 +08:00
}
timer1 . Start ( ) ;
2023-09-04 22:41:19 +08:00
2023-05-23 16:13:17 +08:00
}
2023-11-21 19:18:23 +08:00
2023-05-23 16:13:17 +08:00
/// <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++);
2023-09-04 22:41:19 +08:00
debugLog ( "OnTimer_2 begin" ) ;
2023-05-23 16:13:17 +08:00
timer2 . Stop ( ) ;
2023-11-21 19:18:23 +08:00
try
2023-05-23 16:13:17 +08:00
{
2023-11-21 19:18:23 +08:00
using ( ServiceReferenceScheduledService . ScheduledServiceClient client = new ServiceReferenceScheduledService . ScheduledServiceClient ( ) )
{
debugLog ( "start notice erp process " ) ;
client . noticeErp ( ) ;
debugLog ( "end notice erp " ) ;
2023-05-23 16:13:17 +08:00
2023-11-21 19:18:23 +08:00
}
2023-05-23 16:13:17 +08:00
}
2023-11-21 19:18:23 +08:00
catch ( Exception er )
2023-05-23 16:13:17 +08:00
{
2023-11-21 19:18:23 +08:00
log . Error ( er ) ;
}
finally
{
}
if ( intervalNoticeErp > 0 & timer2 . Interval ! = intervalNoticeErp )
{
debugLog ( " new interal for NoticeErp tasks . old is" + timer2 . Interval / 60000 + ",...............new interval = " + intervalNoticeErp / 60000 ) ;
timer2 . Interval = intervalNoticeErp ; ;
2023-05-23 16:13:17 +08:00
}
timer2 . Start ( ) ;
2023-09-04 22:41:19 +08:00
debugLog ( "OnTimer_2 end" ) ;
2023-05-23 16:13:17 +08:00
}
2023-11-21 19:18:23 +08:00
bool canTest = true ;
2023-05-23 16:13:17 +08:00
/// <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++);
2023-09-04 22:41:19 +08:00
debugLog ( "OnTimer_3 begin" ) ;
2023-05-23 16:13:17 +08:00
timer3 . Stop ( ) ;
using ( ServiceReferenceScheduledService . ScheduledServiceClient client = new ServiceReferenceScheduledService . ScheduledServiceClient ( ) )
{
try
{
2023-09-04 22:41:19 +08:00
debugLog ( "start sync syncGoods " ) ;
2023-05-23 16:13:17 +08:00
client . syncGoods ( ) ;
2023-09-04 22:41:19 +08:00
debugLog ( "end sync syncGoods " ) ;
debugLog ( "start sync syncNewOwners " ) ;
2023-05-23 16:13:17 +08:00
client . syncNewOwners ( ) ;
2023-09-04 22:41:19 +08:00
debugLog ( "end sync syncNewOwners " ) ;
debugLog ( "start syncCustomer " ) ;
2023-05-23 16:13:17 +08:00
client . syncCustomer ( ) ;
2023-09-04 22:41:19 +08:00
debugLog ( "end syncCustomer " ) ;
2023-05-23 16:13:17 +08:00
}
catch ( Exception er )
{
2023-09-04 22:41:19 +08:00
debugLog ( "OnTimer_3 exception:" + er . Message + "/n" + er . StackTrace ) ;
2023-05-23 16:13:17 +08:00
}
}
if ( intervalDoc > 0 & timer3 . Interval ! = intervalDoc )
{
2023-09-04 22:41:19 +08:00
debugLog ( " new interal for dictionary tasks . old is" + timer3 . Interval / 60000 + ",...............new interval = " + intervalDoc / 60000 ) ;
2023-05-23 16:13:17 +08:00
timer3 . Interval = intervalDoc ; ;
}
timer3 . Start ( ) ;
2023-09-04 22:41:19 +08:00
debugLog ( "OnTimer_3 end" ) ;
2023-05-23 16:13:17 +08:00
}
public void OnTimer_NewWorld ( object sender , ElapsedEventArgs args )
{
2023-11-21 19:18:23 +08:00
2023-09-04 22:41:19 +08:00
#if DEBUG
debugLog ( "OnTimer_NewWorld begin" ) ;
2023-05-23 16:13:17 +08:00
timerNewWorld . Stop ( ) ;
using ( ServiceReferenceScheduledService . ScheduledServiceClient client = new ServiceReferenceScheduledService . ScheduledServiceClient ( ) )
{
2023-09-04 22:41:19 +08:00
2023-11-21 19:18:23 +08:00
2023-09-04 22:41:19 +08:00
try
{
2023-11-21 19:18:23 +08:00
if ( canTest )
{
canTest = false ;
debugLog ( "start test ..........." ) ;
client . truncateDataForTestOnly ( ) ;
canTest = true ;
debugLog ( "test done ..... enjoy! " ) ;
}
}
catch ( Exception er )
2023-09-04 22:41:19 +08:00
{
log . Error ( er ) ;
2023-11-21 19:18:23 +08:00
canTest = true ;
2023-09-04 22:41:19 +08:00
}
2023-11-21 19:18:23 +08:00
2023-09-04 22:41:19 +08:00
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
if ( intervalTest > 0 & timerNewWorld . Interval ! = intervalTest )
{
debugLog ( " new interal for test tasks . old is" + timerNewWorld . Interval / 60000 + ",...............new interval = " + intervalTest / 60000 ) ;
timerNewWorld . Interval = intervalTest ; ;
}
2023-05-23 16:13:17 +08:00
timerNewWorld . Start ( ) ;
2023-09-04 22:41:19 +08:00
debugLog ( "OnTimer_NewWorld end" ) ;
2023-05-23 16:13:17 +08:00
2023-09-04 22:41:19 +08:00
#endif
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
2023-05-23 16:13:17 +08:00
public void OnTimer_6 ( object sender , ElapsedEventArgs args )
2023-09-04 22:41:19 +08:00
{
debugLog ( "OnTimer_6 begin" ) ;
2023-05-23 16:13:17 +08:00
2023-09-04 22:41:19 +08:00
timer6 . Stop ( ) ;
try { initialIntervals ( ) ; }
catch
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
}
2023-05-23 16:13:17 +08:00
2023-09-04 22:41:19 +08:00
/ *
if ( lastOut . AddMilliseconds ( intervalOut * 5 ) < DateTime . Now )
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
// isCanStart = true;
// log.Debug(" force to process out...");
try
{
processOut ( ) ;
isCanStart = true ;
}
catch ( Exception er )
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
log . Error ( er ) ;
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
if ( lastOut . AddMilliseconds ( intervalOut ) < DateTime . Now )
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
timer1 . Stop ( ) ;
timer1 . Interval = intervalOut ;
timer1 . Elapsed + = new ElapsedEventHandler ( this . OnTimer_1 ) ;
timer1 . Start ( ) ;
log . Debug ( "call timer1 to start...." ) ;
2023-05-23 16:13:17 +08:00
}
2023-09-04 22:41:19 +08:00
* /
timer6 . Start ( ) ;
debugLog ( "OnTimer_6 end" ) ;
}
/// <summary>
/// 记录到指定路径: D:\log.txt
/// </summary>
/// <param name="message"></param>
private static void debugLog ( string message )
{
log . Debug ( message ) ;
2023-05-23 16:13:17 +08:00
}
}
}