Skip to content

Commit

Permalink
fix bug(job add error)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfboys committed Nov 28, 2018
1 parent 9e8338d commit dee9565
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public String add(Agent agent) {
}
agent.setPassword(DigestUtils.md5Hex(agent.getPassword()));
agent.setStatus(Constants.ConnStatus.CONNECTED.getValue());
agentService.merge(agent);
agentService.saveOrUpdate(agent);
return "redirect:/agent/view.htm";
}

Expand Down Expand Up @@ -155,7 +155,7 @@ public Status edit(Agent agentParam) {
agent.setEmail(agentParam.getEmail());
}
agent.setComment(agentParam.getComment());
agentService.merge(agent);
agentService.saveOrUpdate(agent);
return Status.TRUE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Status checkName(Long jobId, Long agentId, String name) {
@ResponseBody
public Status checkDelete(Long id) {
boolean status = jobService.checkDelete(id);
return Status.create(status);
return Status.create(status);
}

@RequestMapping(value = "delete.do", method = RequestMethod.POST)
Expand All @@ -128,7 +128,7 @@ public String add(HttpSession session, Model model, Long id) {
}
List<Agent> agents = agentService.getOwnerAgents(session);
model.addAttribute("agents", agents);
model.addAttribute("execUser",userService.getExecUser(JobXTools.getUserId(session)));
model.addAttribute("execUser", userService.getExecUser(JobXTools.getUserId(session)));
return "/job/add";
}

