|
@@ -38,7 +38,12 @@ public class MatchVideoServiceImpl {
|
|
|
|
|
|
private static final String CRAWLER_LOCK_KEY = "crawler_lock_key_%s";
|
|
|
|
|
|
- private static final String CRAWLER_COUNT_KEY = "crawler_count_key_%s";
|
|
|
+ private static final String UPLOAD_CRAWLER_LOCK_KEY = "upload_crawler_lock_key_%s";
|
|
|
+
|
|
|
+ private static final String CRAWLER_FAIL_COUNT_KEY = "crawler_count_key_%s";
|
|
|
+
|
|
|
+ private static final String UPLOAD_CRAWLER_FAIL_COUNT_KEY = "upload_crawler_count_key_%s";
|
|
|
+
|
|
|
|
|
|
@Autowired
|
|
|
KimiService kimiService;
|
|
@@ -70,8 +75,6 @@ public class MatchVideoServiceImpl {
|
|
|
|
|
|
// 定义一个阻塞队列
|
|
|
private static final ArrayBlockingQueue<MatchVideo> matchKimiVideoQueue = new ArrayBlockingQueue<>(100000);
|
|
|
-
|
|
|
-
|
|
|
private static final int size = 5;
|
|
|
// 定义一个线程池,设置消费线程的数量
|
|
|
private static final ThreadPoolExecutor matchKimiVideoPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(size);
|
|
@@ -79,10 +82,14 @@ public class MatchVideoServiceImpl {
|
|
|
|
|
|
// 定义一个阻塞队列
|
|
|
private static final ArrayBlockingQueue<MatchVideo> matchCrawlerVideoQueue = new ArrayBlockingQueue<>(100000);
|
|
|
-
|
|
|
// 定义一个线程池,设置消费线程的数量
|
|
|
private static final ThreadPoolExecutor matchCrawlerVideoPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(size);
|
|
|
|
|
|
+ // 定义一个阻塞队列
|
|
|
+ private static final ArrayBlockingQueue<MatchVideo> uploadCrawlerVideoQueue = new ArrayBlockingQueue<>(100000);
|
|
|
+ // 定义一个线程池,设置消费线程的数量
|
|
|
+ private static final ThreadPoolExecutor uploadCrawlerVideoPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(size);
|
|
|
+
|
|
|
@Transactional
|
|
|
public void addMatchVideo(MatchContent matchContent) {
|
|
|
MatchVideoExample example = new MatchVideoExample();
|
|
@@ -156,7 +163,7 @@ public class MatchVideoServiceImpl {
|
|
|
example.setOrderByClause("id asc");
|
|
|
Page<Object> page = new Page<>();
|
|
|
page.setCurrentPage(1);
|
|
|
- page.setPageSize(5000);
|
|
|
+ page.setPageSize(1000);
|
|
|
example.setPage(page);
|
|
|
matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
boolean flag = true;
|
|
@@ -186,7 +193,7 @@ public class MatchVideoServiceImpl {
|
|
|
example.setOrderByClause("id asc");
|
|
|
Page<Object> page = new Page<>();
|
|
|
page.setCurrentPage(1);
|
|
|
- page.setPageSize(5000);
|
|
|
+ page.setPageSize(1000);
|
|
|
example.setPage(page);
|
|
|
matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
if (CollectionUtils.isEmpty(matchVideos)) {
|
|
@@ -255,16 +262,15 @@ public class MatchVideoServiceImpl {
|
|
|
}));
|
|
|
}
|
|
|
List<MatchVideo> matchVideos;
|
|
|
- List<Integer> status = Arrays.asList(1, 2);
|
|
|
Integer id = (Integer) redisTemplate.opsForValue().get("last_match_video_id");
|
|
|
if (id != null) {
|
|
|
do {
|
|
|
MatchVideoExample example = new MatchVideoExample();
|
|
|
- example.createCriteria().andIdGreaterThan(id).andContentStatusIn(status);
|
|
|
+ example.createCriteria().andIdGreaterThan(id).andContentStatusEqualTo(1);
|
|
|
example.setOrderByClause("id asc");
|
|
|
Page<Object> page = new Page<>();
|
|
|
page.setCurrentPage(1);
|
|
|
- page.setPageSize(5000);
|
|
|
+ page.setPageSize(1000);
|
|
|
example.setPage(page);
|
|
|
matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
if (CollectionUtils.isEmpty(matchVideos)) {
|
|
@@ -280,7 +286,7 @@ public class MatchVideoServiceImpl {
|
|
|
|
|
|
|
|
|
public void processCrawlerMatchContent(MatchVideo matchVideo) {
|
|
|
- if (matchVideo.getContentStatus() != 1 && matchVideo.getContentStatus() != 2) {
|
|
|
+ if (matchVideo.getContentStatus() != 1) {
|
|
|
return;
|
|
|
}
|
|
|
boolean existCrawlerVideo = crawlerVideoService.existCrawlerVideo(matchVideo.getContentId());
|
|
@@ -309,10 +315,10 @@ public class MatchVideoServiceImpl {
|
|
|
String rootContentId = getRootContentId(matchVideo.getContentId());
|
|
|
boolean res = crawlerVideoService.addCrawlerVideo(matchVideo.getContentId(), rootContentId, kimiText);
|
|
|
if (res) {
|
|
|
- updateStatus(matchVideo.getId(), ContentStatusEnum.SUCCESS_3.getStatusCode());
|
|
|
+ updateStatus(matchVideo.getId(), ContentStatusEnum.CRAWLER_SUCCESS.getStatusCode());
|
|
|
} else {
|
|
|
//匹配失败记录
|
|
|
- String countKey = String.format(CRAWLER_COUNT_KEY, matchVideo.getContentId());
|
|
|
+ String countKey = String.format(CRAWLER_FAIL_COUNT_KEY, matchVideo.getContentId());
|
|
|
Integer count = (Integer) redisTemplate.opsForValue().get(countKey);
|
|
|
if (count != null && count >= 3) {
|
|
|
//更新状态为失败
|
|
@@ -328,8 +334,8 @@ public class MatchVideoServiceImpl {
|
|
|
redisLock.unlock(lockKey, lockValue);
|
|
|
}
|
|
|
} else {
|
|
|
- //更新状态为etl执行完成
|
|
|
- updateStatus(matchVideo.getId(), ContentStatusEnum.SUCCESS_3.getStatusCode());
|
|
|
+ //更新状态为爬虫执行完成
|
|
|
+ updateStatus(matchVideo.getId(), ContentStatusEnum.CRAWLER_SUCCESS.getStatusCode());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -353,6 +359,108 @@ public class MatchVideoServiceImpl {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ public void uploadCrawlerVideo() {
|
|
|
+ if (uploadCrawlerVideoPoolExecutor.getCorePoolSize() - uploadCrawlerVideoPoolExecutor.getActiveCount() > 0) {
|
|
|
+ int threadSize = uploadCrawlerVideoPoolExecutor.getCorePoolSize() - uploadCrawlerVideoPoolExecutor.getActiveCount();
|
|
|
+ log.info("threadNum={}", threadSize);
|
|
|
+ CountDownLatch countDownLatch = new CountDownLatch(threadSize);
|
|
|
+ // 启动消费者线程
|
|
|
+ for (int i = 0; i < threadSize; i++) {
|
|
|
+ uploadCrawlerVideoPoolExecutor.execute(new Thread(() -> {
|
|
|
+ log.info("启动上传小程序视频线程");
|
|
|
+ while (true) {
|
|
|
+ try {
|
|
|
+ // 超过 5 分钟没有数据,销毁当前线程
|
|
|
+ MatchVideo matchVideo = uploadCrawlerVideoQueue.poll(5, TimeUnit.MINUTES); // 等待最多 5 分钟
|
|
|
+ if (matchVideo == null) {
|
|
|
+ break; // 退出当前线程
|
|
|
+ }
|
|
|
+ processUploadCrawlerVideo(matchVideo);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("启动匹配小程序线程结束");
|
|
|
+ countDownLatch.countDown();
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ List<MatchVideo> matchVideos;
|
|
|
+ Integer id = (Integer) redisTemplate.opsForValue().get("last_match_video_id");
|
|
|
+ if (id != null) {
|
|
|
+ do {
|
|
|
+ MatchVideoExample example = new MatchVideoExample();
|
|
|
+ example.createCriteria().andIdGreaterThan(id)
|
|
|
+ .andContentStatusEqualTo(ContentStatusEnum.CRAWLER_SUCCESS.getStatusCode());
|
|
|
+ example.setOrderByClause("id asc");
|
|
|
+ Page<Object> page = new Page<>();
|
|
|
+ page.setCurrentPage(1);
|
|
|
+ page.setPageSize(1000);
|
|
|
+ example.setPage(page);
|
|
|
+ matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
+ if (CollectionUtils.isEmpty(matchVideos)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ id = matchVideos.get(matchVideos.size() - 1).getId();
|
|
|
+ uploadCrawlerVideoQueue.addAll(matchVideos);
|
|
|
+ } while (!CollectionUtils.isEmpty(matchVideos));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void processUploadCrawlerVideo(MatchVideo matchVideo) {
|
|
|
+ if (matchVideo.getContentStatus() != 2) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ boolean existUploadCrawlerVideo = crawlerVideoService.existUploadCrawlerVideo(matchVideo.getContentId());
|
|
|
+ log.info("processUploadCrawlerVideo contentId={} existCrawlerVideo={}", matchVideo.getContentId(), existUploadCrawlerVideo);
|
|
|
+ if (!existUploadCrawlerVideo) {
|
|
|
+ //查询相同的contentId,如果已经失败,则直接更新状态为失败
|
|
|
+ MatchVideoExample example = new MatchVideoExample();
|
|
|
+ example.createCriteria().andContentIdEqualTo(matchVideo.getContentId());
|
|
|
+ List<MatchVideo> matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
+ if (!CollectionUtils.isEmpty(matchVideos)) {
|
|
|
+ for (MatchVideo matchVideo1 : matchVideos) {
|
|
|
+ if (matchVideo1.getId().equals(matchVideo.getId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (ContentStatusEnum.isFail(matchVideo1.getContentStatus())) {
|
|
|
+ updateStatus(matchVideo.getId(), matchVideo1.getContentStatus());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String lockKey = String.format(UPLOAD_CRAWLER_LOCK_KEY, matchVideo.getContentId());
|
|
|
+ String lockValue = UUID.randomUUID().toString();
|
|
|
+ boolean lock = redisLock.tryLock(lockKey, lockValue, 10, TimeUnit.MINUTES);
|
|
|
+ if (lock) {
|
|
|
+ boolean res = crawlerVideoService.uploadCrawlerVideo(matchVideo.getContentId());
|
|
|
+ if (res) {
|
|
|
+ updateStatus(matchVideo.getId(), ContentStatusEnum.SUCCESS_3.getStatusCode());
|
|
|
+ } else {
|
|
|
+ //匹配失败记录
|
|
|
+ String countKey = String.format(UPLOAD_CRAWLER_FAIL_COUNT_KEY, matchVideo.getContentId());
|
|
|
+ Integer count = (Integer) redisTemplate.opsForValue().get(countKey);
|
|
|
+ if (count != null && count >= 3) {
|
|
|
+ //更新状态为失败
|
|
|
+ updateStatus(matchVideo.getId(), ContentStatusEnum.ERROR_99.getStatusCode());
|
|
|
+ } else {
|
|
|
+ if (count == null) {
|
|
|
+ redisTemplate.opsForValue().set(countKey, 1, 3, TimeUnit.DAYS);
|
|
|
+ } else {
|
|
|
+ redisTemplate.opsForValue().set(countKey, count + 1, 3, TimeUnit.DAYS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ redisLock.unlock(lockKey, lockValue);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //更新状态为etl执行完成
|
|
|
+ updateStatus(matchVideo.getId(), ContentStatusEnum.SUCCESS_3.getStatusCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void publishCrawlerVideo() {
|
|
|
List<MatchVideo> matchVideos;
|
|
|
Integer id = (Integer) redisTemplate.opsForValue().get("last_match_video_id");
|
|
@@ -365,7 +473,7 @@ public class MatchVideoServiceImpl {
|
|
|
example.setOrderByClause("id asc");
|
|
|
Page<Object> page = new Page<>();
|
|
|
page.setCurrentPage(1);
|
|
|
- page.setPageSize(5000);
|
|
|
+ page.setPageSize(1000);
|
|
|
example.setPage(page);
|
|
|
matchVideos = matchVideoMapper.selectByExample(example);
|
|
|
if (CollectionUtils.isEmpty(matchVideos)) {
|