|
|
@@ -0,0 +1,106 @@
|
|
|
+package cn.reghao.tnb.file.app.rpc;
|
|
|
+
|
|
|
+import cn.reghao.file.api.iface.JobService;
|
|
|
+import cn.reghao.jutil.tool.id.SnowFlake;
|
|
|
+import cn.reghao.oss.sdk.OssConsoleClient;
|
|
|
+import cn.reghao.oss.sdk.model.dto.ObjectInfo;
|
|
|
+import cn.reghao.tnb.content.api.iface.AdminVideoService;
|
|
|
+import cn.reghao.tnb.content.api.iface.MallService;
|
|
|
+import cn.reghao.tnb.content.api.iface.UserContentService;
|
|
|
+import cn.reghao.tnb.file.app.config.OssConsoleClientFactory;
|
|
|
+import cn.reghao.tnb.file.app.db.mapper.JobDetailMapper;
|
|
|
+import cn.reghao.tnb.file.app.delay.JobContext;
|
|
|
+import cn.reghao.tnb.file.app.delay.DelayJob;
|
|
|
+import cn.reghao.tnb.file.app.delay.task.ConvertTask;
|
|
|
+import cn.reghao.tnb.file.app.delay.task.ConvertTaskInfo;
|
|
|
+import cn.reghao.tnb.file.app.delay.task.OrderTask;
|
|
|
+import cn.reghao.tnb.file.app.delay.task.PublishVideoTask;
|
|
|
+import cn.reghao.tnb.file.app.model.constant.JobStatus;
|
|
|
+import cn.reghao.tnb.file.app.model.po.JobDetail;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.apache.dubbo.config.annotation.DubboService;
|
|
|
+import org.springframework.boot.autoconfigure.web.ServerProperties;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2024-12-04 13:31:48
|
|
|
+ */
|
|
|
+@DubboService
|
|
|
+@Service
|
|
|
+public class JobServiceImpl implements JobService {
|
|
|
+ @DubboReference(check = false, timeout = 60_000)
|
|
|
+ private UserContentService userContentService;
|
|
|
+ @DubboReference(check = false, timeout = 60_000)
|
|
|
+ private MallService mallService;
|
|
|
+ @DubboReference(check = false, timeout = 60_000)
|
|
|
+ private AdminVideoService adminVideoService;
|
|
|
+
|
|
|
+ private final SnowFlake idGenerator;
|
|
|
+ private final JobContext jobContext;
|
|
|
+ private final OssConsoleClientFactory ossConsoleClientFactory;
|
|
|
+ private final JobDetailMapper jobDetailMapper;
|
|
|
+ private final String baseDir;
|
|
|
+
|
|
|
+ public JobServiceImpl(JobContext jobContext, OssConsoleClientFactory ossConsoleClientFactory,
|
|
|
+ JobDetailMapper jobDetailMapper, ServerProperties serverProperties) {
|
|
|
+ this.idGenerator = new SnowFlake(1L, 1L);
|
|
|
+ this.jobContext = jobContext;
|
|
|
+ this.ossConsoleClientFactory = ossConsoleClientFactory;
|
|
|
+ this.jobDetailMapper = jobDetailMapper;
|
|
|
+ this.baseDir = serverProperties.getTomcat().getBasedir().getAbsolutePath();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long addOrderTimeoutJob(long orderId) {
|
|
|
+ long jobId = idGenerator.nextId();
|
|
|
+ OrderTask orderTask = new OrderTask(mallService, orderId);
|
|
|
+ long delaySecond = 15;
|
|
|
+ DelayJob delayJob = new DelayJob(jobId, orderTask, delaySecond);
|
|
|
+ jobContext.addJob(delayJob);
|
|
|
+
|
|
|
+ return jobId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long addPublishVideoJob(String videoId, long publishAt) {
|
|
|
+ long jobId = idGenerator.nextId();
|
|
|
+ PublishVideoTask publishVideoTask = new PublishVideoTask(userContentService, videoId);
|
|
|
+ long delaySecond = (publishAt - System.currentTimeMillis())/1000;
|
|
|
+ DelayJob delayJob = new DelayJob(jobId, publishVideoTask, delaySecond);
|
|
|
+ jobContext.addJob(delayJob);
|
|
|
+ return jobId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long addConvertVideoJob(String videoFileId, int channelCode) {
|
|
|
+ long jobId = idGenerator.nextId();
|
|
|
+ try {
|
|
|
+ OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
|
|
|
+ ObjectInfo objectInfo = ossConsoleClient.getObjectInfo(channelCode, videoFileId);
|
|
|
+ if (objectInfo == null) {
|
|
|
+ throw new Exception("not found ObjectInfo by videoFileId " + videoFileId);
|
|
|
+ }
|
|
|
+
|
|
|
+ String objectName = objectInfo.getObjectName();
|
|
|
+ String localPath = ossConsoleClient.getObject(objectName, channelCode, baseDir);
|
|
|
+ String destPath = String.format("%s/%s", baseDir, UUID.randomUUID());
|
|
|
+ ConvertTaskInfo convertTaskInfo =
|
|
|
+ new ConvertTaskInfo(videoFileId, channelCode, localPath, destPath, ossConsoleClient, adminVideoService);
|
|
|
+ ConvertTask convertTask = new ConvertTask(jobId, jobDetailMapper, convertTaskInfo);
|
|
|
+ long delaySecond = 5;
|
|
|
+ DelayJob delayJob = new DelayJob(jobId, convertTask, delaySecond);
|
|
|
+ jobContext.addJob(delayJob);
|
|
|
+
|
|
|
+ String status = JobStatus.Running.getDesc();
|
|
|
+ JobDetail jobDetail = new JobDetail(jobId, delayJob, status);
|
|
|
+ jobDetailMapper.save(jobDetail);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return jobId;
|
|
|
+ }
|
|
|
+}
|