Expand All @@ -145,13 +145,13 @@ public String addflow(HttpSession session, Model model, Long id) {

@RequestMapping(value = "search.do", method = RequestMethod.POST)
@ResponseBody
public PageBean<Job> search(HttpSession session, Long agentId,String jobName, Integer pageNo) {
public PageBean<Job> search(HttpSession session, Long agentId, String jobName, Integer pageNo) {
PageBean pageBean = new PageBean<JobBean>(6);
pageBean.setPageNo(pageNo == null?1:pageNo);
pageBean.setPageNo(pageNo == null ? 1 : pageNo);
if (agentId == null && CommonUtils.isEmpty(jobName)) {
return pageBean;
}
return jobService.search(session,pageBean,agentId,jobName);
return jobService.search(session, pageBean, agentId, jobName);
}

@RequestMapping(value = "save.do", method = RequestMethod.POST)
Expand Down Expand Up @@ -192,8 +192,8 @@ public String save(HttpSession session, Job jobParam, HttpServletRequest request
jobParam.setCreateType(Constants.CreateType.NORMAL.getValue());
jobParam.setToken(CommonUtils.uuid());
Agent agent = agentService.getAgent(jobParam.getAgentId());
if (agent!=null) {
if (agent.getPlatform() != 1 ) {
if (agent != null) {
if (agent.getPlatform() == null || agent.getPlatform() != 1) {
jobParam.setExecUser(null);
}
}
Expand Down Expand Up @@ -334,12 +334,12 @@ public Status jobIsRunning(Long jobId) {

@RequestMapping(value = "execute.do", method = RequestMethod.POST)
@ResponseBody
public Status remoteExecute(HttpSession session, Long id,String param) {
public Status remoteExecute(HttpSession session, Long id, String param) {
final Job job = jobService.getById(id);//找到要执行的任务
if (!jobService.checkJobOwner(session, job.getUserId())) return Status.FALSE;
//手动执行
Long userId = JobXTools.getUserId(session);
if(StringUtils.isNotEmpty(param)){
if (StringUtils.isNotEmpty(param)) {
job.setInputParam(param);
}
job.setUserId(userId);
Expand Down Expand Up @@ -383,7 +383,7 @@ public ParamsMap token(Long jobId) {
String token = CommonUtils.uuid();
if (job != null) {
job.setToken(CommonUtils.uuid());
jobService.updateToken(jobId,token);
jobService.updateToken(jobId, token);
}
return ParamsMap.map().set("token", token);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ public Status validateCronExp(String cronExp) {
public List<String> getRecentTriggerTime(String cronExp) {
return PageUtils.getRecentTriggerTime(cronExp);
}

/**
* 仅检查是否可以连接
* @return
*/
@RequestMapping(value = "ping.do", method = RequestMethod.POST)
@ResponseBody
public Map<String,Integer> validatePing(Agent agent) {
Map<String,Integer> result = new HashMap<String, Integer>(0);
if (hasProxy(agent, result)) return result;
Constants.ConnStatus connStatus = executeService.ping(agent,true);
Constants.ConnStatus connStatus = executeService.ping(agent,false);
result.put("status",connStatus.getValue());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public List<Agent> getOwnerByConnType(HttpSession session) {
List<AgentBean> list;
if (!JobXTools.isPermission(session)) {
User user = JobXTools.getUser(session);
list = agentDao.getByConnType(user.getUserId(),Constants.ConnStatus.CONNECTED.getValue());
}else {
list = agentDao.getByConnType(null,Constants.ConnStatus.CONNECTED.getValue());
list = agentDao.getByConnType(user.getUserId(), Constants.ConnStatus.CONNECTED.getValue());
} else {
list = agentDao.getByConnType(null, Constants.ConnStatus.CONNECTED.getValue());
}
return Lists.transform(list,Agent.transfer);
return Lists.transform(list, Agent.transfer);
}

public List<Agent> getAll() {
Expand All @@ -88,21 +88,22 @@ public List<Agent> getAll() {
private synchronized void flushLocalAgent() {
JobXTools.CACHE.put(
Constants.PARAM_CACHED_AGENT_KEY,
Lists.transform(agentDao.getAll(),Agent.transfer)
Lists.transform(agentDao.getAll(), Agent.transfer)
);
}

public int getCountByStatus(HttpSession session, Constants.ConnStatus status) {
Map<String,Object> map = new HashMap<String, Object>(0);
Map<String, Object> map = new HashMap<String, Object>(0);
if (!JobXTools.isPermission(session)) {
map.put("userId",JobXTools.getUserId(session));
map.put("userId", JobXTools.getUserId(session));
}
map.put("status",status.getValue());
map.put("status", status.getValue());
return agentDao.getCount(map);
}

/**
* 获取所属用户的agent
*
* @param session
* @param pageBean
*/
Expand All @@ -112,12 +113,12 @@ public void getPageBean(HttpSession session, Agent agent, PageBean pageBean) {
pageBean.put("userId", user.getUserId());
}
pageBean.put("agentName", agent.getName());
pageBean.put("status",agent.getStatus());
pageBean.put("status", agent.getStatus());
pageBean.verifyOrderBy("name", "name", "host", "port");
List<AgentBean> agentList = agentDao.getByPageBean(pageBean);
if (CommonUtils.notEmpty(agentList)) {
int count = agentDao.getCount(pageBean.getFilter());
List<Agent> agents = Lists.transform(agentList,Agent.transfer);
List<Agent> agents = Lists.transform(agentList, Agent.transfer);
pageBean.setResult(agents);
pageBean.setTotalCount(count);
}
Expand All @@ -131,12 +132,12 @@ public Agent getAgent(Long id) {
return null;
}

public void merge(Agent agent) {
public void saveOrUpdate(Agent agent) {
AgentBean agentBean = AgentBean.transfer.apply(agent);
if (agentBean.getAgentId() == null) {
agentDao.save(agentBean);
agent.setAgentId(agentBean.getAgentId());
}else {
} else {
agentDao.update(agentBean);
}
flushLocalAgent();
Expand All @@ -145,6 +146,7 @@ public void merge(Agent agent) {
/**
* true can delete
* false can't delete
*
* @param id
* @return
*/
Expand All @@ -165,19 +167,19 @@ public void delete(Long id) {
}

public boolean existsName(Long id, String name) {
return agentDao.existsCount(id,"name",name) > 0;
return agentDao.existsCount(id, "name", name) > 0;
}

public boolean existsHost(Long id, String host) {
return agentDao.existsCount(id,"host", host) > 0;
return agentDao.existsCount(id, "host", host) > 0;
}

public String editPassword(Long id, Boolean type, String pwd0, String pwd1, String pwd2) {
Agent agent = this.getAgent(id);
boolean verify;
if (type) {//直接输入的密钥
agent.setPassword(pwd0);
verify = executeService.ping(agent,false).equals(Constants.ConnStatus.CONNECTED);
verify = executeService.ping(agent, false).equals(Constants.ConnStatus.CONNECTED);
} else {//密码...
verify = DigestUtils.md5Hex(pwd0).equals(agent.getPassword());
}
Expand All @@ -187,7 +189,7 @@ public String editPassword(Long id, Boolean type, String pwd0, String pwd1, Stri
Boolean flag = executeService.password(agent, pwd1);
if (flag) {
agent.setPassword(pwd1);
executeService.ping(agent,true);
executeService.ping(agent, true);
return "true";
} else {
return "false";
Expand All @@ -205,15 +207,15 @@ public List<Agent> getOwnerAgents(HttpSession session) {
pageBean.setPageNo(0);
if (!JobXTools.isPermission(session)) {
User userDto = JobXTools.getUser(session);
pageBean.put("user_id",userDto.getUserId());
pageBean.put("user_id", userDto.getUserId());
}
List<AgentBean> agentList = agentDao.getByPageBean(pageBean);
return Lists.transform(agentList,Agent.transfer);
return Lists.transform(agentList, Agent.transfer);
}

public Agent getByMacId(String machineId) {
AgentBean agent = agentDao.getByMacId(machineId);
if (agent!=null) {
if (agent != null) {
return Agent.transfer.apply(agent);
}
return null;
Expand All @@ -223,17 +225,17 @@ public void doDisconnect(Agent agent) {
if (CommonUtils.isEmpty(agent.getNotifyTime()) || new Date().getTime() - agent.getNotifyTime().getTime() >= configService.getSysConfig().getSpaceTime() * 60 * 1000) {
noticeService.notice(agent);
//记录本次任务失败的时间
agentDao.updateNotifyTime(agent.getAgentId(),new Date());
agentDao.updateNotifyTime(agent.getAgentId(), new Date());
}
agentDao.updateStatus(agent.getAgentId(),Constants.ConnStatus.DISCONNECTED.getValue());
agentDao.updateStatus(agent.getAgentId(), Constants.ConnStatus.DISCONNECTED.getValue());
}

public void doDisconnect(String info) {
if (CommonUtils.notEmpty(info)) {
String macId = info.split("_")[0];
String password = info.split("_")[1];
String password = info.split("_")[1];
Agent agent = getByMacId(macId);
if ( CommonUtils.notEmpty(agent,password) && password.equals(agent.getPassword()) ) {
if (CommonUtils.notEmpty(agent, password) && password.equals(agent.getPassword())) {
doDisconnect(agent);
}
}
Expand All @@ -250,7 +252,7 @@ public void doConnect(String agentInfo) {
Agent agent = transfers.get(1);

//exists in db...
if ( agent!=null ) {
if (agent != null) {
//agent和server密码一致则连接...
if (registry.getPassword().equals(agent.getPassword())) {
executeService.ping(agent, true);
Expand All @@ -270,9 +272,9 @@ public void doConnect(String agentInfo) {
registry.setMobile(null);
registry.setEmail(null);
registry.setProxyId(null);
if (executeService.ping(registry,false).equals(Constants.ConnStatus.CONNECTED)) {
if (executeService.ping(registry, false).equals(Constants.ConnStatus.CONNECTED)) {
registry.setStatus(Constants.ConnStatus.CONNECTED.getValue());
merge(registry);
saveOrUpdate(registry);
}
}

Expand Down Expand Up @@ -304,13 +306,13 @@ private List<Agent> transfer(String registryInfo) {

public List<Agent> getByGroup(Long groupId) {
List<AgentBean> list = agentDao.getByGroup(groupId);
return Lists.transform(list,Agent.transfer);
return Lists.transform(list, Agent.transfer);
}

public void updateStatus(Agent agent) {
if (agent!=null) {
if (agent.getAgentId()!=null&&agent.getStatus()!=null) {
agentDao.updateStatus(agent.getAgentId(),agent.getStatus());
if (agent != null) {
if (agent.getAgentId() != null && agent.getStatus() != null) {
agentDao.updateStatus(agent.getAgentId(), agent.getStatus());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public Constants.ConnStatus ping(Agent agent, boolean update) {

if (update) {
agent.setStatus(status.getValue());
agentService.updateStatus(agent);
agentService.saveOrUpdate(agent);
}

return status;
Expand Down
23 changes: 13 additions & 10 deletions jobx-server/src/main/resources/mapper/AgentDao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,19 @@
<update id="update" parameterType="com.jobxhub.server.domain.AgentBean">
update t_agent
set
warning = #{warning},
comment = #{comment},
email = #{email},
mobile = #{mobile},
host = #{host},
machine_id = #{machineId},
`name` = #{name},
password = #{password},
port = #{port},
proxy_id = #{proxyId}
comment = #{comment},
email = #{email},
host = #{host},
platform = #{platform},
machine_id = #{machineId},
mobile = #{mobile},
`name` = #{name},
notify_time = #{notifyTime},
password = #{password},
port = #{port},
proxy_id = #{proxyId},
status = #{status},
warning = #{warning}
where agent_id = #{agentId}
</update>

Expand Down

0 comments on commit dee9565

Please sign in to comment.