package com.example.unusualsounds.project.vox.task; import com.example.unusualsounds.project.vox.config.VideoTaskManager; import com.example.unusualsounds.project.vox.entity.VoiceTicketLogs; import com.example.unusualsounds.project.vox.service.impl.VoiceTicketLogsServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CacheEvict; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.*; import java.util.stream.Collectors; /** * 工单定时任务 * --- * 用于应对边缘测网络问题,主要针对定时关闭问题 * 1. 算法启接口调用后,将工单存入表中 * 2. 定时查询本地存入的工单,主要侦测当前停止的工单任务是否再运行 * 3. 只添加算法停接口因网络问题未收到停止的请求 * 4. 忽略因网络问题未收到启动的请求,所以定时查询是自己表内已经插入的数据 * */ @Component @Slf4j public class TicketsTask { @Autowired private VideoTaskManager videoTaskManager; @Autowired private VoiceTicketLogsServiceImpl voiceTicketLogsServiceImpl; @Value("${vox.video-dir}") private String videoDir; @Scheduled(cron = "0 */10 * * * ?") public void checkStopTickets() { List strings = this.stopTickets(); if (strings != null && strings.size() > 0) { strings.forEach(s -> { try { videoTaskManager.stopTask(s); } catch (InterruptedException e) { throw new RuntimeException(e); } }); }else { log.info("定时停止工单:无"); } } /** * 停止到时间未停止的工单 * @return */ public List stopTickets() { List taskStatuses = videoTaskManager.listTasks(); if(taskStatuses == null || taskStatuses.size() == 0) return null; //openUuid,streamId Map taskStatusesList = new HashMap<>(); taskStatuses.forEach( task ->{ taskStatusesList.put(task.getOpenUuid(),task.getStreamId()); } ); List delTasks = voiceTicketLogsServiceImpl.contrastTasks(taskStatusesList); delTasks.forEach(str -> { log.info("需要停止的工单:{}",str); }); List collect = delTasks.stream().map( task -> { return task.getDeviceUuid() + "_" + task.getSkillName(); }) .distinct().collect(Collectors.toList()); return collect; } }