Skip to content

Commit

Permalink
fix 修复训练营视频下载完成后,重命名失败问题 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetInk committed Jun 27, 2021
1 parent cc627ea commit 915caea
Showing 1 changed file with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import cn.hutool.http.HttpRequest;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import online.githuboy.lagou.course.support.ExecutorService;
import online.githuboy.lagou.course.support.MediaLoader;
import online.githuboy.lagou.course.support.Mp4History;
import online.githuboy.lagou.course.support.Stats;
import online.githuboy.lagou.course.support.*;
import online.githuboy.lagou.course.utils.ConfigUtil;
import online.githuboy.lagou.course.utils.FileUtils;

Expand All @@ -21,15 +18,17 @@
* @since 2021年5月20日
*/
@Slf4j
public class BigCourseMp4Downloader implements Runnable, NamedTask, MediaLoader {
public class BigCourseMp4Downloader extends AbstractRetryTask implements NamedTask, MediaLoader {

private final static int MAX_RETRY_COUNT = 3;
private final String videoName;
private final String lessonId;
private final String playUrl;
private volatile int retryCount = 0;
private int retryCount = 0;
@Setter
private String basePath;
private CountDownLatch fileDownloadFinishedLatch;


@Setter
private CountDownLatch latch;
Expand All @@ -42,10 +41,11 @@ public BigCourseMp4Downloader(String videoName, String lessonId, String playUrl)
}

@Override
public void run() {
try {
if (this.playUrl != null) {
File mp4File = new File(basePath, "[" + lessonId + "] " + FileUtils.getCorrectFileName(videoName) + ".!mp4");
protected void action() {
if (this.playUrl != null) {
File mp4File = new File(basePath, "[" + lessonId + "] " + FileUtils.getCorrectFileName(videoName) + ".!mp4");
fileDownloadFinishedLatch = new CountDownLatch(1);
try {
HttpRequest.get(this.playUrl).timeout(Integer.parseInt(ConfigUtil.readValue("mp4_download_timeout")) * 60 * 1000).execute(true).writeBody(mp4File, new StreamProgress() {
@Override
public void start() {
Expand All @@ -61,35 +61,51 @@ public void progress(long l) {

@Override
public void finish() {
Stats.remove(videoName);
Mp4History.append(lessonId);
latch.countDown();
long count = latch.getCount();
FileUtils.replaceFileName(mp4File, ".!mp4", ".mp4");
log.info("====>视频下载完成【{}】,耗时:{} s,剩余{}", videoName, (System.currentTimeMillis() - startTime) / 1000, count);
fileDownloadFinishedLatch.countDown();
}
});

} else {
log.warn("没有获取到视频【{}】播放地址:", videoName);
latch.countDown();
}
} catch (Exception e) {
log.error("获取视频:{}信息失败:", videoName, e);
if (retryCount < MAX_RETRY_COUNT) {
Stats.incr(videoName);
retryCount += 1;
log.info("第:{}次重试获取:{}", retryCount, videoName);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
ExecutorService.execute(this);
} else {
log.info(" video:{}最大重试结束:{}", videoName, MAX_RETRY_COUNT);
latch.countDown();
fileDownloadFinishedLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Thread interrupted", e);
} finally {
fileDownloadFinishedLatch.countDown();
}
Stats.remove(videoName);
Mp4History.append(lessonId);
latch.countDown();
long count = latch.getCount();
FileUtils.replaceFileName(mp4File, ".!mp4", ".mp4");
log.info("====>视频下载完成【{}】,耗时:{} s,剩余{}", videoName, (System.currentTimeMillis() - startTime) / 1000, count);
} else {
log.warn("没有获取到视频【{}】播放地址:", videoName);
latch.countDown();
}
}

@Override
protected void retry(Throwable throwable) {
super.retry(throwable);
Stats.incr(videoName);
retryCount += 1;
log.info("第:{}次重试获取:{}", retryCount, videoName, throwable);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
ExecutorService.execute(this);
}

@Override
public void retryComplete() {
super.retryComplete();
log.error(" video:{}最大重试结束:{}", videoName, MAX_RETRY_COUNT);
latch.countDown();
}

@Override
public boolean canRetry() {
return retryCount < MAX_RETRY_COUNT;
}

}

0 comments on commit 915caea

Please sign in to comment.