@soga/encoder 0.2.8 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/encoder.js +1 -1
- package/package.json +4 -3
package/dist/encoder.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Encoder=void 0;const typeorm_1=require("typeorm"),entities_1=require("@soga/entities"),types_1=require("@soga/types"),piscina_1=require("piscina"),single_encoder_1=require("@soga/single-encoder"),utils_1=require("@soga/utils"),sdk_1=require("@soga/sdk"),path_1=require("path"),piscina=new piscina_1.Piscina({filename:require.resolve("@soga/single-encoder")});class Encoder{onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;isRunning=!1;thread_count=3;maxThreads=10;threads=[];params;sdk;getValidThreads(e){return Math.min(Math.max(e??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:t}=e;this.fileRepository=t.getRepository(entities_1.UploadFile),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=(0,sdk_1.getSdk)(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this)),piscina.on("message",(async e=>{if("percent"==e.type){const{id:t,data:s}=e,i=await this.fileRepository.findOneBy({id:t});if(!i)return;const{percent:a,step:
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Encoder=void 0;const typeorm_1=require("typeorm"),entities_1=require("@soga/entities"),types_1=require("@soga/types"),piscina_1=require("piscina"),single_encoder_1=require("@soga/single-encoder"),utils_1=require("@soga/utils"),sdk_1=require("@soga/sdk"),path_1=require("path"),piscina=new piscina_1.Piscina({filename:require.resolve("@soga/single-encoder")});let pis_count=0;class Encoder{onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;isRunning=!1;thread_count=3;maxThreads=10;threads=[];params;sdk;getValidThreads(e){return Math.min(Math.max(e??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:t}=e;this.fileRepository=t.getRepository(entities_1.UploadFile),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=(0,sdk_1.getSdk)(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this)),console.log("pis_count: ",pis_count++),piscina.on("message",(async e=>{if("percent"==e.type){const{id:t,data:s}=e,i=await this.fileRepository.findOneBy({id:t});if(!i)return;const{percent:a,step:o}=s;i.progress[o].percent=a;const r=(0,utils_1.getProgressPercent)(i.progress);await this.fileRepository.update(i.id,{progress:i.progress,percent:r}),this.onProgress(i)}})),piscina.on("error",(async e=>{await(this.params.onError?.(null,new utils_1.SimpleError({code:12200,message:e.message,stack:e.stack})))}))}async setThreads(e){const t=this.getValidThreads(e);this.thread_count=t,await this.run()}async init(){const e=await this.getUid();await this.fileRepository.update({uid:e,encode_status:types_1.EncodeStatus.PROCESS},{encode_status:types_1.EncodeStatus.NULL})}async start(){await this.run()}async stopFiles(e,t=!1){t&&await this.fileRepository.update({id:(0,typeorm_1.In)(e)},{is_paused:!0});const s=this.threads.filter((t=>e.includes(t.id)));await Promise.all(s.map((e=>e.stop()))),await this.run()}async stopAll(e=!1){const t=await this.getUid();e&&await this.fileRepository.update({uid:t},{is_paused:!0}),await Promise.all(this.threads.map((e=>e.stop())))}getThread(e){const t=new AbortController;return{id:e.id,uid:e.uid,start:async()=>{try{const s=await this.fileRepository.findOneBy({id:e.id});if(s.encode_status!=types_1.EncodeStatus.PROCESS)return;if(!s)return;const{id:i,uid:a,config:o,inputs:r,type:n,external_texts:d}=s,p=[];s.ali_host_id&&p.push(types_1.HostType.ALI),s.baidu_host_id&&p.push(types_1.HostType.BAIDU);const{encode_root:c}=await this.sdk.getSystemConfig(),u=(0,path_1.resolve)(c,s.id.toString());if(await this.fileRepository.update(e.id,{output_root:u}),s.type!=types_1.RecordType.AFFIX){const c={id:i,uid:a,keepPreview:o.file_keeps==types_1.RecordFileKeep.PREVIEW||o.file_keeps==types_1.RecordFileKeep.BOTH,keepSource:o.file_keeps==types_1.RecordFileKeep.SOURCE||o.file_keeps==types_1.RecordFileKeep.BOTH,input:r[0],type:n,hosts:p,outputRoot:u,texts:d,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug};if(!Object.keys(s.progress??{}).length){const t=await(0,single_encoder_1.prepare)(c);await this.fileRepository.update(e.id,{progress:t.data})}const h=await piscina.run(c,{name:"encode",signal:t.signal});if(!h)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:h})}else{const a={id:i,inputs:s.inputs,outputRoot:u,hosts:p,debug:this.params.debug};if(!Object.keys(s.progress??{}).length){const t=await(0,single_encoder_1.prepareAffix)(a);await this.fileRepository.update(e.id,{progress:t})}const o=await piscina.run(a,{name:"encodeAffix",signal:t.signal});if(!o)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:{affix:o}})}await this.onSuccess(await this.fileRepository.findOneBy({id:i}))}catch(t){"AbortError"==t.name?await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.NULL}):"SimpleError"==t.name?await this.onError(await this.fileRepository.findOneBy({id:e.id}),t):await this.onError(await this.fileRepository.findOneBy({id:e.id}),new utils_1.SimpleError({code:12100,message:t.message,stack:t.stack}))}finally{this.threads=this.threads.filter((t=>t.id!==e.id)),await this.run()}},stop:async()=>{t.abort()}}}async run(){for(;this.isRunning;)await new Promise((e=>{setTimeout(e,100)}));this.isRunning=!0;const e=await this.getUid(),t=this.threads.length,s=this.thread_count;if(t<s){const i=s-t;for(let t=0;t<i;t++){const t=await this.fileRepository.findOne({where:{uid:e,encode_status:types_1.EncodeStatus.NULL,type:(0,typeorm_1.Not)(types_1.RecordType.FOLDER),is_paused:!1},order:{created_at:"ASC"}});if(!t)break;const s=this.getThread(t);this.threads.push(s),await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.PROCESS}),s.start()}}else if(t>s){const e=t-s,i=this.threads.slice(0,e);for(const e of i)await e.stop()}this.isRunning=!1}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}}exports.Encoder=Encoder;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soga/encoder",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -50,11 +50,12 @@
|
|
|
50
50
|
"@soga/single-encoder": "^0.2.8",
|
|
51
51
|
"@soga/types": "^0.2.8",
|
|
52
52
|
"fs-extra": "^11.2.0",
|
|
53
|
-
"piscina": "^4.8.0"
|
|
53
|
+
"piscina": "^4.8.0",
|
|
54
|
+
"reflect-metadata": "^0.2.2"
|
|
54
55
|
},
|
|
55
56
|
"peerDependencies": {
|
|
56
57
|
"better-sqlite3": "*",
|
|
57
58
|
"typeorm": "*"
|
|
58
59
|
},
|
|
59
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "2e688c2ed275589e0b5b8eada07b1f6b8d45c0b5"
|
|
60
61
|
}
|