JobServiceController.java 4.66 KB
/**
 * 
 */
package com.yoho.jobs.server.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yoho.jobs.common.domain.ConstantEnum;
import com.yoho.jobs.common.domain.JobInfo;
import com.yoho.jobs.dal.IJobResultMapper;
import com.yoho.jobs.dal.domain.ProcessResultVO;
import com.yoho.jobs.server.domain.JobResponse;
import com.yoho.jobs.server.domain.PageRequest;
import com.yoho.jobs.server.domain.PageResponse;
import com.yoho.jobs.server.scheduler.JobService;

/**
 * @author yanzhang.fu
 *
 */
@RestController
public class JobServiceController {
	@Resource(name = "jobService")
	private JobService jobService;

	@Resource
	private IJobResultMapper jobResultMapper;

	@Value("${limitrecord}")
	private int limit;

	@RequestMapping("/jobmanager/createjob.do")
	public JobResponse createJob(@RequestBody JobInfo jobInfo) {
		JobResponse response = new JobResponse();
		try {
			jobService.createJob(jobInfo);
		} catch (Exception e) {
			buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
		}
		return response;
	}

	@RequestMapping("/jobmanager/stopjob.do")
	public JobResponse stopJob(@RequestBody JobInfo jobInfo) {
		JobResponse response = new JobResponse();
		try {
			jobService.stopJob(jobInfo);
		} catch (Exception e) {
			buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
		}
		return response;
	}

	@RequestMapping("/jobmanager/startjob.do")
	public JobResponse startJob(@RequestBody JobInfo jobInfo) {
		JobResponse response = new JobResponse();
		try {
			jobService.startJob(jobInfo);
		} catch (Exception e) {
			buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
		}

		return response;
	}

	@RequestMapping("/jobmanager/updatejob.do")
	public JobResponse updateJob(@RequestBody JobInfo jobInfo) {
		JobResponse response = new JobResponse();
		try {
			jobService.updateJob(jobInfo);
		} catch (Exception e) {
			buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
		}

		return response;
	}

	@RequestMapping("/jobmanager/showalljob.do")
	public JobResponse showAllJobs(@RequestBody PageRequest request) {
		JobResponse response = new JobResponse();
		try {
			checkParam(request);
			Map<String, List<JobInfo>> jobs = jobService.getAllJobs();
			List<JobInfo> allJobs = new ArrayList<JobInfo>();
			jobs.forEach((k, v) -> {
				allJobs.addAll(v);
			});
			PageResponse<JobInfo> pr = new PageResponse<JobInfo>();
			int endIdx = min(allJobs.size(), request.getCurrentPage() * request.getPageSize());
			pr.setRows(allJobs.subList((request.getCurrentPage() - 1)*request.getPageSize(), endIdx));
			pr.setCurrPage(request.getCurrentPage());
			pr.setPageSize(request.getPageSize());
			pr.setTotal(allJobs.size());
			response.setData(pr);
			response.setCode(ConstantEnum.JobResponse.SUCCESS);
			response.setDesc(ConstantEnum.JobResponse.SUCCESS_DESC);
		} catch (Exception e) {
			response.setCode(ConstantEnum.JobResponse.FAIL);
			response.setDesc(e.getMessage());
		}
		return response;
	}

	private void checkParam(PageRequest request) throws Exception {
		if(request.getCurrentPage()<1 || request.getPageSize()<1) {
			throw new Exception("check request failed. currentPage or pageSize less than 1");
		}
	}

	@RequestMapping("/jobmanager/retryjob.do")
	public JobResponse retryJob(@RequestBody JobInfo jobInfo) {
		JobResponse response = new JobResponse();
		try {
			jobService.retryJob(jobInfo);

		} catch (Exception e) {
			buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
		}

		return response;
	}

	@RequestMapping("/jobmanager/getdetailjob.do")
	public JobResponse getDetailJob(@RequestBody JobInfo jobInfo) {
		JobResponse response = new JobResponse();
		try {
			List<ProcessResultVO> jobs = jobResultMapper.selectJobs(jobInfo.getJobName(), jobInfo.getModule(), limit);
			PageResponse<ProcessResultVO> pr = new PageResponse<ProcessResultVO>();
			pr.setRows(jobs);
			pr.setCurrPage(1);
			pr.setTotal(jobs.size());
			response.setData(pr);
		} catch (Exception e) {
			buildJobResponse(ConstantEnum.JobResponse.FAIL, e.getMessage(), response);
		}

		return response;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	private void buildJobResponse(String code, String message, JobResponse response) {
		response.setCode(code);
		response.setDesc(message);
	}

	private int min(int a, int b) {
		return a > b ? b : a;
	}
}