Skip to content

Commit

Permalink
JPEG変換のリトライ追加
Browse files Browse the repository at this point in the history
  • Loading branch information
WeakenedPlayer committed Nov 18, 2017
1 parent 1af80e7 commit 0cfb823
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Thumbs.db
# Dist #
/test/constants.ts
/dist/*
/dist-test/*
/type/*
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
tsconfig.json
src
test
dist-test
.project
.settings
package-lock.json
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"scope": "@weakenedplayer",
"name": "@weakenedplayer/screenshot-bot",
"version": "0.1.1-beta.1",
"version": "0.1.1-beta.2",
"description": "Discord client for screenshot posting.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "tsc && node dist\\test"
"test": "tsc -p tsconfig.test.json && node dist-test\\test"
},
"repository": {
"type": "git",
Expand Down
26 changes: 20 additions & 6 deletions src/image/jpeg-converter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ImageProvider } from './image';
import { Observable, Subject } from 'rxjs';
import { Observable, Subject, Observer } from 'rxjs';
import * as Path from 'path';

const sharp = require( 'sharp');
Expand All @@ -18,7 +18,9 @@ export class JpegOutputOption {

export class JpegConverterOption {
constructor( public readonly workDirectory: string = '',
public readonly jpegOption: JpegOutputOption = new JpegOutputOption() ) {}
public readonly jpegOption: JpegOutputOption = new JpegOutputOption(),
public readonly maxRetry = 5,
public readonly retryInterval = 200 ) {}
}

export class JpegConverter implements ImageProvider {
Expand All @@ -38,19 +40,31 @@ export class JpegConverter implements ImageProvider {
let dst: string = Path.join( option.workDirectory, base );

// 変換後のファイル名を出力するObservable
return Observable.create( observer => {
let retryCount = 0;
let convert$: Observable<string> = Observable.create( ( observer: Observer<Observable<string>> ) => {
sharp( src )
.jpeg( option.jpegOption )
.toFile( dst, ( err, info ) => {
if( !err ) {
// Memo: 1回ごとに完了させる
observer.next( dst );
observer.next( Observable.of( dst ) );
observer.complete();
} else {
console.warn( 'unable to convert.' );
if( retryCount < option.maxRetry ) {
// ディレイ付きリトライ
retryCount++;
console.warn( 'Unable to convert \"' + src + '\". Retry: ' + retryCount + '/' + option.maxRetry );
observer.next( Observable.timer( option.retryInterval ).flatMap( () => convert$ ) );
} else {
// リトライNG
console.warn( 'Unable to convert. Exceed retry count of ' + option.maxRetry );
observer.complete();
}
}
} );
} ).map( () => dst );
} ).flatMap( dst => dst );

return convert$;
} );
}

Expand Down
8 changes: 5 additions & 3 deletions src/watcher/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { FSWatcher, watch } from 'chokidar';
import { Observable, BehaviorSubject } from 'rxjs';

export class Watcher {
static STABILITY_THRESHOLD = 2000;
static POLL_INTERVAL = 500;
private watcher: FSWatcher = null;

// 変更ごとのObservable
Expand Down Expand Up @@ -37,16 +39,16 @@ export class Watcher {
} ).publish().refCount();
}

constructor( private path: string ) {
constructor( private path: string, stabilityThreshold: number = Watcher.STABILITY_THRESHOLD, pollInterval: number = Watcher.POLL_INTERVAL ) {
// TENATIVE: この時点で監視を開始している。
// 一つのWatcherで1つの対象しか監視しない前提で作成する。
this.watcher = watch( this.path, {
ignoreInitial: true,
followSymlinks: false,
persistent: true, // 継続的にモニタする
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 200
stabilityThreshold: stabilityThreshold,
pollInterval: pollInterval
},
} );

Expand Down
25 changes: 25 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { JpegConverter, JpegConverterOption, ImageWatcher } from '../src';
import { Subject, Subscription, Observable } from 'rxjs';
import { srcDir, srcDir2, tmpDir } from './constants';

let condition = new Subject<boolean>();
let toggle = false;

let filter$ = new Subject<string>();
let src$ = new ImageWatcher( filter$ );
let option$: Observable<JpegConverterOption> = Observable.of( new JpegConverterOption( tmpDir ) ).shareReplay(1);
let converter$ = new JpegConverter( src$.image$, option$ );

//setInterval( ()=>{
// if( toggle ) {
// toggle = false;
// filter$.next( srcDir1 );
// console.log( 'Watching ' + srcDir1 );
// } else {
// toggle = true;
// filter$.next( srcDir2 );
// console.log( 'Watching ' + srcDir2 );
// }
//}, 5000 );
converter$.image$.map( image => { console.log( image ) } ).subscribe();
filter$.next( srcDir );
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"typeRoots": [ "node_modules/@types" ]
},
"include": [ "src/**/*" ],
"exclude": [ "node_modules", "dist" ]
"exclude": [ "node_modules", "dist", "dist-test" ]
}
16 changes: 16 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"outDir": "./dist-test",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"lib": [ "dom", "es6" ],
"typeRoots": [ "node_modules/@types" ]
},
"include": [ "src/**/*", "test/**/*" ],
"exclude": [ "node_modules", "dist", "dist-test" ]
}

0 comments on commit 0cfb823

Please sign in to comment.