@soga/encoder 0.2.30 → 0.2.31

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.d.ts CHANGED
@@ -18,8 +18,8 @@ export declare class Encoder {
18
18
  setThreads(threads: number): Promise<void>;
19
19
  init(): Promise<void>;
20
20
  start(): Promise<void>;
21
- stopFiles(ids: number[], change_db?: boolean): Promise<void>;
22
- stopAll(change_db?: boolean): Promise<void>;
21
+ stopFiles(ids: number[]): Promise<void>;
22
+ stopAll(): Promise<void>;
23
23
  private getThread;
24
24
  private run;
25
25
  private getUid;
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");class Encoder{piscina;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));const s=new piscina_1.Piscina({filename:require.resolve("@soga/single-encoder")});this.piscina=s,s.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:r}=s;i.progress[r].percent=a;const o=(0,utils_1.getProgressPercent)(i.progress);await this.fileRepository.update(i.id,{progress:i.progress,percent:o}),this.onProgress(i)}})),s.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:r,inputs:o,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(),h=(0,path_1.resolve)(c,s.id.toString());if(await this.fileRepository.update(e.id,{output_root:h}),s.type!=types_1.RecordType.AFFIX){const c={id:i,uid:a,keepPreview:r.file_keeps==types_1.RecordFileKeep.PREVIEW||r.file_keeps==types_1.RecordFileKeep.BOTH,keepSource:r.file_keeps==types_1.RecordFileKeep.SOURCE||r.file_keeps==types_1.RecordFileKeep.BOTH,input:o[0],type:n,hosts:p,outputRoot:h,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 u=await this.piscina.run(c,{name:"encode",signal:t.signal});if(!u)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:u})}else{const a={id:i,inputs:s.inputs,outputRoot:h,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 r=await this.piscina.run(a,{name:"encodeAffix",signal:t.signal});if(!r)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:{affix:r}})}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)}));try{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()}}finally{this.isRunning=!1}}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}}exports.Encoder=Encoder;
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");class Encoder{piscina;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));const s=new piscina_1.Piscina({filename:require.resolve("@soga/single-encoder")});this.piscina=s,s.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:r,step:a}=s;i.progress[a].percent=r;const o=(0,utils_1.getProgressPercent)(i.progress);await this.fileRepository.update(i.id,{progress:i.progress,percent:o}),this.onProgress(i)}})),s.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){const t=this.threads.filter((t=>e.includes(t.id)));await Promise.all(t.map((e=>e.stop()))),await this.run()}async stopAll(){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:r,config:a,inputs:o,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(),h=(0,path_1.resolve)(c,s.id.toString());if(await this.fileRepository.update(e.id,{output_root:h}),s.type!=types_1.RecordType.AFFIX){const c={id:i,uid:r,keepPreview:a.file_keeps==types_1.RecordFileKeep.PREVIEW||a.file_keeps==types_1.RecordFileKeep.BOTH,keepSource:a.file_keeps==types_1.RecordFileKeep.SOURCE||a.file_keeps==types_1.RecordFileKeep.BOTH,input:o[0],type:n,hosts:p,outputRoot:h,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 u=await this.piscina.run(c,{name:"encode",signal:t.signal});if(!u)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:u})}else{const r={id:i,inputs:s.inputs,outputRoot:h,hosts:p,debug:this.params.debug};if(!Object.keys(s.progress??{}).length){const t=await(0,single_encoder_1.prepareAffix)(r);await this.fileRepository.update(e.id,{progress:t})}const a=await this.piscina.run(r,{name:"encodeAffix",signal:t.signal});if(!a)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:{affix:a}})}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)}));try{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()}}finally{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.30",
3
+ "version": "0.2.31",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -45,7 +45,7 @@
45
45
  "author": "",
46
46
  "license": "ISC",
47
47
  "dependencies": {
48
- "@soga/entities": "^0.2.24",
48
+ "@soga/entities": "^0.2.31",
49
49
  "@soga/sdk": "^0.2.22",
50
50
  "@soga/single-encoder": "^0.2.30",
51
51
  "@soga/types": "^0.2.22",
@@ -56,5 +56,5 @@
56
56
  "better-sqlite3": "*",
57
57
  "typeorm": "*"
58
58
  },
59
- "gitHead": "267ba2a40160336d743b9368f9086fbaa5b361ae"
59
+ "gitHead": "52c8accbbc7637438cd3be69028653902c61f66e"
60
60
  }