TaskConfig.java
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package top.panll.assist.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import top.panll.assist.service.VideoFileService;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Component
@EnableScheduling
public class TaskConfig {
private final static Logger logger = LoggerFactory.getLogger(TaskConfig.class);
@Autowired
private VideoFileService videoFileService;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Scheduled(cron = "0 0 0 * * ?")
private void configureTasks() {
logger.info("录像过期自检任务执行");
List<File> appList = videoFileService.getAppList();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, - 7);
Date monday = calendar.getTime();
if (appList != null && appList.size() > 0) {
for (File appFile : appList) {
List<File> streamList = videoFileService.getStreamList(appFile.getName());
if (streamList != null && streamList.size() > 0) {
for (File streamFile : streamList) {
File[] recordDateFileList = streamFile.listFiles();
if (recordDateFileList != null && recordDateFileList.length > 0) {
for (File recordDateFile : recordDateFileList) {
try {
Date fileDaye = simpleDateFormat.parse(recordDateFile.getName());
if (fileDaye.before(monday)){
logger.debug("移除文件[{}]", recordDateFile.getAbsolutePath());
recordDateFile.delete();
}
} catch (ParseException e) {
logger.error("无法格式化[{}]为日期, 直接移除", recordDateFile.getName());
recordDateFile.delete();
}
}
}
if (streamFile.listFiles().length == 0) {
streamFile.delete();
}
}
}
if (appFile.listFiles().length == 0) {
appFile.delete();
}
}
}
}
}