Authored by jack

update

Showing 18 changed files with 734 additions and 263 deletions
package com.monitor.awstools.comp;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.TaskStatus;
import com.monitor.awstools.job.AwsTask;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.CuratorListener;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.utils.ZKPaths;
import org.quartz.JobKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yoho on 2016/9/1.
*/
@Component
public class TaskListener {
@Autowired
ZkClient zkClient;
@Autowired
TaskScheduler taskScheduler;
@Autowired
TaskSaver taskSaver;
PathChildrenCache childrenCache;
@PostConstruct
public void addWatch() {
childrenCache = new PathChildrenCache(zkClient.getCuratorFramework(), Constants.TASKPATH, true);
PathChildrenCacheListener childrenCacheListener = new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
switch (pathChildrenCacheEvent.getType()) {
case CHILD_REMOVED:
deleteNode(pathChildrenCacheEvent);
break;
case CHILD_ADDED:
addNode(pathChildrenCacheEvent);
break;
default:
break;
}
}
};
childrenCache.getListenable().addListener(childrenCacheListener);
try {
childrenCache.start();
} catch (Exception e) {
}
}
private void deleteNode(PathChildrenCacheEvent pathChildrenCacheEvent) {
String path = pathChildrenCacheEvent.getData().getPath();
String jobKey = ZKPaths.getNodeFromPath(path);
String[] info = StringUtils.split(jobKey, ".");
taskScheduler.deleteJob(new JobKey(info[1], info[0]));
}
private void addNode(PathChildrenCacheEvent pathChildrenCacheEvent) {
String path = pathChildrenCacheEvent.getData().getPath();
String jobKey = ZKPaths.getNodeFromPath(path);
try {
Thread.sleep(1*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
AwsTask awsTask = taskSaver.queryTask(jobKey);
taskScheduler.scheduleJob(awsTask.buildJobDetail(), awsTask.buildJobTrigger());
}
}
... ...
package com.monitor.awstools.comp;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.job.AwsTask;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.CreateMode;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by yoho on 2016/8/30.
*/
@Component
public class JobSaver {
public class TaskSaver {
public static final Logger DEBUG = LoggerFactory.getLogger(TaskSaver.class);
@Autowired
ZkClient zkClient;
... ... @@ -63,6 +74,14 @@ public class JobSaver {
}
}
public void configJob(String path, String value) {
try {
zkClient.getCuratorFramework().setData().forPath(path, value.getBytes());
} catch (Exception e) {
}
}
public void deleteJob(String jobKey) {
... ... @@ -78,4 +97,89 @@ public class JobSaver {
e.printStackTrace();
}
}
public List<AwsTask> queryAllTasks() {
List<AwsTask> awsTasksList = new ArrayList<>();
//读取zookeeper中任务配置
List<String> childNodes = null;
try {
childNodes = zkClient.getCuratorFramework().getChildren().forPath(Constants.TASKPATH);
} catch (Exception e) {
DEBUG.error("Failed to load task from zookeeper ,error {}", e);
}
if (null == childNodes || childNodes.isEmpty()) {
DEBUG.info("Not found task in zookeeper...");
return awsTasksList;
}
for (String child : childNodes) {
AwsTask task = queryTask(child);
if (null != task) {
awsTasksList.add(task);
}
}
return awsTasksList;
}
public AwsTask queryTask(String taskKey) {
String taskPath = ZKPaths.makePath(Constants.TASKPATH, taskKey);
List<String> childNodes = null;
try {
childNodes = zkClient.getCuratorFramework().getChildren().forPath(taskPath);
} catch (Exception e) {
DEBUG.error("Failed to load task from zookeeper ,error {}", e);
}
if (null == childNodes || childNodes.isEmpty()) {
return null;
}
Map<String, String> proMaps = new HashMap<>();
for (String key : childNodes) {
String keyPath = ZKPaths.makePath(taskPath, key);
try {
byte[] data = zkClient.getCuratorFramework().getData().forPath(keyPath);
proMaps.put(key, new String(data));
} catch (Exception e) {
e.printStackTrace();
}
}
String jobName = proMaps.get(Constants.TASKNAME);
String jobGroup = proMaps.get(Constants.TASKGROUP);
JobType jobType = JobType.ONCE;
if (StringUtils.equals(proMaps.get(Constants.JOBTYPE), JobType.CRON.name())) {
jobType = JobType.CRON;
} else if (StringUtils.equals(proMaps.get(Constants.JOBTYPE), JobType.INTERVAL.name())) {
jobType = JobType.INTERVAL;
}
AwsTask awsTask = new AwsTask(jobName, jobGroup, jobType);
awsTask.setProMaps(proMaps);
return awsTask;
}
}
... ...
... ... @@ -13,7 +13,7 @@ import java.util.Map;
* Created by yoho on 2016/8/30.
*/
@Component
public class JobScheduler {
public class TaskScheduler {
public Scheduler scheduler;
... ...
... ... @@ -22,4 +22,14 @@ public class Constants {
public static final String TASKTYPE = "taskType";
public static final String GROUP = "YOHO";
public static final String EXPRESSION = "expression";
public static final String VOLUME = "volume";
public static final String DESCRPTION = "descrption";
public static final String CREATETIME = "createTime";
}
... ...
package com.monitor.awstools.constant;
import sun.print.PSStreamPrinterFactory;
/**
* Created by yoho on 2016/8/30.
... ... @@ -10,10 +9,10 @@ public enum JobType {
//一次任务
ONCE,
//cron表达式定时任务
CRON,
//间歇任务
INTERVAL
INTERVAL,
//cron表达式定时任务
CRON
}
... ...
package com.monitor.awstools.controller;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.constant.TaskStatus;
import com.monitor.awstools.constant.TaskType;
import com.monitor.awstools.job.AwsTask;
import com.monitor.awstools.model.*;
import com.monitor.awstools.service.AwsQueryService;
import com.monitor.awstools.service.TaskService;
import com.monitor.model.response.BaseResponse;
import com.sun.javafx.collections.MappingChange;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by yoho on 2016/8/30.
*/
@RestController
@RequestMapping(value = "/aws")
public class AwsToolController {
public static final Logger DEBUG = LoggerFactory.getLogger(AwsToolController.class);
@Autowired
AwsQueryService queryService;
@Autowired
TaskService taskService;
@RequestMapping(value = "/snapshot/check")
public BaseResponse checkSnapShot(@RequestBody AwsEbsReq task) {
List<M_SnapShot> snapShotList = queryService.querySnapShotList(task.getVolumeId());
BaseResponse response = new BaseResponse();
response.setData(snapShotList);
return response;
}
/**
* volume Id校验
*
* @param task
* @return
*/
@RequestMapping(value = "/volume/check")
public BaseResponse checkVolume(@RequestBody AwsEbsReq task) {
M_Volume m_volume = queryService.queryVolumeInfo(task.getVolumeId());
if (null == m_volume) {
return new BaseResponse(400, "未找到相关volume,请确认id...");
}
String instanceId = m_volume.getInstanceId();
AwsEbsResp awsEbsTaskResp = new AwsEbsResp();
if (StringUtils.isBlank(instanceId)) {
awsEbsTaskResp.setM_instance(new M_Instance());
awsEbsTaskResp.setM_volume(m_volume);
} else {
M_Instance m_instance = queryService.queryInstanceInfo(instanceId);
awsEbsTaskResp.setM_volume(m_volume);
awsEbsTaskResp.setM_instance(m_instance);
}
BaseResponse response = new BaseResponse();
response.setData(awsEbsTaskResp);
return response;
}
/**
* 查询所有的task信息
*
* @return
*/
@RequestMapping(value = "/task/all")
public BaseResponse queryTasks() {
List<AwsEbsTaskResp> awsEbsTaskRespList = new ArrayList<>();
List<AwsTask> awsTaskList = taskService.queryAllTasks();
for (AwsTask awsTask : awsTaskList) {
awsEbsTaskRespList.add(converAwsTask(awsTask));
}
BaseResponse baseResponse = new BaseResponse();
baseResponse.setData(awsEbsTaskRespList);
return baseResponse;
}
/**
* 创建task
*
* @param task
* @return
*/
@RequestMapping(value = "/task/create")
public BaseResponse createTask(@RequestBody AwsEbsReq task) {
Map<String, String> proMaps = convertProMap(task);
//插入创建时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy/MM/dd HH:mm");
proMaps.put(Constants.CREATETIME, simpleDateFormat.format(new Date()));
try {
taskService.addTask(proMaps.get(Constants.TASKNAME), proMaps.get(Constants.TASKGROUP), JobType.valueOf(proMaps.get(Constants.JOBTYPE)), proMaps);
} catch (Exception e) {
DEBUG.error("Failed to create task.... error {}", e);
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
/**
* 修改task
*
* @param task
* @return
*/
@RequestMapping(value = "/task/edit")
public BaseResponse editTask(@RequestBody AwsEbsReq task) {
Map<String, String> proMap = convertProMap(task);
try {
taskService.configTask(proMap.get(Constants.TASKNAME), proMap.get(Constants.TASKGROUP), proMap);
} catch (Exception e) {
DEBUG.error("Failed to re-config task .... error {}", e);
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
/**
* 暂停task
*
* @param task
* @return
*/
@RequestMapping(value = "/task/stop")
public BaseResponse stopTask(@RequestBody AwsEbsReq task) {
try {
taskService.pauseTask(task.getTaskName(), Constants.GROUP);
} catch (Exception e) {
DEBUG.error("Failed to stop task .... error {}", e);
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
/**
* 启动task
*
* @param task
* @return
*/
@RequestMapping(value = "/task/start")
public BaseResponse startTask(@RequestBody AwsEbsReq task) {
try {
taskService.resumeTask(task.getTaskName(), Constants.GROUP);
} catch (Exception e) {
DEBUG.error("Failed to start task .... error {}", e);
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
/**
* 删除task
*
* @param task
* @return
*/
@RequestMapping(value = "/task/delete")
public BaseResponse deleteTask(@RequestBody AwsEbsReq task) {
try {
taskService.deleteTask(task.getTaskName(), Constants.GROUP);
} catch (Exception e) {
DEBUG.error("Failed to start task .... error {}", e);
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
/**
* 查询task
*
* @param task
* @return
*/
@RequestMapping(value = "/task/query")
public BaseResponse queryTask(@RequestBody AwsEbsReq task) {
AwsTask awsTask;
try {
awsTask = taskService.queryTask(task.getTaskName(), Constants.GROUP);
} catch (Exception e) {
DEBUG.error("Failed to start task .... error {}", e);
return new BaseResponse(400, e.getMessage());
}
BaseResponse response = new BaseResponse();
response.setData(converAwsTask(awsTask));
return response;
}
/**
* 查询task运行详情
*
* @param task
* @return
*/
public BaseResponse infoTask(@RequestBody AwsEbsReq task) {
try {
taskService.deleteTask(task.getTaskName(), Constants.GROUP);
} catch (Exception e) {
DEBUG.error("Failed to start task .... error {}", e);
return new BaseResponse(400, e.getMessage());
}
return new BaseResponse();
}
private Map<String, String> convertProMap(AwsEbsReq awsEbsReq) {
Map<String, String> proMap = new HashMap<>();
proMap.put(Constants.TASKNAME, awsEbsReq.getTaskName());
proMap.put(Constants.TASKGROUP, Constants.GROUP);
JobType jobType;
switch (awsEbsReq.getTaskType()) {
case "0":
jobType = JobType.ONCE;
break;
case "1":
jobType = JobType.INTERVAL;
break;
case "2":
jobType = JobType.CRON;
break;
default:
jobType = JobType.ONCE;
break;
}
proMap.put(Constants.JOBTYPE, jobType.name());
proMap.put(Constants.EXPRESSION, awsEbsReq.getTaskExp());
proMap.put(Constants.VOLUME, awsEbsReq.getVolumeId());
proMap.put(Constants.DESCRPTION, awsEbsReq.getSanpShotDesc());
proMap.put(Constants.TASKSTATUS, TaskStatus.START.name());
proMap.put(Constants.TASKTYPE, TaskType.CREATESNAPSHOT.name());
return proMap;
}
public AwsEbsTaskResp converAwsTask(AwsTask awsTask) {
AwsEbsTaskResp ebsTaskResp = new AwsEbsTaskResp();
ebsTaskResp.setTaskName(awsTask.getJobName());
ebsTaskResp.setTaskType(String.valueOf(awsTask.getJobType().ordinal()));
ebsTaskResp.setTaskCtime(awsTask.getProMaps().get(Constants.CREATETIME));
ebsTaskResp.setTaskState(awsTask.getProMaps().get(Constants.TASKSTATUS));
ebsTaskResp.setSanpShotDesc(awsTask.getProMaps().get(Constants.DESCRPTION));
ebsTaskResp.setTaskExp(awsTask.getProMaps().get(Constants.EXPRESSION));
ebsTaskResp.setVolumeId(awsTask.getProMaps().get(Constants.VOLUME));
return ebsTaskResp;
}
}
... ...
... ... @@ -18,7 +18,7 @@ import java.util.List;
/**
* Created by yoho on 2016/8/30.
*/
public class Task implements InterruptableJob {
public class AwsJobImpl implements InterruptableJob {
@Override
public void interrupt() throws UnableToInterruptJobException {
... ... @@ -27,7 +27,7 @@ public class Task implements InterruptableJob {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
String aciton = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("action");
String aciton = (String) jobExecutionContext.getJobDetail().getJobDataMap().get(Constants.TASKTYPE);
AWSCredentials awsCredentials = new BasicAWSCredentials(Constants.ACCESSKEYID, Constants.ACCESSKEY);
... ... @@ -35,58 +35,14 @@ public class Task implements InterruptableJob {
if (StringUtils.equals(aciton, TaskType.CREATESNAPSHOT.name())) {
String volumeId = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("volumeId");
String volumeId = (String) jobExecutionContext.getJobDetail().getJobDataMap().get(Constants.VOLUME);
String description = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("description");
String description = (String) jobExecutionContext.getJobDetail().getJobDataMap().get(Constants.DESCRPTION);
CreateSnapshotResult result = amazonEC2.createSnapshot(new CreateSnapshotRequest(volumeId, description));
result.getSnapshot().getSnapshotId();
}
}
public static void main(String[] args) {
AWSCredentials awsCredentials = new BasicAWSCredentials(Constants.ACCESSKEYID, Constants.ACCESSKEY);
AmazonEC2 amazonEC2 = new AmazonEC2Client(awsCredentials);
Region region = Region.getRegion(Regions.CN_NORTH_1);
amazonEC2.setRegion(region);
String volumeId = "vol-4c808589";
String description = "test_snapshot";
/* CreateSnapshotResult result = amazonEC2.createSnapshot(new CreateSnapshotRequest(volumeId, description));
result.getSnapshot().getSnapshotId();*/
String volume="{VolumeId: vol-4c808589,InstanceId: i-cb4e1bf3,Device: /dev/sdb,State: attached,AttachTime: Mon Mar 14 15:24:58 CST 2016,DeleteOnTermination: true}";
List<String> instanceIdList=new ArrayList<>();
instanceIdList.add("i-cb4e1bf3");
DescribeInstancesRequest request=new DescribeInstancesRequest();
request.setInstanceIds(instanceIdList);
DescribeInstancesResult result2=amazonEC2.describeInstances(request);
List<Reservation> reservationList=result2.getReservations();
Instance instance=reservationList.get(0).getInstances().get(0);
System.out.println(instance.toString());
}
}
... ...
package com.monitor.awstools.job;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.sun.javafx.collections.MappingChange;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.quartz.*;
import java.util.Map;
import java.util.Properties;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
... ... @@ -16,7 +17,7 @@ import static org.quartz.TriggerBuilder.newTrigger;
* Created by yoho on 2016/8/30.
*/
@Data
public class AwsJob {
public class AwsTask {
private String jobName;
... ... @@ -24,9 +25,11 @@ public class AwsJob {
private JobType jobType;
@Getter
@Setter
Map<String, String> proMaps;
public AwsJob(String jobName, String jobGroup, JobType jobType) {
public AwsTask(String jobName, String jobGroup, JobType jobType) {
this.jobName = jobName;
this.jobGroup = jobGroup;
this.jobType = jobType;
... ... @@ -35,7 +38,7 @@ public class AwsJob {
public JobDetail buildJobDetail() {
JobDetail jobDetail = newJob()
.ofType(Task.class)
.ofType(AwsJobImpl.class)
.withIdentity(jobName, jobGroup)
.setJobData(buildJobDataMap())
.build();
... ... @@ -49,14 +52,14 @@ public class AwsJob {
if (StringUtils.equals(JobType.INTERVAL.name(), jobType.name())) {
trigger = newTrigger()
.withIdentity(jobName, jobGroup)
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(Integer.valueOf(proMaps.get("interval"))).repeatForever())
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(Integer.valueOf(proMaps.get(Constants.EXPRESSION))).repeatForever())
.startNow()
.build();
} else if (StringUtils.equals(JobType.CRON.name(), jobType.name())) {
trigger = newTrigger()
.withIdentity(jobName, jobGroup)
.withSchedule(CronScheduleBuilder.cronSchedule((String) proMaps.get("cron")))
.withSchedule(CronScheduleBuilder.cronSchedule((String) proMaps.get(Constants.EXPRESSION)))
.build();
} else {
... ...
package com.monitor.awstools.model;
import lombok.Data;
/**
* Created by yoho on 2016/8/31.
*/
@Data
public class AwsEbsReq {
String volumeId;
String taskName;
String sanpShotDesc;
String taskType;
String taskExp;
}
... ...
... ... @@ -6,7 +6,9 @@ import lombok.Data;
* Created by yoho on 2016/8/31.
*/
@Data
public class AwsEbsTaskReq {
public class AwsEbsResp {
String volumeId;
M_Instance m_instance;
M_Volume m_volume;
}
... ...
package com.monitor.awstools.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/9/1.
*/
@Data
public class AwsEbsTaskInfoResp {
M_Instance m_instance;
M_Volume m_volume;
List<M_SnapShot> snapShots;
}
... ...
... ... @@ -3,12 +3,21 @@ package com.monitor.awstools.model;
import lombok.Data;
/**
* Created by yoho on 2016/8/31.
* Created by yoho on 2016/9/1.
*/
@Data
public class AwsEbsTaskResp {
String taskName;
M_Instance m_instance;
String taskType;
M_Volume m_volume;
String taskCtime;
String taskState;
String volumeId;
String sanpShotDesc;
String taskExp;
}
... ...
package com.monitor.awstools.model;
import lombok.Data;
/**
* Created by yoho on 2016/9/1.
*/
@Data
public class M_SnapShot {
String snapshotId;
String state;
String startTime;
String progress;
String description;
}
... ...
... ... @@ -3,6 +3,7 @@ package com.monitor.awstools.service;
import com.amazonaws.services.ec2.model.*;
import com.monitor.awstools.comp.AWSClientComp;
import com.monitor.awstools.model.M_Instance;
import com.monitor.awstools.model.M_SnapShot;
import com.monitor.awstools.model.M_Volume;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -19,7 +20,7 @@ import java.util.List;
public class AwsQueryService {
@Autowired
AWSClientComp awsClientComp=new AWSClientComp();
AWSClientComp awsClientComp = new AWSClientComp();
/**
* 查询volume信息
... ... @@ -133,19 +134,76 @@ public class AwsQueryService {
}
public List<M_SnapShot> querySnapShotList(String volumeId) {
List<Filter> filterList = new ArrayList<>();
Filter filter = new Filter("volume-id");
List<String> volumeList = new ArrayList<>();
volumeList.add(volumeId);
filter.setValues(volumeList);
filterList.add(filter);
DescribeSnapshotsRequest request = new DescribeSnapshotsRequest();
request.setFilters(filterList);
DescribeSnapshotsResult result = awsClientComp.getClient().describeSnapshots(request);
List<Snapshot> snapshotList = result.getSnapshots();
List<M_SnapShot> m_snapShotList = new ArrayList<>();
for (Snapshot snapshot : snapshotList) {
m_snapShotList.add(convertSnapShot(snapshot));
}
return m_snapShotList;
}
public static void main(String[] args) {
AwsQueryService queryService=new AwsQueryService();
AwsQueryService queryService = new AwsQueryService();
M_Volume m_volume=queryService.queryVolumeInfo("vol-4c808589");
/* M_Volume m_volume = queryService.queryVolumeInfo("vol-4c808589");
if(null==m_volume)
{
if (null == m_volume) {
return;
}
M_Instance m_instance=queryService.queryInstanceInfo(m_volume.getInstanceId());
M_Instance m_instance = queryService.queryInstanceInfo(m_volume.getInstanceId());
System.out.println(m_volume);
System.out.println(m_instance);
System.out.println(m_instance);*/
List<M_SnapShot> snapShotList = queryService.querySnapShotList("vol-4c808589");
for (M_SnapShot m_snapShot : snapShotList) {
System.out.println(m_snapShot);
}
}
private M_SnapShot convertSnapShot(Snapshot snapshot) {
M_SnapShot m_snapShot = new M_SnapShot();
m_snapShot.setDescription(snapshot.getDescription());
m_snapShot.setProgress(snapshot.getProgress());
m_snapShot.setSnapshotId(snapshot.getSnapshotId());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy/MM/dd HH:mm");
m_snapShot.setStartTime(simpleDateFormat.format(snapshot.getStartTime()));
m_snapShot.setState(snapshot.getState());
return m_snapShot;
}
}
... ...
package com.monitor.awstools.service;
import com.monitor.awstools.model.AwsEbsTaskReq;
import com.monitor.awstools.model.AwsEbsTaskResp;
import com.monitor.awstools.model.M_Instance;
import com.monitor.awstools.model.M_Volume;
import com.monitor.model.response.BaseResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by yoho on 2016/8/30.
*/
@RestController
@RequestMapping(value = "/aws")
public class AwsToolService {
@Autowired
AwsQueryService queryService;
@RequestMapping(value = "/strategy")
public BaseResponse configStrategy() {
return null;
}
@RequestMapping(value = "/snapshot")
public BaseResponse configSnapshot() {
return null;
}
@RequestMapping(value = "/volume/check")
public BaseResponse checkVolume(@RequestBody AwsEbsTaskReq task) {
M_Volume m_volume = queryService.queryVolumeInfo(task.getVolumeId());
if (null == m_volume) {
return new BaseResponse(400, "未找到相关volume,请确认id...");
}
String instanceId = m_volume.getInstanceId();
AwsEbsTaskResp awsEbsTaskResp = new AwsEbsTaskResp();
if (StringUtils.isBlank(instanceId)) {
awsEbsTaskResp.setM_instance(new M_Instance());
awsEbsTaskResp.setM_volume(m_volume);
} else {
M_Instance m_instance = queryService.queryInstanceInfo(instanceId);
awsEbsTaskResp.setM_volume(m_volume);
awsEbsTaskResp.setM_instance(m_instance);
}
BaseResponse response = new BaseResponse();
response.setData(awsEbsTaskResp);
return response;
}
}
package com.monitor.awstools.service;
import com.monitor.awstools.comp.JobScheduler;
import com.monitor.awstools.comp.TaskSaver;
import com.monitor.awstools.comp.TaskScheduler;
import com.monitor.awstools.comp.ZkClient;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.job.AwsJob;
import com.monitor.awstools.job.AwsTask;
import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.slf4j.Logger;
... ... @@ -27,82 +29,21 @@ public class JobInitialService implements ApplicationListener<ContextRefreshedEv
public static final Logger DEBUG = LoggerFactory.getLogger(JobInitialService.class);
@Autowired
ZkClient zkClient;
TaskSaver taskSaver;
@Autowired
JobScheduler jobScheduler;
TaskScheduler taskScheduler;
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
//读取zookeeper中配置,恢复定时任务
List<String> childNodes = null;
try {
childNodes = zkClient.getCuratorFramework().getChildren().forPath(Constants.TASKPATH);
} catch (Exception e) {
DEBUG.error("Failed to load task from zookeeper ,error {}", e);
}
if (null == childNodes || childNodes.isEmpty()) {
DEBUG.info("Not found task in zookeeper...");
return;
}
for (String child : childNodes) {
loadTaskFromZookeeper(child);
}
}
public void loadTaskFromZookeeper(String taskKey) {
String taskPath = ZKPaths.makePath(Constants.TASKPATH, taskKey);
List<String> childNodes = null;
try {
childNodes = zkClient.getCuratorFramework().getChildren().forPath(taskPath);
} catch (Exception e) {
DEBUG.error("Failed to load task from zookeeper ,error {}", e);
}
if (null == childNodes || childNodes.isEmpty()) {
return;
}
Map<String, String> proMaps = new HashMap<>();
for (String key : childNodes) {
String keyPath = ZKPaths.makePath(taskPath, key);
try {
byte[] data = zkClient.getCuratorFramework().getData().forPath(keyPath);
List<AwsTask> taskList = taskSaver.queryAllTasks();
proMaps.put(key, new String(data));
for (AwsTask task : taskList) {
//
DEBUG.info("Load task {} from zk...", task.getJobName());
} catch (Exception e) {
e.printStackTrace();
}
taskScheduler.scheduleJob(task.buildJobDetail(), task.buildJobTrigger());
}
String jobName = proMaps.get(Constants.TASKNAME);
String jobGroup = proMaps.get(Constants.TASKGROUP);
JobType jobType = JobType.ONCE;
if (StringUtils.equals(proMaps.get(Constants.JOBTYPE), JobType.CRON.name())) {
jobType = JobType.CRON;
} else if (StringUtils.equals(proMaps.get(Constants.JOBTYPE), JobType.INTERVAL.name())) {
jobType = JobType.INTERVAL;
}
AwsJob awsJob = new AwsJob(jobName, jobGroup, jobType);
jobScheduler.scheduleJob(awsJob.buildJobDetail(), awsJob.buildJobTrigger());
}
}
... ...
package com.monitor.awstools.service;
import com.monitor.awstools.comp.JobSaver;
import com.monitor.awstools.comp.JobScheduler;
import com.monitor.awstools.comp.TaskSaver;
import com.monitor.awstools.comp.TaskScheduler;
import com.monitor.awstools.constant.Constants;
import com.monitor.awstools.constant.JobType;
import com.monitor.awstools.job.AwsJob;
import com.sun.javafx.collections.MappingChange;
import com.monitor.awstools.constant.TaskStatus;
import com.monitor.awstools.job.AwsTask;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.quartz.JobKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
... ... @@ -21,90 +23,64 @@ import java.util.Map;
public class TaskService {
@Autowired
JobSaver jobSaver;
TaskSaver taskSaver;
@Autowired
JobScheduler jobScheduler;
TaskScheduler taskScheduler;
public void addTask(String name, String group, JobType jobType, Map<String, String> proMaps) throws Exception {
proMaps.put(Constants.TASKNAME, name);
proMaps.put(Constants.TASKGROUP, group);
taskSaver.storeJob(proMaps);
jobSaver.storeJob(proMaps);
AwsJob job = new AwsJob(name, group, jobType);
jobScheduler.scheduleJob(job.buildJobDetail(), job.buildJobTrigger());
}
public void deleteTask(String name, String group) throws Exception {
JobKey jobKey = new JobKey(name, group);
jobScheduler.deleteJob(jobKey);
jobSaver.deleteJob(jobKey.toString());
taskSaver.deleteJob(jobKey.toString());
}
public void pauseTask(String name, String group) throws Exception {
JobKey jobKey = new JobKey(name, group);
Map<String, String> proMaps = new HashMap<>();
proMaps.put(Constants.TASKNAME, name);
String path= ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH,jobKey.toString()),Constants.TASKSTATUS);
proMaps.put(Constants.TASKGROUP, group);
taskSaver.configJob(path, TaskStatus.STOP.name());
proMaps.put(Constants.TASKSTATUS, "STOP");
jobSaver.configJob(proMaps);
jobScheduler.pauseJob(jobKey);
}
public void resumeTask(String name, String group) throws Exception {
JobKey jobKey = new JobKey(name, group);
Map<String, String> proMaps = new HashMap<>();
proMaps.put(Constants.TASKNAME, name);
proMaps.put(Constants.TASKGROUP, group);
String path= ZKPaths.makePath(ZKPaths.makePath(Constants.TASKPATH,jobKey.toString()),Constants.TASKSTATUS);
proMaps.put(Constants.TASKSTATUS, "START");
taskSaver.configJob(path, TaskStatus.START.name());
jobSaver.configJob(proMaps);
jobScheduler.resumeJob(jobKey);
}
public void configTask(String name, String group, Map<String, String> proMaps) throws Exception {
JobKey jobKey = new JobKey(name, group);
String jobTypeStr = proMaps.get(Constants.JOBTYPE);
JobType jobType = null;
//删除
taskSaver.deleteJob(jobKey.toString());
if (StringUtils.equals(jobTypeStr, JobType.CRON.name())) {
jobType = JobType.CRON;
//新增
taskSaver.storeJob(proMaps);
}
} else if (StringUtils.equals(jobTypeStr, JobType.INTERVAL.name())) {
public List<AwsTask> queryAllTasks() {
jobType = JobType.INTERVAL;
return taskSaver.queryAllTasks();
} else {
jobType = JobType.ONCE;
}
}
jobScheduler.deleteJob(jobKey);
public AwsTask queryTask(String name, String group) {
return taskSaver.queryTask(JobKey.jobKey(name, group).toString());
}
jobSaver.deleteJob(jobKey.toString());
addTask(name, group, jobType, proMaps);
}
}
... ...
... ... @@ -23,7 +23,7 @@
<!-- 打开aop 注解 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- Enables the Spring Task @Scheduled programming model -->
<!-- Enables the Spring AwsJobImpl @Scheduled programming model -->
<task:executor id="executor" pool-size="50"/>
<task:scheduler id="scheduler" pool-size="50"/>
<task:annotation-driven executor="executor" scheduler="scheduler"/>
... ...