Skip to content

Commit aec59cf

Browse files
author
Yang Guo
committed
job failure when agent offline
1 parent ee7c453 commit aec59cf

File tree

7 files changed

+70
-4
lines changed

7 files changed

+70
-4
lines changed

platform-api/src/main/java/com/flow/platform/api/controller/AgentController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.flow.platform.api.events.AgentStatusChangeEvent;
2323
import com.flow.platform.api.service.AgentService;
2424
import com.flow.platform.core.exception.IllegalParameterException;
25-
import com.flow.platform.core.service.ApplicationEventService;
2625
import com.flow.platform.domain.Agent;
2726
import com.flow.platform.domain.AgentPath;
2827
import com.flow.platform.domain.AgentSettings;
@@ -42,7 +41,7 @@
4241
*/
4342
@RestController
4443
@RequestMapping(path = "/agents")
45-
public class AgentController extends ApplicationEventService {
44+
public class AgentController {
4645

4746
@Autowired
4847
private AgentService agentService;
@@ -193,7 +192,7 @@ public BooleanValue shutDown(@RequestParam String zone,
193192
*/
194193
@PostMapping(path = "/callback")
195194
public void callback(@RequestBody Agent agent) {
196-
this.dispatchEvent(new AgentStatusChangeEvent(this, agent));
195+
agentService.onAgentStatusChange(agent);
197196
}
198197

199198
/**

platform-api/src/main/java/com/flow/platform/api/dao/job/JobDao.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public interface JobDao extends BaseDao<BigInteger, Job> {
6969
*/
7070
Job get(String path, Integer number);
7171

72+
/**
73+
* get job by session id
74+
*/
75+
Job get(String sessionId);
76+
7277
/**
7378
* get max build number for node path
7479
*/

platform-api/src/main/java/com/flow/platform/api/dao/job/JobDaoImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public Job get(BigInteger key) {
8787
});
8888
}
8989

90+
91+
9092
@Override
9193
public List<Job> latestByPath(List<String> paths) {
9294
return execute((Session session) -> {
@@ -163,6 +165,13 @@ public Job get(String path, Integer number) {
163165
});
164166
}
165167

168+
@Override
169+
public Job get(String sessionId) {
170+
return execute(session -> session.createQuery("from Job where sessionId = :sessionId", Job.class)
171+
.setParameter("sessionId", sessionId)
172+
.uniqueResult());
173+
}
174+
166175
@Override
167176
public Integer maxBuildNumber(String path) {
168177
return execute((Session session) -> {

platform-api/src/main/java/com/flow/platform/api/service/AgentService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public interface AgentService {
5858
* @param agentPath required
5959
*/
6060
void sendSysCmd(AgentPath agentPath);
61+
62+
/**
63+
* Handle agent status call back from cc
64+
*/
65+
void onAgentStatusChange(Agent agent);
6166
}

platform-api/src/main/java/com/flow/platform/api/service/AgentServiceImpl.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919
import com.flow.platform.api.dao.job.JobDao;
2020
import com.flow.platform.api.domain.AgentWithFlow;
2121
import com.flow.platform.api.domain.job.Job;
22+
import com.flow.platform.api.domain.job.JobStatus;
23+
import com.flow.platform.api.domain.job.NodeResult;
2224
import com.flow.platform.api.domain.job.NodeStatus;
25+
import com.flow.platform.api.events.AgentStatusChangeEvent;
2326
import com.flow.platform.api.service.job.CmdService;
27+
import com.flow.platform.api.service.job.JobService;
28+
import com.flow.platform.api.service.job.NodeResultService;
2429
import com.flow.platform.api.util.PlatformURL;
2530
import com.flow.platform.core.exception.HttpException;
2631
import com.flow.platform.core.exception.IllegalStatusException;
32+
import com.flow.platform.core.service.ApplicationEventService;
2733
import com.flow.platform.domain.Agent;
2834
import com.flow.platform.domain.AgentPath;
2935
import com.flow.platform.domain.AgentPathWithWebhook;
@@ -53,7 +59,7 @@
5359
*/
5460

5561
@Service
56-
public class AgentServiceImpl implements AgentService {
62+
public class AgentServiceImpl extends ApplicationEventService implements AgentService {
5763

5864
private final Logger LOGGER = new Logger(AgentService.class);
5965

@@ -71,6 +77,12 @@ public class AgentServiceImpl implements AgentService {
7177
@Autowired
7278
private CmdService cmdService;
7379

80+
@Autowired
81+
private JobService jobService;
82+
83+
@Autowired
84+
private NodeResultService nodeResultService;
85+
7486
@Value(value = "${domain}")
7587
private String domain;
7688

@@ -228,4 +240,30 @@ private Agent findAgent(AgentPath agentPath) {
228240
return agent;
229241
}
230242

243+
@Override
244+
public void onAgentStatusChange(Agent agent) {
245+
this.dispatchEvent(new AgentStatusChangeEvent(this, agent));
246+
247+
// do not check related job if agent status not offline
248+
if (agent.getStatus() != AgentStatus.OFFLINE) {
249+
return;
250+
}
251+
252+
// find related job and set job to failure
253+
String sessionId = agent.getSessionId();
254+
if (Strings.isNullOrEmpty(sessionId)) {
255+
return;
256+
}
257+
258+
// find agent related job by session id
259+
Job job = jobService.find(sessionId);
260+
if (job == null) {
261+
return;
262+
}
263+
264+
if (Job.RUNNING_STATUS.contains(job.getStatus())) {
265+
job.setFailureMessage(String.format("Agent %s is offline when job running", agent.getPath()));
266+
jobService.updateJobStatusAndSave(job, JobStatus.FAILURE);
267+
}
268+
}
231269
}

platform-api/src/main/java/com/flow/platform/api/service/job/JobService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public interface JobService {
4545
*/
4646
Job find(BigInteger jobId);
4747

48+
/**
49+
* Find by agent session id
50+
*/
51+
Job find(String sessionId);
52+
4853
/**
4954
* Get job yml content
5055
*/

platform-api/src/main/java/com/flow/platform/api/service/job/JobServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public Job find(BigInteger jobId) {
146146
return find(job);
147147
}
148148

149+
@Override
150+
public Job find(String sessionId) {
151+
return jobDao.get(sessionId);
152+
}
153+
149154
@Override
150155
public String findYml(String path, Integer number) {
151156
Job job = find(path, number);

0 commit comments

Comments
 (0)