Skip to content

Commit c9af6ff

Browse files
committed
refactor on init methods
1 parent 7e4468e commit c9af6ff

File tree

9 files changed

+65
-65
lines changed

9 files changed

+65
-65
lines changed

core/src/main/java/com/flowci/core/agent/service/AgentHostServiceImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import lombok.AllArgsConstructor;
4848
import lombok.extern.log4j.Log4j2;
4949
import org.springframework.beans.factory.annotation.Autowired;
50-
import org.springframework.context.event.ContextRefreshedEvent;
5150
import org.springframework.context.event.EventListener;
5251
import org.springframework.core.env.Environment;
5352
import org.springframework.core.task.TaskExecutor;
@@ -56,6 +55,7 @@
5655

5756
import javax.annotation.Nonnull;
5857
import javax.annotation.Nullable;
58+
import javax.annotation.PostConstruct;
5959
import java.nio.file.Files;
6060
import java.nio.file.Paths;
6161
import java.util.*;
@@ -114,6 +114,12 @@ public class AgentHostServiceImpl implements AgentHostService {
114114
mapping.put(K8sAgentHost.class, new K8sHostAdaptor());
115115
}
116116

117+
@PostConstruct
118+
public void init() {
119+
autoCreateLocalAgentHost();
120+
syncAgents();
121+
}
122+
117123
//====================================================================
118124
// %% Public functions
119125
//====================================================================
@@ -378,12 +384,6 @@ public void scheduleCollect() {
378384
// %% Internal events
379385
//====================================================================
380386

381-
@EventListener
382-
public void onContextReady(ContextRefreshedEvent event) {
383-
autoCreateLocalAgentHost();
384-
syncAgents();
385-
}
386-
387387
@EventListener
388388
public void onNoIdleAgent(NoIdleAgentEvent event) {
389389
Set<String> agentTags = event.getSelector().getLabel();

core/src/main/java/com/flowci/core/agent/service/AgentServiceImpl.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
import com.google.common.collect.Sets;
4343
import lombok.extern.log4j.Log4j2;
4444
import org.springframework.beans.factory.annotation.Autowired;
45-
import org.springframework.context.event.ContextRefreshedEvent;
4645
import org.springframework.context.event.EventListener;
4746
import org.springframework.dao.DuplicateKeyException;
4847
import org.springframework.stereotype.Service;
4948

49+
import javax.annotation.PostConstruct;
5050
import java.io.IOException;
5151
import java.time.Instant;
5252
import java.util.*;
@@ -108,7 +108,7 @@ public class AgentServiceImpl implements AgentService {
108108
@Autowired
109109
private SocketPushManager socketPushManager;
110110

111-
@EventListener(ContextRefreshedEvent.class)
111+
@PostConstruct
112112
public void initAgentStatus() {
113113
taskManager.run("init-agent-status", true, () -> {
114114
for (Agent agent : agentDao.findAll()) {
@@ -122,7 +122,7 @@ public void initAgentStatus() {
122122
});
123123
}
124124

125-
@EventListener(ContextRefreshedEvent.class)
125+
@PostConstruct
126126
public void subscribeIdleAgentQueue() throws IOException {
127127
idleAgentQueueManager.startConsumer(idleAgentQueue, false, (header, body, envelope) -> {
128128
String agentId = new String(body);
@@ -152,6 +152,22 @@ public void subscribeIdleAgentQueue() throws IOException {
152152
}, null);
153153
}
154154

155+
@PostConstruct
156+
public void lockNodeCleanup() {
157+
List<String> children = zk.children(zkProperties.getAgentRoot());
158+
for (String path : children) {
159+
String agentId = Util.getAgentIdFromLockPath(path);
160+
Optional<Agent> optional = agentDao.findById(agentId);
161+
162+
if (!optional.isPresent()) {
163+
try {
164+
zk.delete(path, true);
165+
} catch (Throwable ignore) {
166+
}
167+
}
168+
}
169+
}
170+
155171
//====================================================================
156172
// %% Public Methods
157173
//====================================================================
@@ -369,22 +385,6 @@ public void dispatch(CmdIn cmd, Agent agent) {
369385
// %% Spring Event Listener
370386
//====================================================================
371387

372-
@EventListener(ContextRefreshedEvent.class)
373-
public void lockNodeCleanup() {
374-
List<String> children = zk.children(zkProperties.getAgentRoot());
375-
for (String path : children) {
376-
String agentId = Util.getAgentIdFromLockPath(path);
377-
Optional<Agent> optional = agentDao.findById(agentId);
378-
379-
if (!optional.isPresent()) {
380-
try {
381-
zk.delete(path, true);
382-
} catch (Throwable ignore) {
383-
}
384-
}
385-
}
386-
}
387-
388388
@EventListener
389389
public void onConnected(OnConnectedEvent event) {
390390
Optional<InterLock> lock = lock();

core/src/main/java/com/flowci/core/common/manager/SocketPushManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
import com.flowci.core.common.rabbit.RabbitOperations;
2424
import lombok.extern.log4j.Log4j2;
2525
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.context.event.ContextRefreshedEvent;
27-
import org.springframework.context.event.EventListener;
2826
import org.springframework.messaging.simp.SimpMessagingTemplate;
2927
import org.springframework.stereotype.Component;
3028

29+
import javax.annotation.PostConstruct;
3130
import java.io.IOException;
3231
import java.util.HashMap;
3332
import java.util.Map;
@@ -56,7 +55,7 @@ public class SocketPushManager {
5655
@Autowired
5756
private RabbitOperations broadcastQueueManager;
5857

59-
@EventListener(ContextRefreshedEvent.class)
58+
@PostConstruct
6059
public void subscribeBroadcastQueue() throws IOException {
6160
broadcastQueueManager.startConsumer(wsBroadcastQueue, true, (headers, body, envelope) -> {
6261
try {

core/src/main/java/com/flowci/core/common/manager/SpringEventManagerImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.context.ApplicationEvent;
2929
import org.springframework.context.ApplicationEventPublisher;
30-
import org.springframework.context.event.ContextRefreshedEvent;
31-
import org.springframework.context.event.EventListener;
3230
import org.springframework.stereotype.Component;
3331

32+
import javax.annotation.PostConstruct;
3433
import java.io.IOException;
3534
import java.util.Map;
3635

@@ -55,7 +54,7 @@ public class SpringEventManagerImpl implements SpringEventManager {
5554
@Autowired
5655
private RabbitOperations broadcastQueueManager;
5756

58-
@EventListener(ContextRefreshedEvent.class)
57+
@PostConstruct
5958
public void subscribeBroadcastQueue() throws IOException {
6059
broadcastQueueManager.startConsumer(eventBroadcastQueue, true, (headers, body, envelope) -> {
6160
try {

core/src/main/java/com/flowci/core/config/service/ConfigServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import lombok.extern.log4j.Log4j2;
1717
import org.springframework.beans.factory.annotation.Autowired;
1818
import org.springframework.beans.factory.annotation.Value;
19-
import org.springframework.context.event.ContextRefreshedEvent;
2019
import org.springframework.context.event.EventListener;
2120
import org.springframework.core.io.Resource;
2221
import org.springframework.dao.DuplicateKeyException;
2322
import org.springframework.stereotype.Service;
2423

24+
import javax.annotation.PostConstruct;
2525
import java.io.IOException;
2626
import java.util.List;
2727
import java.util.Optional;
@@ -42,8 +42,8 @@ public class ConfigServiceImpl implements ConfigService {
4242
@Autowired
4343
private SpringEventManager eventManager;
4444

45-
@EventListener
46-
public void onInit(ContextRefreshedEvent ignore) {
45+
@PostConstruct
46+
public void onInit() {
4747
try {
4848
Config config = ConfigParser.parse(defaultSmtpConfigYml.getInputStream());
4949
Optional<Config> optional = configDao.findByName(config.getName());

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
import com.google.common.collect.Sets;
4949
import lombok.extern.log4j.Log4j2;
5050
import org.springframework.beans.factory.annotation.Autowired;
51-
import org.springframework.context.event.ContextRefreshedEvent;
5251
import org.springframework.context.event.EventListener;
5352
import org.springframework.dao.DuplicateKeyException;
5453
import org.springframework.stereotype.Service;
5554

55+
import javax.annotation.PostConstruct;
5656
import java.io.IOException;
5757
import java.util.*;
5858

@@ -93,6 +93,12 @@ public class FlowServiceImpl implements FlowService {
9393
@Autowired
9494
private AppProperties appProperties;
9595

96+
@PostConstruct
97+
public void initJobQueueForFlow() {
98+
List<Flow> all = flowDao.findAll();
99+
eventManager.publish(new FlowInitEvent(this, all));
100+
}
101+
96102
// ====================================================================
97103
// %% Public function
98104
// ====================================================================
@@ -309,12 +315,6 @@ public void removeUsers(Flow flow, String... emails) {
309315
// %% Internal events
310316
// ====================================================================
311317

312-
@EventListener
313-
public void initJobQueueForFlow(ContextRefreshedEvent ignore) {
314-
List<Flow> all = flowDao.findAll();
315-
eventManager.publish(new FlowInitEvent(this, all));
316-
}
317-
318318
@EventListener
319319
public void deleteUserFromFlow(UserDeletedEvent event) {
320320
// TODO:

core/src/main/java/com/flowci/core/job/service/JobActionServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
import groovy.util.ScriptException;
5454
import lombok.extern.log4j.Log4j2;
5555
import org.springframework.beans.factory.annotation.Autowired;
56-
import org.springframework.context.event.ContextRefreshedEvent;
5756
import org.springframework.context.event.EventListener;
5857
import org.springframework.stereotype.Service;
5958

59+
import javax.annotation.PostConstruct;
6060
import java.io.IOException;
6161
import java.nio.file.Files;
6262
import java.nio.file.Path;
@@ -163,8 +163,8 @@ public class JobActionServiceImpl implements JobActionService {
163163
@Autowired
164164
private StateMachine<JobSmContext> sm;
165165

166-
@EventListener
167-
public void init(ContextRefreshedEvent ignore) {
166+
@PostConstruct
167+
public void init() {
168168
try {
169169
fromPending();
170170
fromLoading();

core/src/main/java/com/flowci/core/job/service/JobEventServiceImpl.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
import com.flowci.tree.FlowNode;
4040
import lombok.extern.log4j.Log4j2;
4141
import org.springframework.beans.factory.annotation.Autowired;
42-
import org.springframework.context.event.ContextRefreshedEvent;
4342
import org.springframework.context.event.EventListener;
4443
import org.springframework.core.task.TaskExecutor;
4544
import org.springframework.stereotype.Service;
4645

46+
import javax.annotation.PostConstruct;
4747
import java.io.IOException;
4848
import java.util.Arrays;
4949

@@ -81,6 +81,20 @@ public class JobEventServiceImpl implements JobEventService {
8181
@Autowired
8282
private StepService stepService;
8383

84+
@PostConstruct
85+
public void startJobDeadLetterConsumer() throws IOException {
86+
String deadLetterQueue = rabbitProperties.getJobDlQueue();
87+
jobsQueueManager.startConsumer(deadLetterQueue, true, (header, body, envelope) -> {
88+
String jobId = new String(body);
89+
try {
90+
jobActionService.toTimeout(jobId);
91+
} catch (Exception e) {
92+
log.warn(e);
93+
}
94+
return false;
95+
}, null);
96+
}
97+
8498
//====================================================================
8599
// %% Internal events
86100
//====================================================================
@@ -178,24 +192,6 @@ public void handleCallback(ShellOut so) {
178192
jobActionService.toContinue(jobId, so);
179193
}
180194

181-
//====================================================================
182-
// %% Init events
183-
//====================================================================
184-
185-
@EventListener(value = ContextRefreshedEvent.class)
186-
public void startJobDeadLetterConsumer() throws IOException {
187-
String deadLetterQueue = rabbitProperties.getJobDlQueue();
188-
jobsQueueManager.startConsumer(deadLetterQueue, true, (header, body, envelope) -> {
189-
String jobId = new String(body);
190-
try {
191-
jobActionService.toTimeout(jobId);
192-
} catch (Exception e) {
193-
log.warn(e);
194-
}
195-
return false;
196-
}, null);
197-
}
198-
199195
//====================================================================
200196
// %% Utils
201197
//====================================================================

core/src/main/java/com/flowci/core/plugin/service/PluginServiceImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.scheduling.annotation.Scheduled;
4343
import org.springframework.stereotype.Component;
4444

45+
import javax.annotation.PostConstruct;
4546
import java.io.ByteArrayInputStream;
4647
import java.io.File;
4748
import java.io.IOException;
@@ -94,6 +95,11 @@ public class PluginServiceImpl implements PluginService {
9495

9596
private final Object reloadLock = new Object();
9697

98+
@PostConstruct
99+
public void init() {
100+
reload();
101+
}
102+
97103
@EventListener
98104
public void onGetPluginEvent(GetPluginEvent event) {
99105
try {
@@ -226,7 +232,7 @@ public void reload() {
226232
}
227233
}
228234

229-
@Scheduled(fixedRate = 1000 * 3600)
235+
@Scheduled(initialDelay = 1000 * 3600, fixedRate = 1000 * 3600)
230236
public void scheduleSync() {
231237
if (pluginProperties.getAutoUpdate()) {
232238
reload();

0 commit comments

Comments
 (0)