60 lines
2.0 KiB
C++
60 lines
2.0 KiB
C++
|
#include "CheckStorageThread.h"
|
||
|
#include "Constant.h"
|
||
|
#include "Log.h"
|
||
|
#include "Tool.h"
|
||
|
#include <QFile>
|
||
|
#include <QMutex>
|
||
|
#include <QWaitCondition>
|
||
|
|
||
|
// 磁盘剩余最小容量
|
||
|
#define THRESHOLD 10 * 1024 * 1024
|
||
|
// 检查间隔
|
||
|
#define INTERVAL 10 * 60 * 1000
|
||
|
|
||
|
extern QString curFilename;
|
||
|
extern QMutex mutex;
|
||
|
extern QWaitCondition condition;
|
||
|
|
||
|
CheckStorageThread::CheckStorageThread()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief check available storage each 10 min
|
||
|
*/
|
||
|
void CheckStorageThread::run()
|
||
|
{
|
||
|
while (true) {
|
||
|
int64_t available = Tool::getAvailableStorage(Constant::MountedPath);
|
||
|
if (available < THRESHOLD) {
|
||
|
Log::info("there are not enough storage, then remove some files...");
|
||
|
// get the file list
|
||
|
QStringList fileList = Tool::getFileList(QString("%1/%2").arg(Constant::VideoPath).arg(Constant::MainChannel));
|
||
|
if (!fileList.isEmpty()) {
|
||
|
// remove the first video file
|
||
|
QString filename = fileList.first();
|
||
|
// wait until file is not playing
|
||
|
mutex.lock();
|
||
|
if (filename == curFilename) {
|
||
|
Log::info("{} is playing, wait for play end and remove the files...", filename.toStdString());
|
||
|
condition.wait(&mutex);
|
||
|
Log::info("check thread end wait...");
|
||
|
}
|
||
|
mutex.unlock();
|
||
|
// start remove file
|
||
|
QString path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(Constant::MainChannel).arg(filename);
|
||
|
bool ret = Tool::removeFile(path);
|
||
|
if (!ret) {
|
||
|
Log::error("remove file {} failed", path.toStdString());
|
||
|
}
|
||
|
path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(Constant::SecondaryChannel).arg(filename);
|
||
|
ret = Tool::removeFile(path);
|
||
|
if (!ret) {
|
||
|
Log::error("remove file {} failed", path.toStdString());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
QThread::msleep(INTERVAL);
|
||
|
}
|
||
|
}
|