优化打单产品分区管理功能,修复相关逻辑
This commit is contained in:
@@ -0,0 +1,159 @@
|
|||||||
|
package com.bms.price.task;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
import com.bms.common.utils.StringUtils;
|
||||||
|
import com.bms.price.search.ElasticsearchProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理 ES 业务日志(bms_biz_log)中超期文档。
|
||||||
|
*
|
||||||
|
* <p>纳入「系统监控 - 定时任务」:
|
||||||
|
* 调用目标 {@code esBizLogCleanupTask.cleanupOlderThanMonths(3)},
|
||||||
|
* 表示删除 3 个月前的数据;也可 {@code cleanup()} 默认 3 个月。</p>
|
||||||
|
*/
|
||||||
|
@Component("esBizLogCleanupTask")
|
||||||
|
public class EsBizLogCleanupTask
|
||||||
|
{
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(EsBizLogCleanupTask.class);
|
||||||
|
|
||||||
|
private static final int DEFAULT_MONTHS = 3;
|
||||||
|
private static final int MIN_MONTHS = 1;
|
||||||
|
private static final int MAX_MONTHS = 24;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ElasticsearchProperties properties;
|
||||||
|
|
||||||
|
/** 默认清理 3 个月前数据。 */
|
||||||
|
public void cleanup()
|
||||||
|
{
|
||||||
|
cleanupOlderThanMonths(Integer.valueOf(DEFAULT_MONTHS));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除早于(今天 - months 个月)的 ES 日志。
|
||||||
|
* Quartz:esBizLogCleanupTask.cleanupOlderThanMonths(3)
|
||||||
|
*/
|
||||||
|
public void cleanupOlderThanMonths(Integer months)
|
||||||
|
{
|
||||||
|
if (properties == null || !properties.isEnabled())
|
||||||
|
{
|
||||||
|
log.info("ES 未启用,跳过日志清理");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int keepMonths = months == null ? DEFAULT_MONTHS : months.intValue();
|
||||||
|
if (keepMonths < MIN_MONTHS)
|
||||||
|
{
|
||||||
|
keepMonths = MIN_MONTHS;
|
||||||
|
}
|
||||||
|
if (keepMonths > MAX_MONTHS)
|
||||||
|
{
|
||||||
|
keepMonths = MAX_MONTHS;
|
||||||
|
}
|
||||||
|
String cutoff = cutoffTime(keepMonths);
|
||||||
|
String index = StringUtils.isEmpty(properties.getIndex()) ? "bms_biz_log" : properties.getIndex().trim();
|
||||||
|
log.info("开始清理 ES 日志 index={} cutoff(<)={} keepMonths={}", index, cutoff, keepMonths);
|
||||||
|
|
||||||
|
RestTemplate rest = buildRestTemplate();
|
||||||
|
long deleted = deleteOlderThan(rest, index, cutoff);
|
||||||
|
log.info("ES 日志清理完成 index={} deleted={}", index, deleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long deleteOlderThan(RestTemplate rest, String index, String cutoff)
|
||||||
|
{
|
||||||
|
JSONObject body = new JSONObject();
|
||||||
|
body.put("query", buildOlderQuery(cutoff));
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
HttpEntity<String> entity = new HttpEntity<String>(body.toJSONString(), headers);
|
||||||
|
String url = baseUrl() + "/" + index + "/_delete_by_query?conflicts=proceed&refresh=true";
|
||||||
|
ResponseEntity<String> resp = rest.exchange(url, HttpMethod.POST, entity, String.class);
|
||||||
|
JSONObject root = JSON.parseObject(resp.getBody());
|
||||||
|
if (root == null)
|
||||||
|
{
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
return root.getLongValue("deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
private JSONObject buildOlderQuery(String cutoff)
|
||||||
|
{
|
||||||
|
JSONArray should = new JSONArray();
|
||||||
|
for (String field : new String[] { "updateTime.keyword", "updatedAt.keyword", "updateTime", "updatedAt" })
|
||||||
|
{
|
||||||
|
JSONObject rangeInner = new JSONObject();
|
||||||
|
JSONObject lt = new JSONObject();
|
||||||
|
lt.put("lt", cutoff);
|
||||||
|
rangeInner.put(field, lt);
|
||||||
|
JSONObject range = new JSONObject();
|
||||||
|
range.put("range", rangeInner);
|
||||||
|
should.add(range);
|
||||||
|
}
|
||||||
|
JSONObject bool = new JSONObject();
|
||||||
|
bool.put("should", should);
|
||||||
|
bool.put("minimum_should_match", Integer.valueOf(1));
|
||||||
|
JSONObject query = new JSONObject();
|
||||||
|
query.put("bool", bool);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String cutoffTime(int months)
|
||||||
|
{
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(new Date());
|
||||||
|
cal.add(Calendar.MONTH, -months);
|
||||||
|
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||||
|
cal.set(Calendar.MINUTE, 0);
|
||||||
|
cal.set(Calendar.SECOND, 0);
|
||||||
|
cal.set(Calendar.MILLISECOND, 0);
|
||||||
|
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
private RestTemplate buildRestTemplate()
|
||||||
|
{
|
||||||
|
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||||
|
factory.setConnectTimeout(10000);
|
||||||
|
// delete_by_query 可能较久
|
||||||
|
factory.setReadTimeout(600000);
|
||||||
|
RestTemplate template = new RestTemplate(factory);
|
||||||
|
if (StringUtils.isNotEmpty(properties.getUsername()))
|
||||||
|
{
|
||||||
|
String user = properties.getUsername();
|
||||||
|
String pass = properties.getPassword() == null ? "" : properties.getPassword();
|
||||||
|
template.getInterceptors().add((request, body, execution) -> {
|
||||||
|
request.getHeaders().setBasicAuth(user, pass);
|
||||||
|
return execution.execute(request, body);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String baseUrl()
|
||||||
|
{
|
||||||
|
String base = properties.getUris();
|
||||||
|
if (StringUtils.isEmpty(base))
|
||||||
|
{
|
||||||
|
base = "http://127.0.0.1:9200";
|
||||||
|
}
|
||||||
|
if (base.endsWith("/"))
|
||||||
|
{
|
||||||
|
base = base.substring(0, base.length() - 1);
|
||||||
|
}
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
-- Optional: register ES biz-log cleanup job (keep last 3 months)
|
||||||
|
-- Invoke: esBizLogCleanupTask.cleanupOlderThanMonths(3)
|
||||||
|
-- Cron: 03:00 on the 1st of every month
|
||||||
|
|
||||||
|
INSERT INTO sys_job (
|
||||||
|
job_name, job_group, invoke_target, cron_expression,
|
||||||
|
misfire_policy, concurrent, status, create_by, create_time, remark
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
'ES业务日志清理',
|
||||||
|
'DEFAULT',
|
||||||
|
'esBizLogCleanupTask.cleanupOlderThanMonths(3)',
|
||||||
|
'0 0 3 1 * ?',
|
||||||
|
'3',
|
||||||
|
'1',
|
||||||
|
'0',
|
||||||
|
'admin',
|
||||||
|
NOW(),
|
||||||
|
'删除 bms_biz_log 中 3 个月前的文档;ES 需在线'
|
||||||
|
FROM DUAL
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM sys_job
|
||||||
|
WHERE invoke_target = 'esBizLogCleanupTask.cleanupOlderThanMonths(3)'
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user