Skip to content

Commit

Permalink
refactor on init methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Aug 7, 2021
1 parent 7e4468e commit c9af6ff
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;
Expand All @@ -56,6 +55,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
Expand Down Expand Up @@ -114,6 +114,12 @@ public class AgentHostServiceImpl implements AgentHostService {
mapping.put(K8sAgentHost.class, new K8sHostAdaptor());
}

@PostConstruct
public void init() {
autoCreateLocalAgentHost();
syncAgents();
}

//====================================================================
// %% Public functions
//====================================================================
Expand Down Expand Up @@ -378,12 +384,6 @@ public void scheduleCollect() {
// %% Internal events
//====================================================================

@EventListener
public void onContextReady(ContextRefreshedEvent event) {
autoCreateLocalAgentHost();
syncAgents();
}

@EventListener
public void onNoIdleAgent(NoIdleAgentEvent event) {
Set<String> agentTags = event.getSelector().getLabel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
import com.google.common.collect.Sets;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
Expand Down Expand Up @@ -108,7 +108,7 @@ public class AgentServiceImpl implements AgentService {
@Autowired
private SocketPushManager socketPushManager;

@EventListener(ContextRefreshedEvent.class)
@PostConstruct
public void initAgentStatus() {
taskManager.run("init-agent-status", true, () -> {
for (Agent agent : agentDao.findAll()) {
Expand All @@ -122,7 +122,7 @@ public void initAgentStatus() {
});
}

@EventListener(ContextRefreshedEvent.class)
@PostConstruct
public void subscribeIdleAgentQueue() throws IOException {
idleAgentQueueManager.startConsumer(idleAgentQueue, false, (header, body, envelope) -> {
String agentId = new String(body);
Expand Down Expand Up @@ -152,6 +152,22 @@ public void subscribeIdleAgentQueue() throws IOException {
}, null);
}

@PostConstruct
public void lockNodeCleanup() {
List<String> children = zk.children(zkProperties.getAgentRoot());
for (String path : children) {
String agentId = Util.getAgentIdFromLockPath(path);
Optional<Agent> optional = agentDao.findById(agentId);

if (!optional.isPresent()) {
try {
zk.delete(path, true);
} catch (Throwable ignore) {
}
}
}
}

//====================================================================
// %% Public Methods
//====================================================================
Expand Down Expand Up @@ -369,22 +385,6 @@ public void dispatch(CmdIn cmd, Agent agent) {
// %% Spring Event Listener
//====================================================================

@EventListener(ContextRefreshedEvent.class)
public void lockNodeCleanup() {
List<String> children = zk.children(zkProperties.getAgentRoot());
for (String path : children) {
String agentId = Util.getAgentIdFromLockPath(path);
Optional<Agent> optional = agentDao.findById(agentId);

if (!optional.isPresent()) {
try {
zk.delete(path, true);
} catch (Throwable ignore) {
}
}
}
}

@EventListener
public void onConnected(OnConnectedEvent event) {
Optional<InterLock> lock = lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import com.flowci.core.common.rabbit.RabbitOperations;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -56,7 +55,7 @@ public class SocketPushManager {
@Autowired
private RabbitOperations broadcastQueueManager;

@EventListener(ContextRefreshedEvent.class)
@PostConstruct
public void subscribeBroadcastQueue() throws IOException {
broadcastQueueManager.startConsumer(wsBroadcastQueue, true, (headers, body, envelope) -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Map;

Expand All @@ -55,7 +54,7 @@ public class SpringEventManagerImpl implements SpringEventManager {
@Autowired
private RabbitOperations broadcastQueueManager;

@EventListener(ContextRefreshedEvent.class)
@PostConstruct
public void subscribeBroadcastQueue() throws IOException {
broadcastQueueManager.startConsumer(eventBroadcastQueue, true, (headers, body, envelope) -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
Expand All @@ -42,8 +42,8 @@ public class ConfigServiceImpl implements ConfigService {
@Autowired
private SpringEventManager eventManager;

@EventListener
public void onInit(ContextRefreshedEvent ignore) {
@PostConstruct
public void onInit() {
try {
Config config = ConfigParser.parse(defaultSmtpConfigYml.getInputStream());
Optional<Config> optional = configDao.findByName(config.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
import com.google.common.collect.Sets;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.*;

Expand Down Expand Up @@ -93,6 +93,12 @@ public class FlowServiceImpl implements FlowService {
@Autowired
private AppProperties appProperties;

@PostConstruct
public void initJobQueueForFlow() {
List<Flow> all = flowDao.findAll();
eventManager.publish(new FlowInitEvent(this, all));
}

// ====================================================================
// %% Public function
// ====================================================================
Expand Down Expand Up @@ -309,12 +315,6 @@ public void removeUsers(Flow flow, String... emails) {
// %% Internal events
// ====================================================================

@EventListener
public void initJobQueueForFlow(ContextRefreshedEvent ignore) {
List<Flow> all = flowDao.findAll();
eventManager.publish(new FlowInitEvent(this, all));
}

@EventListener
public void deleteUserFromFlow(UserDeletedEvent event) {
// TODO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
import groovy.util.ScriptException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -163,8 +163,8 @@ public class JobActionServiceImpl implements JobActionService {
@Autowired
private StateMachine<JobSmContext> sm;

@EventListener
public void init(ContextRefreshedEvent ignore) {
@PostConstruct
public void init() {
try {
fromPending();
fromLoading();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
import com.flowci.tree.FlowNode;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Arrays;

Expand Down Expand Up @@ -81,6 +81,20 @@ public class JobEventServiceImpl implements JobEventService {
@Autowired
private StepService stepService;

@PostConstruct
public void startJobDeadLetterConsumer() throws IOException {
String deadLetterQueue = rabbitProperties.getJobDlQueue();
jobsQueueManager.startConsumer(deadLetterQueue, true, (header, body, envelope) -> {
String jobId = new String(body);
try {
jobActionService.toTimeout(jobId);
} catch (Exception e) {
log.warn(e);
}
return false;
}, null);
}

//====================================================================
// %% Internal events
//====================================================================
Expand Down Expand Up @@ -178,24 +192,6 @@ public void handleCallback(ShellOut so) {
jobActionService.toContinue(jobId, so);
}

//====================================================================
// %% Init events
//====================================================================

@EventListener(value = ContextRefreshedEvent.class)
public void startJobDeadLetterConsumer() throws IOException {
String deadLetterQueue = rabbitProperties.getJobDlQueue();
jobsQueueManager.startConsumer(deadLetterQueue, true, (header, body, envelope) -> {
String jobId = new String(body);
try {
jobActionService.toTimeout(jobId);
} catch (Exception e) {
log.warn(e);
}
return false;
}, null);
}

//====================================================================
// %% Utils
//====================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -94,6 +95,11 @@ public class PluginServiceImpl implements PluginService {

private final Object reloadLock = new Object();

@PostConstruct
public void init() {
reload();
}

@EventListener
public void onGetPluginEvent(GetPluginEvent event) {
try {
Expand Down Expand Up @@ -226,7 +232,7 @@ public void reload() {
}
}

@Scheduled(fixedRate = 1000 * 3600)
@Scheduled(initialDelay = 1000 * 3600, fixedRate = 1000 * 3600)
public void scheduleSync() {
if (pluginProperties.getAutoUpdate()) {
reload();
Expand Down

0 comments on commit c9af6ff

Please sign in to comment.