项目中用到了一个开源音视频服务,但是同事的服务有可能导致开源服务崩溃,所以就写了一个实时监控开源服务输出日志的服务,如果日志中有error信息的话就自动重启那个开源服务。
不过后来还是把这部分屏蔽了,毕竟太简单粗暴,但是先分享一下代码。
这部分是随项目启动的部分。
@Component
public class FileWatcherRunner implements ApplicationRunner {
private final FileWatcherService fileWatcherService;
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
@Autowired
public FileWatcherRunner(FileWatcherService fileWatcherService) {
this.fileWatcherService = fileWatcherService;
}
@Override
public void run(ApplicationArguments args) throws Exception {
// 在应用程序启动时启动文件监控服务
String filePath = "/opt/yisuo/macaque/srs/std.log"; // 修改为要监控的文件路径
LOGGER.info("{} Start the monitoring file service", sdf.format(new Date()));
try {
fileWatcherService.startFileWatcher(filePath);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
这部分是具体的操作逻辑部分。
@Service
public class FileWatcherService {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private SRSApiController srsApiController;
public void startFileWatcher(String filePath) throws IOException, InterruptedException {
Path path = Paths.get(filePath);
Path dir = path.getParent();
WatchService watchService = FileSystems.getDefault().newWatchService();
dir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
Path fileName = (Path) event.context();
if (kind == StandardWatchEventKinds.ENTRY_MODIFY && fileName.equals(path.getFileName())) {
// 读取新生成的数据
readNewDataFromFile(path);
}
}
boolean valid = key.reset();
if (!valid) {
LOGGER.error("{} 无法继续监听目录,退出监控。", sdf.format(new Date()));
break;
}
}
}
private void readNewDataFromFile(Path filePath) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath.toFile()));
String line;
while ((line = reader.readLine()) != null) {
// 处理每行数据
if (line.contains("error code")) {
srsApiController.onCallback();
LOGGER.error("{} SRS line contains error,restart service", sdf.format(new Date()));
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}