SaveJobInfoController.java 2.67 KB
/**
 * 
 */
package com.yoho.jobs.server.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yoho.jobs.common.domain.ConstantEnum;
import com.yoho.jobs.common.domain.ProcessResult;
import com.yoho.jobs.dal.IJobResultMapper;
import com.yoho.jobs.dal.domain.ProcessResultVO;
import com.yoho.jobs.server.alarm.SendAlarmTool;

/**
 * @author yanzhang.fu
 *
 */
@Controller
@RequestMapping("/jobmanager")
public class SaveJobInfoController {

	@Resource
	private IJobResultMapper jobResultMapper;

	@Resource(name = "sendAlarmTool")
	private SendAlarmTool sendAlarmTool;

	@RequestMapping("/saveJobResult.do")
	public void saveJobResult(@RequestBody ProcessResult jobInfo) {
		ProcessResultVO result = new ProcessResultVO();
		convert(result, jobInfo);
		// TODO 若执行失败,则上报告警并入库
		if (ConstantEnum.JobProcessResultCode.ERROR_CODE.equals(jobInfo.getCode())) {
			// 上报告警
			Map<String, String> tags = new HashMap<String, String>();
			buildTags(result, tags);
			Map<String, Object> fields = new HashMap<String, Object>();
			buildFields(result, fields);
			sendAlarmTool.sendAlarm(tags, fields);
		}
		jobResultMapper.insertJob(result);
	}

	private void convert(ProcessResultVO result, ProcessResult jobInfo) {
		result.setActiveMode(jobInfo.getJobInfo().getActiveMode());
		result.setBeginTime(jobInfo.getBeginTime());
		result.setCode(jobInfo.getCode());
		result.setContext(jobInfo.getJobInfo().getContext());
		result.setCronExpression(jobInfo.getJobInfo().getCronExpression());
		result.setDesc(jobInfo.getJobInfo().getDesc());
		result.setEndTime(jobInfo.getEndTime());
		result.setJobGroup(jobInfo.getJobInfo().getJobGroup());
		result.setJobName(jobInfo.getJobInfo().getJobName());
		result.setJobType(jobInfo.getJobInfo().getJobType());
		result.setMessage(jobInfo.getMessage());
		result.setModule(jobInfo.getJobInfo().getModule());
		result.setOperator(jobInfo.getJobInfo().getOperator());
		result.setProcessIp(jobInfo.getJobInfo().getProcessIp());
		result.setProcessor(jobInfo.getJobInfo().getProcessor());
		result.setStatus(jobInfo.getJobInfo().getStatus());
		result.setRecordDate(new Date());
	}

	private void buildTags(ProcessResultVO result, Map<String, String> tags) {
		tags.put("jobname", result.getJobName());
		tags.put("module", result.getModule());
	}

	private void buildFields(ProcessResultVO result, Map<String, Object> fields) {
		fields.put("message", result.getMessage());
		fields.put("desc", "exec job faild");

	}
}