package com.terra.proxy.schedule;
|
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.terra.proxy.bean.ResActionRecord;
|
import com.terra.proxy.bean.VistorBean;
|
import com.terra.proxy.properties.TerraProperties;
|
import com.terra.proxy.service.Impl.LogServiceImpl;
|
import com.terra.proxy.util.HttpUtils;
|
import com.terra.proxy.util.JedisUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
import redis.clients.jedis.Jedis;
|
|
import java.io.IOException;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
@Component
|
@Configuration
|
@EnableScheduling
|
public class schduletask {
|
private static Logger log = LoggerFactory.getLogger(schduletask.class);
|
|
@Autowired
|
private LogServiceImpl logService;
|
|
@Autowired
|
private TerraProperties properties;
|
|
@Value("${sys.jwt.expire}")
|
private Long expireSeconds;
|
|
/**
|
* 每隔一段时间(1 hour),将redis内的资源访问日志写入数据库
|
*/
|
@Scheduled(cron = "0 0 1 * * ?")
|
public void logwrite() {
|
int countNum = 5000;
|
Jedis jedis = JedisUtils.getJedis();
|
try {
|
Set<String> set = jedis.smembers("visitlog");
|
List<Map<String, Object>> list = new ArrayList<>();
|
Iterator<String> it = set.iterator();
|
while (it.hasNext()) {
|
String objstr = it.next();
|
JSONObject json = JSONUtil.parseObj(objstr);
|
Map<String, Object> map = json.toBean(Map.class);
|
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
Date date = null;
|
try {
|
date = sd.parse(map.get("date").toString());
|
} catch (ParseException e) {
|
log.error("日期转换错误");
|
e.printStackTrace();
|
}
|
map.put("timestamp", date);
|
list.add(map);
|
if (list.size() > countNum) {
|
logService.batchsavelog(list);
|
list.clear();
|
}
|
}
|
if (!list.isEmpty()) {
|
logService.batchsavelog(list);
|
}
|
jedis.del("visitlog");
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
jedis.close();
|
}
|
|
}
|
|
@Scheduled(cron = "0 0 2 * * ?")
|
public void jgptlogwrite() {
|
Jedis jedis = JedisUtils.getJedis();
|
try {
|
Set<String> set = jedis.smembers("TerraResActionRecordForJGPT");
|
List<Map<String, Object>> list = new ArrayList<>();
|
Iterator<String> it = set.iterator();
|
while (it.hasNext()) {
|
String objstr = it.next();
|
JSONObject json = JSONUtil.parseObj(objstr);
|
ResActionRecord map = json.toBean(ResActionRecord.class);
|
String url = properties.getProxy().getLogapipath() + "/actionrecord/adduseinfo";
|
Map<String, Object> params = new HashMap<>();
|
params.put("resourceid", map.getResourceid());
|
params.put("appid", map.getAppid());
|
params.put("userid", map.getUserid());
|
params.put("ip", map.getIp());
|
try {
|
HttpUtils.post(url, params);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
jedis.del("TerraResActionRecordForJGPT");
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
jedis.close();
|
}
|
|
}
|
|
|
@Scheduled(cron = "0 0 0 * * ?")
|
public void writeRecord() {
|
Jedis jedis = JedisUtils.getJedis();
|
try {
|
Set<String> set = jedis.smembers("TerraResActionRecord");
|
List<ResActionRecord> list = new ArrayList<>();
|
Iterator<String> it = set.iterator();
|
while (it.hasNext()) {
|
String objstr = it.next();
|
JSONObject json = JSONUtil.parseObj(objstr);
|
ResActionRecord map = json.toBean(ResActionRecord.class);
|
list.add(map);
|
}
|
if (!list.isEmpty()) {
|
logService.batchsaveResRecord(list);
|
}
|
jedis.del("TerraResActionRecord");
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
jedis.close();
|
}
|
|
}
|
|
/* 对于自动续约且到期时间不足一天的token进行续约
|
*续约时间
|
*每天凌晨0点完成token的自动续约
|
*
|
*/
|
@Scheduled(cron = "0 0 0 * * ?")
|
public void autoToken() {
|
Jedis jedis = JedisUtils.getJedis();
|
try {
|
Set<String> autoTokens = jedis.smembers("autoToken");
|
log.info("开始Token自动续约");
|
for (String autoToken : autoTokens) {
|
log.info("本次Token自动续约,共" + autoTokens.size() + "个数据");
|
String token = autoToken;
|
String value = jedis.get(token);
|
Integer ttl = Math.toIntExact(jedis.ttl(token));
|
if (ttl < expireSeconds) {
|
Integer integer = Integer.valueOf(value);
|
if (integer < expireSeconds) integer = Math.toIntExact(expireSeconds);
|
jedis.setex(token, ttl + integer, value);
|
}
|
}
|
log.info("Token自动续约完成");
|
} catch (Exception e) {
|
log.error("Token自动续约失败,请检查是否存在需要自动续约Token");
|
} finally {
|
jedis.close();
|
}
|
}
|
|
|
public static void main(String[] args) {
|
Date date = new Date();
|
System.out.println("date = " + date);
|
|
}
|
|
/**
|
* 每15分钟清理一次黑名单异常数据 移除黑名单但是统计数仍为1000的数据
|
*/
|
@Scheduled(cron = "0 */15 * * * ?")
|
public void releaseBlackList() {
|
Jedis jedis = JedisUtils.getJedis();
|
try {
|
HashMap<String, Object> map = new HashMap<>();
|
map.put("status","2");
|
List<VistorBean> list = logService.queryBlackLists(map);
|
for (VistorBean vistorBean : list) {
|
String requestip = vistorBean.getRequestip();
|
String s = jedis.get(requestip);
|
if(StringUtils.isNotEmpty(s)){
|
jedis.del(requestip);
|
log.info("本次清除异常ip为:"+requestip);
|
}
|
}
|
//15分钟释放一次特殊IP
|
String tempAllowBlackList = properties.getProxy().getTempAllowBlackList();
|
String[] split = tempAllowBlackList.split(";");
|
for (String s : split) {
|
jedis.del(s);
|
log.info("本次释放ip为:"+s);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
jedis.close();
|
}
|
}
|
|
}
|