package six.com.crawler.work;

import java.io.Serializable;
import java.util.List;

import six.com.crawler.entity.JobSnapshot;
import six.com.crawler.entity.JobSnapshotStatus;
import six.com.crawler.entity.WorkerErrMsg;
import six.com.crawler.schedule.TriggerType;
import six.com.crawler.work.exception.WorkerException;
import six.com.crawler.work.exception.WorkerMonitorException;
import six.com.crawler.work.space.WorkSpaceData;

/**
 * @author 作者
 * @E-mail: 359852326@qq.com
 * @date 创建时间：2017年6月6日 上午10:56:46
 * 
 *       监控任务worker
 * 
 */
public class CommonMonitorWorker extends AbstractMonitorWorker implements Serializable{

	private static final long serialVersionUID = -3421176209187012514L;

	/**
	 * 实现监控逻辑,需要循环监控的话，返回true,否则返回false监控任务线程将会结束
	 * 
	 * @return
	 * @throws WorkerException
	 */
	protected boolean doMonitor() throws WorkerException{
		JobSnapshot jobSnapshot=getManager().getScheduleCache().getJobSnapshot(getTriggerJobName());
		if(null==jobSnapshot){
			jobSnapshot=getManager().getJobSnapshotDao().query(getTriggerJobSnapshotId(), getTriggerJobName());
		}
		if(null==jobSnapshot){
			 throw new WorkerMonitorException("Job info exception!");
		}
		if(jobSnapshot.getStatus()==JobSnapshotStatus.FINISHED.value()){
			//任务结束
			getWorkSpace().clearDoing();
			getManager().getMasterSchedulerManager().finish(TriggerType.newDispatchTypeByMaster(),getJobSnapshot().getName());
			return false;
		}else{
			//非正常结束
			List<WorkerErrMsg> msgs = getManager().getWorkerErrMsgDao().queryByJob(getTriggerJobSnapshotId(), getTriggerJobName());
			
			if(jobSnapshot.getStatus()==JobSnapshotStatus.STOP.value() && (msgs == null || msgs.size() == 0)){
				getManager().getMasterSchedulerManager().stop(TriggerType.newDispatchTypeByMaster(),getJobSnapshot().getName());
				return false;
			}
			
			for (int i = 0; i < msgs.size(); i++) {
				if(msgs.get(i).getType().equals("worker_init")){
					//重新调度任务并返回false
					getManager().getMasterSchedulerManager().execute(TriggerType.newDispatchTypeByMaster(), getTriggerJobName());
					return false;
				}
			}
			return true;
		}
	};

	@Override
	protected void onError(Exception t, WorkSpaceData workerData) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void insideDestroy() {
		// TODO Auto-generated method stub
		
	}
}

@@ -1676,12 +1676,18 @@
 ue()
- && 
+)%7B%0D%0A%09%09%09%09if
 (msg
@@ -1716,20 +1716,20 @@
 () == 0)
-)
 %7B%0D%0A
+%09
 %09%09%09%09getM
@@ -1838,24 +1838,25 @@
 me());%0D%0A%09%09%09%09
+%09
 return false
@@ -1865,17 +1865,20 @@
 %0A%09%09%09
-%7D
+%09%7Delse%7B
 %0D%0A%09%09%09
-%0D%0A%09
 %09%09fo
@@ -1916,16 +1916,18 @@
 i++) %7B%0D%0A
+%09%09
 %09%09%09%09if(m
@@ -1977,16 +1977,18 @@
 %7B%0D%0A%09%09%09%09%09
+%09%09
 //%E9%87%8D%E6%96%B0%E8%B0%83%E5%BA%A6%E4%BB%BB%E5%8A%A1
@@ -1997,16 +1997,18 @@
 %E5%9B%9Efalse%0D%0A
+%09%09
 %09%09%09%09%09get
@@ -2112,32 +2112,34 @@
 bName());%0D%0A%09%09%09%09%09
+%09%09
 return false;%0D%0A%09
@@ -2145,15 +2145,37 @@
 %09%09%09%09
+%09%09
 %7D%0D%0A%09%09%09
-%7D
+%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%09
 %0D%0A%09%09

修改逻辑判断，