Skip to content

Commit bd00886

Browse files
committed
refactor local task
1 parent 7300a0c commit bd00886

24 files changed

+285
-201
lines changed

core/src/main/java/com/flowci/core/flow/controller/YmlController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.flowci.core.flow.service.FlowService;
2323
import com.flowci.core.flow.service.YmlService;
2424
import com.flowci.domain.http.RequestMessage;
25+
import com.flowci.tree.FlowNode;
2526
import com.flowci.tree.StepNode;
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.http.MediaType;
@@ -43,10 +44,10 @@ public class YmlController {
4344
@Autowired
4445
private YmlService ymlService;
4546

46-
@GetMapping("/{name}/yml/steps")
47-
public List<StepNode> listSteps(@PathVariable String name) {
47+
@GetMapping("/{name}/yml/obj")
48+
public FlowNode listSteps(@PathVariable String name) {
4849
Flow flow = flowService.get(name);
49-
return ymlService.ListChildren(flow);
50+
return ymlService.getRaw(flow);
5051
}
5152

5253
@PostMapping("/{name}/yml")

core/src/main/java/com/flowci/core/flow/domain/Flow.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import com.fasterxml.jackson.annotation.JsonIgnore;
2020
import com.flowci.core.common.domain.Mongoable;
2121
import com.flowci.core.common.domain.Variables;
22-
import com.flowci.domain.*;
22+
import com.flowci.domain.StringVars;
23+
import com.flowci.domain.TypedVars;
24+
import com.flowci.domain.VarValue;
25+
import com.flowci.domain.Vars;
2326
import com.flowci.exception.ArgumentException;
2427
import com.flowci.store.Pathable;
2528
import com.flowci.tree.NodePath;
@@ -28,8 +31,6 @@
2831
import org.springframework.data.mongodb.core.index.Indexed;
2932
import org.springframework.data.mongodb.core.mapping.Document;
3033

31-
import java.util.LinkedList;
32-
import java.util.List;
3334
import java.util.Objects;
3435

3536
/**
@@ -82,8 +83,6 @@ public enum Status {
8283

8384
private WebhookStatus webhookStatus;
8485

85-
private List<Notification> notifications = new LinkedList<>();
86-
8786
public Flow(String name) {
8887
this.name = name;
8988
}

core/src/main/java/com/flowci/core/flow/service/YmlService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.flowci.core.flow.domain.Flow;
2020
import com.flowci.core.flow.domain.Yml;
21+
import com.flowci.tree.FlowNode;
2122
import com.flowci.tree.Node;
2223
import com.flowci.tree.StepNode;
2324

@@ -29,9 +30,9 @@
2930
public interface YmlService {
3031

3132
/**
32-
* List all children node from YAML
33+
* Get FlowNode that represent yaml in obj
3334
*/
34-
List<StepNode> ListChildren(Flow flow);
35+
FlowNode getRaw(Flow flow);
3536

3637
/**
3738
* Get yml by flow

core/src/main/java/com/flowci/core/flow/service/YmlServiceImpl.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.flowci.core.flow.service;
1818

1919
import com.flowci.core.common.manager.SpringEventManager;
20-
import com.flowci.core.flow.dao.FlowDao;
2120
import com.flowci.core.flow.dao.YmlDao;
2221
import com.flowci.core.flow.domain.Flow;
2322
import com.flowci.core.flow.domain.Yml;
@@ -40,7 +39,9 @@
4039

4140
import java.io.IOException;
4241
import java.io.StringWriter;
43-
import java.util.*;
42+
import java.util.HashSet;
43+
import java.util.Optional;
44+
import java.util.Set;
4445

4546
/**
4647
* @author yang
@@ -54,9 +55,6 @@ public class YmlServiceImpl implements YmlService {
5455
@Autowired
5556
private YmlDao ymlDao;
5657

57-
@Autowired
58-
private FlowDao flowDao;
59-
6058
@Autowired
6159
private SpringEventManager eventManager;
6260

@@ -68,14 +66,12 @@ public class YmlServiceImpl implements YmlService {
6866
//====================================================================
6967

7068
@Override
71-
public List<StepNode> ListChildren(Flow flow) {
69+
public FlowNode getRaw(Flow flow) {
7270
Optional<Yml> optional = ymlDao.findById(flow.getId());
73-
if (!optional.isPresent()) {
74-
return Collections.emptyList();
71+
if (optional.isPresent()) {
72+
return YmlParser.load(flow.getName(), optional.get().getRaw());
7573
}
76-
77-
FlowNode root = YmlParser.load(flow.getName(), optional.get().getRaw());
78-
return root.getChildren();
74+
throw new NotFoundException("No yml defined for flow {0}", flow.getName());
7975
}
8076

8177
@Override
@@ -107,10 +103,6 @@ public Yml saveYml(Flow flow, String yml) {
107103
vars.clear();
108104
vars.merge(root.getEnvironments());
109105

110-
// set notifications from yml
111-
flow.setNotifications(root.getNotifications());
112-
flowDao.save(flow);
113-
114106
// update cron task
115107
cronService.update(flow, root, ymlObj);
116108
return ymlObj;

core/src/main/java/com/flowci/core/job/JobController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public class JobController {
7575
@Autowired
7676
private StepService stepService;
7777

78+
@Autowired
79+
private LocalTaskService localTaskService;
80+
7881
@Autowired
7982
private LoggingService loggingService;
8083

@@ -135,7 +138,7 @@ public List<ExecutedCmd> listSteps(@PathVariable String flow,
135138
public List<ExecutedLocalTask> listTasks(@PathVariable String flow,
136139
@PathVariable String buildNumberOrLatest) {
137140
Job job = get(flow, buildNumberOrLatest);
138-
return stepService.listTasks(job);
141+
return localTaskService.list(job);
139142
}
140143

141144
@GetMapping("/logs/{executedCmdId}")

core/src/main/java/com/flowci/core/job/dao/ExecutedLocalTaskDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import org.springframework.stereotype.Repository;
66

77
import java.util.List;
8+
import java.util.Optional;
89

910
@Repository
1011
public interface ExecutedLocalTaskDao extends MongoRepository<ExecutedLocalTask, String> {
1112

1213
List<ExecutedLocalTask> findAllByJobId(String jobId);
14+
15+
Optional<ExecutedLocalTask> findByJobIdAndAndName(String jobId, String name);
1316
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.flowci.core.job.domain;
2+
3+
import com.google.common.collect.ImmutableSet;
4+
import lombok.Getter;
5+
6+
import java.util.Date;
7+
import java.util.Set;
8+
9+
public interface Executed {
10+
11+
Set<Status> FailureStatus = ImmutableSet.of(
12+
Status.EXCEPTION,
13+
Status.KILLED,
14+
Status.TIMEOUT
15+
);
16+
17+
Set<Status> SuccessStatus = ImmutableSet.of(
18+
Status.SUCCESS,
19+
Status.SKIPPED
20+
);
21+
22+
enum Status {
23+
24+
PENDING(-1),
25+
26+
RUNNING(1),
27+
28+
SUCCESS(2),
29+
30+
SKIPPED(2),
31+
32+
EXCEPTION(3),
33+
34+
KILLED(3),
35+
36+
TIMEOUT(4);
37+
38+
@Getter
39+
private final Integer level;
40+
41+
Status(Integer level) {
42+
this.level = level;
43+
}
44+
}
45+
46+
Integer getCode();
47+
48+
Status getStatus();
49+
50+
String getContainerId();
51+
52+
Date getStartAt();
53+
54+
Date getFinishAt();
55+
56+
String getError();
57+
}

core/src/main/java/com/flowci/core/job/domain/ExecutedCmd.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@
2020
import com.flowci.domain.CmdBase;
2121
import com.flowci.domain.StringVars;
2222
import com.flowci.domain.Vars;
23-
import com.google.common.collect.ImmutableSet;
2423
import lombok.Data;
2524
import lombok.EqualsAndHashCode;
26-
import lombok.Getter;
2725
import lombok.NoArgsConstructor;
2826
import org.springframework.data.mongodb.core.index.CompoundIndex;
2927
import org.springframework.data.mongodb.core.mapping.Document;
3028

3129
import java.util.Date;
32-
import java.util.Set;
3330

34-
import static com.flowci.core.job.domain.ExecutedCmd.Status.*;
3531

3632
/**
3733
* ExecutedCmd == Step node with executed status and data
@@ -47,46 +43,7 @@
4743
def = "{'jobId': 1, 'nodePath': 1}",
4844
unique = true
4945
)
50-
public class ExecutedCmd extends CmdBase {
51-
52-
public final static Integer CODE_TIMEOUT = -100;
53-
54-
public final static Integer CODE_SUCCESS = 0;
55-
56-
private final static Set<Status> FailureStatus = ImmutableSet.of(
57-
EXCEPTION,
58-
KILLED,
59-
TIMEOUT
60-
);
61-
62-
private final static Set<Status> SuccessStatus = ImmutableSet.of(
63-
SUCCESS,
64-
SKIPPED
65-
);
66-
67-
public enum Status {
68-
69-
PENDING(-1),
70-
71-
RUNNING(1),
72-
73-
SUCCESS(2),
74-
75-
SKIPPED(2),
76-
77-
EXCEPTION(3),
78-
79-
KILLED(3),
80-
81-
TIMEOUT(4);
82-
83-
@Getter
84-
private Integer level;
85-
86-
Status(Integer level) {
87-
this.level = level;
88-
}
89-
}
46+
public class ExecutedCmd extends CmdBase implements Executed {
9047

9148
/**
9249
* Process id
@@ -142,11 +99,11 @@ public boolean isSuccess() {
14299

143100
@JsonIgnore
144101
public boolean isRunning() {
145-
return status == RUNNING;
102+
return status == Status.RUNNING;
146103
}
147104

148105
@JsonIgnore
149106
public boolean isPending() {
150-
return status == PENDING;
107+
return status == Status.PENDING;
151108
}
152109
}

core/src/main/java/com/flowci/core/job/domain/ExecutedLocalTask.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
package com.flowci.core.job.domain;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.flowci.util.StringHelper;
45
import lombok.Getter;
56
import lombok.NonNull;
67
import lombok.Setter;
8+
import org.springframework.data.mongodb.core.index.CompoundIndex;
79
import org.springframework.data.mongodb.core.index.Indexed;
810
import org.springframework.data.mongodb.core.mapping.Document;
911

12+
import java.util.Date;
13+
1014
@Setter
1115
@Getter
12-
@Document("local_task_result")
13-
public final class ExecutedLocalTask {
16+
@Document("executed_local_task")
17+
@CompoundIndex(
18+
name = "index_job_id_and_name",
19+
def = "{'jobId': 1, 'name': 1}",
20+
unique = true
21+
)
22+
public final class ExecutedLocalTask implements Executed {
1423

1524
private String id;
1625

17-
@NonNull
18-
private String name;
19-
2026
@NonNull
2127
@Indexed(name = "index_task_job_id")
2228
private String jobId;
2329

24-
private int code = -1;
30+
@NonNull
31+
private String name;
32+
33+
private Status status = Status.PENDING;
34+
35+
private Integer code;
2536

2637
private String containerId;
2738

28-
private String err;
39+
private String error;
40+
41+
private Date startAt;
42+
43+
private Date finishAt;
2944

45+
@JsonIgnore
3046
public boolean hasContainerId() {
3147
return StringHelper.hasValue(containerId);
3248
}

core/src/main/java/com/flowci/core/job/domain/Job.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.fasterxml.jackson.annotation.JsonIgnore;
2020
import com.flowci.core.common.domain.Mongoable;
2121
import com.flowci.core.common.domain.Variables;
22-
import com.flowci.domain.Notification;
2322
import com.flowci.domain.Agent;
2423
import com.flowci.domain.StringVars;
2524
import com.flowci.domain.Vars;
@@ -34,7 +33,9 @@
3433

3534
import java.text.SimpleDateFormat;
3635
import java.time.Instant;
37-
import java.util.*;
36+
import java.util.Date;
37+
import java.util.Objects;
38+
import java.util.Set;
3839

3940
/**
4041
* @author yang
@@ -243,11 +244,6 @@ public static Pathable path(Long buildNumber) {
243244
*/
244245
private Date finishAt;
245246

246-
/**
247-
* Executed as local task
248-
*/
249-
private List<Notification> notifications = new LinkedList<>();
250-
251247
@JsonIgnore
252248
public boolean isCancelled() {
253249
return status == Status.CANCELLED;

0 commit comments

Comments
 (0)