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 if(jobSnapshot.getStatus()==JobSnapshotStatus.STOP.value()){
			
			getManager().getMasterSchedulerManager().stop(TriggerType.newDispatchTypeByMaster(),getJobSnapshot().getName());
			return false;
		}else{
			//非正常结束
			List<WorkerErrMsg> msgs = getManager().getWorkerErrMsgDao().queryByJob(getTriggerJobSnapshotId(), getTriggerJobName());
			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
		
	}
}

@@ -1467,17 +1467,163 @@
 %0A%09%09%7Delse
- 
+%7B%0D%0A%09%09%09//%E9%9D%9E%E6%AD%A3%E5%B8%B8%E7%BB%93%E6%9D%9F%0D%0A%09%09%09List%3CWorkerErrMsg%3E msgs = getManager().getWorkerErrMsgDao().queryByJob(getTriggerJobSnapshotId(), getTriggerJobName());%0D%0A%09%09%09%0D%0A%09%09%09
 if(jobSn
@@ -1676,19 +1676,53 @@
 ue()
+ && (msgs == null %7C%7C msgs.size() == 0)
 )%7B%0D%0A%09%09%09
-%0D%0A%09%09
 %09get
@@ -1827,32 +1827,33 @@
 getName());%0D%0A%09%09%09
+%09
 return false;%0D%0A%09
@@ -1857,150 +1857,15 @@
 %0D%0A%09%09
-%7Delse%7B%0D%0A%09%09%09//%E9%9D%9E%E6%AD%A3%E5%B8%B8%E7%BB%93%E6%9D%9F%0D%0A%09%09%09List%3CWorkerErrMsg%3E msgs = getManager().getWorkerErrMsgDao().queryByJob(getTriggerJobSnapshotId(), getTriggerJobName());
+%09%7D%0D%0A%09%09%09
 %0D%0A%09%09
@@ -2132,21 +2132,16 @@
 %0D%0A%09%09%09%7D%0D%0A
-%09%09%09%0D%0A
 %09%09%09retur

修改当状态为stop时，添加msgs空判断及size判断。如果为空则正常stop，不为空且size大于0则判断异常触发任务。