添加定时删除功能

This commit is contained in:
孙小云 2025-01-07 18:38:21 +08:00
parent 6f9cb77402
commit b050cee8bd
2 changed files with 36 additions and 4 deletions

View File

@ -8,6 +8,9 @@ import com.tuoheng.steam.util.TimeUtils;
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.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@ -18,8 +21,35 @@ import java.util.*;
import java.util.concurrent.*;
@Service
@EnableScheduling
public class TaskService implements ITaskService{
@Value("${srs.targetPath}")
private String targetPath;
/**
* 清除没有被关闭的进程
*/
@Scheduled(fixedRate = 60000)
public void cleaTask() {
for (ConcurrentHashMap.Entry<String, StreamTask> entry : runningTasks.entrySet()) {
StreamTask streamTask = entry.getValue();
if(streamTask.getStartTime().getTime() < new Date().getTime() - 60 * 20 *60 ) {
StreamTask s = stopTask(entry.getKey());
scheduler.schedule(() -> {
File file = new File(targetPath +"/"+ s.getOutFileName());
try {
file.delete();
logger.info("废弃文件删除成功");
}catch (Exception e) {
logger.error(e.getMessage());
}
}, 60, TimeUnit.SECONDS);
}
}
}
private static final Logger logger = LoggerFactory.getLogger(TaskService.class);
@Autowired
@ -38,7 +68,7 @@ public class TaskService implements ITaskService{
/**
* 任务池
*/
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
@PostConstruct
public void init() {
@ -96,7 +126,7 @@ public class TaskService implements ITaskService{
if (runningTasks.putIfAbsent(streamUrl, taskInstance) == null) {
logger.info("streamUrl {} startTask {} ", streamUrl, taskInstance.getTaskId());
startTask(streamUrl, taskInstance);
runningTasks.put(streamUrl, taskInstance);
// runningTasks.put(streamUrl, taskInstance);
return taskInstance;
} else {
return runningTasks.get(streamUrl);

View File

@ -1,4 +1,6 @@
spring.application.name=demo
server.port = 9011
server.port = 8080
srs.splitPath=/data/java/srs/stream_server/temp
srs.targetPath=/data/java/srs/srs/trunk/objs/nginx/html
srs.targetPath=/data/java/srs/srs/trunk/objs/nginx/html
#srs.splitPath=/Users/sunpeng/workspace/stream/temp
#srs.targetPath=/Users/sunpeng/workspace/stream/html