Skip to content

Commit

Permalink
Merge pull request #142 from stelin/1.x
Browse files Browse the repository at this point in the history
🎨fixed style
  • Loading branch information
stelin authored Jul 30, 2023
2 parents 5c1aac1 + d5c6515 commit c3ec4a9
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
@AllArgsConstructor
@Getter
public enum JobInstanceStopEnum {

/**
* Normal stop
*/
NORMAL(1, "Normal stop"),

/**
* Timout stop
*/
TIMEOUT(2, "Timeout stop"),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
public abstract class AbstractChannel implements AlarmChannel {

/**
* Locale size
*/
protected static final Integer LOCALE_SIZE = 2;

/**
* Http client
*/
Expand Down Expand Up @@ -114,7 +119,7 @@ protected String getFeishuOrWebhookSign(String secret, Long timestamp) throws No
protected Locale getLocale(AlertRule alertRule) {
String locale = alertRule.getLocale();
String[] splitLocale = locale.split("_");
if (splitLocale.length != 2) {
if (splitLocale.length != LOCALE_SIZE) {
return Locale.US;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
* @since 1.0.6
*/
public interface AlarmChannel {

/**
* Send
*
* @param alarmDTO alarmDTO
*/
void send(AlarmDTO alarmDTO);

/**
* Channel type.
*
* @return AlertMethodEnum
*/
AlertMethodEnum channel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ public class FeishuChannel extends AbstractChannel {
/**
* Header color map
*/
private static final Map<String, String> COLOR_MAP = new HashMap<String, String>() {{
put(AlarmEventEnum.JOB_EXECUTE_FAIL.getEvent(), "red");
put(AlarmEventEnum.JOB_DISCARD.getEvent(), "yellow");
put(AlarmEventEnum.JOB_EXECUTE_TIMEOUT.getEvent(), "red");
put(AlarmEventEnum.JOB_REACH_RETRY_TIMES.getEvent(), "yellow");

put(AlarmEventEnum.DELAY_EXECUTE_FAIL.getEvent(), "red");
put(AlarmEventEnum.DELAY_EXECUTE_TIMEOUT.getEvent(), "red");
put(AlarmEventEnum.DELAY_TASK_IGNORE.getEvent(), "yellow");
}};
private static final Map<String, String> COLOR_MAP = new HashMap<String, String>() {
{
put(AlarmEventEnum.JOB_EXECUTE_FAIL.getEvent(), "red");
put(AlarmEventEnum.JOB_DISCARD.getEvent(), "yellow");
put(AlarmEventEnum.JOB_EXECUTE_TIMEOUT.getEvent(), "red");
put(AlarmEventEnum.JOB_REACH_RETRY_TIMES.getEvent(), "yellow");
put(AlarmEventEnum.DELAY_EXECUTE_FAIL.getEvent(), "red");
put(AlarmEventEnum.DELAY_EXECUTE_TIMEOUT.getEvent(), "red");
put(AlarmEventEnum.DELAY_TASK_IGNORE.getEvent(), "yellow");
}
};

/**
* Resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,40 @@
@Getter
@AllArgsConstructor
public enum AlarmEventEnum {

/**
* Job execute fail
*/
JOB_EXECUTE_FAIL("alarm.job.001", "Job execute failed"),

/**
* Job discard
*/
JOB_DISCARD("alarm.job.002", "Job discard"),

/**
* Job execute timeout
*/
JOB_EXECUTE_TIMEOUT("alarm.job.003", "Job execute timeout"),

/**
* Job reach retry times
*/
JOB_REACH_RETRY_TIMES("alarm.job.004", "Job reach retry times"),

/**
*
*/
DELAY_EXECUTE_FAIL("alarm.delay.001", "Delay execute failed"),

/**
* Delay execute timeout
*/
DELAY_EXECUTE_TIMEOUT("alarm.delay.002", "Delay execute timeout"),

/**
* Delay task ignore
*/
DELAY_TASK_IGNORE("alarm.delay.003", "Delay task ignore"),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public static synchronized void refreshDelayMap() {
log.info("Refresh alarm delay map success!");
}

/**
* Get alarm rules
*
* @param supplier supplier
* @return List
*/
public static synchronized List<AlertRule> getAlarmRules(Supplier<List<AlertRule>> supplier) {
if (CollectionUtils.isEmpty(ALARM_RULES)) {
ALARM_RULES.addAll(supplier.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public AlarmEventListener() {
consumer.start();
}

/**
* Alarm listener
*
* @param alarmEvent alarmEvent
*/
@EventListener
public void alarmListener(AlarmEvent alarmEvent) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.openjob.server.autoconfigure;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.openjob.server.httpclient.ApacheHttpClientConnectionManagerFactory;
import io.openjob.server.httpclient.ApacheHttpClientFactory;
import io.openjob.server.httpclient.DefaultApacheHttpClientConnectionManagerFactory;
Expand All @@ -8,14 +9,17 @@
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @author stelin [email protected]
Expand All @@ -24,10 +28,18 @@
@Configuration
@EnableConfigurationProperties(value = {HttpClientProperties.class})
public class HttpClientAutoConfiguration {
private final Timer connectionManagerTimer = new Timer(
"HttpClientAutoConfiguration.connectionManagerTimer", true);
private final ScheduledExecutorService scheduledService;
private CloseableHttpClient httpClient;

@Autowired
public HttpClientAutoConfiguration() {
this.scheduledService = new ScheduledThreadPoolExecutor(
1,
new ThreadFactoryBuilder().setNameFormat("Openjob-http-client-manager").build(),
new ThreadPoolExecutor.AbortPolicy()
);
}

@Bean
@ConditionalOnMissingBean
public ApacheHttpClientConnectionManagerFactory connManFactory() {
Expand All @@ -47,6 +59,9 @@ public ApacheHttpClientFactory apacheHttpClientFactory(
return new DefaultApacheHttpClientFactory(builder);
}

/**
* Connection manager
*/
@Bean
@ConditionalOnMissingBean
public HttpClientConnectionManager connectionManager(
Expand All @@ -58,15 +73,17 @@ public HttpClientConnectionManager connectionManager(
httpClientProperties.getMaxConnectionsPerRoute(),
httpClientProperties.getTimeToLive(),
httpClientProperties.getTimeToLiveUnit());
this.connectionManagerTimer.schedule(new TimerTask() {
@Override
public void run() {
connectionManager.closeExpiredConnections();
}
}, 30000, httpClientProperties.getConnectionTimerRepeat());


this.scheduledService.scheduleWithFixedDelay(connectionManager::closeExpiredConnections, 1000,
httpClientProperties.getConnectionTimerRepeat(), TimeUnit.MILLISECONDS);

return connectionManager;
}

/**
* Http client
*/
@Bean
@ConditionalOnMissingBean
public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory,
Expand All @@ -85,9 +102,12 @@ public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory,
return this.httpClient;
}

/**
* Destroy
*/
@PreDestroy
public void destroy() throws Exception {
this.connectionManagerTimer.cancel();
this.scheduledService.shutdown();
if (this.httpClient != null) {
this.httpClient.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* @author stelin [email protected]
* @since 1.0.6
*/
public interface ApacheHttpClientConnectionManagerFactory {String HTTP_SCHEME = "http";
public interface ApacheHttpClientConnectionManagerFactory {
String HTTP_SCHEME = "http";

/**
* Scheme for HTTPS based communication.
Expand All @@ -17,12 +18,13 @@ public interface ApacheHttpClientConnectionManagerFactory {String HTTP_SCHEME =

/**
* Creates a new {@link HttpClientConnectionManager}.
* @param disableSslValidation If true, SSL validation will be disabled.
* @param maxTotalConnections The total number of connections.
*
* @param disableSslValidation If true, SSL validation will be disabled.
* @param maxTotalConnections The total number of connections.
* @param maxConnectionsPerRoute The total number of connections per route.
* @param timeToLive The time a connection is allowed to exist.
* @param timeUnit The time unit for the time-to-live value.
* manager.
* @param timeToLive The time a connection is allowed to exist.
* @param timeUnit The time unit for the time-to-live value.
* manager.
* @return A new {@link HttpClientConnectionManager}.
*/
HttpClientConnectionManager newConnectionManager(boolean disableSslValidation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
* @since 1.0.6
*/
public interface ApacheHttpClientFactory {
/**
* Create builder
*
* @return HttpClientBuilder
*/
HttpClientBuilder createBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface TaskContainer {

/**
* Stop
* @param type type
*/
void stop(Integer type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class Task {
public Task() {
}

public Task(String taskId, Integer status) {
this.taskId = taskId;
this.status = status;
}

/**
* New task
*
* @param taskId taskId
* @param status status
* @param result result
*/
public Task(String taskId, Integer status, String result) {
this.taskId = taskId;
this.status = status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public WorkerInitializer() {
this.taskMasterManager = TaskMasterManager.INSTANCE;
}

/**
* Init
*/
public void init() {
// Initialize task master.
this.taskMasterManager.init();
Expand All @@ -26,6 +29,9 @@ public void init() {
this.delayManager.init();
}

/**
* Stop
*/
public void stop() {
this.taskMasterManager.stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public interface TaskMaster {

/**
* Stop
*
* @param type type
*/
void stop(Integer type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ private TaskMasterManager() {

}

/**
* Init
*/
public void init() {
// Already initialized
if (this.isInit.get()) {
Expand Down
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<module>openjob-common</module>
<module>openjob-worker</module>
<module>openjob-server</module>
<module>openjob-server/openjob-server-alarm</module>
</modules>

<licenses>
Expand Down

0 comments on commit c3ec4a9

Please sign in to comment.