2020-05-29 14:39:34 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-06-06 20:59:06 -07:00
|
|
|
import { InnerLogger, Log, apiLog } from './logger';
|
2020-05-29 14:39:34 -07:00
|
|
|
import { TimeoutError } from './errors';
|
2020-06-05 15:53:30 -07:00
|
|
|
import { assert } from './helper';
|
2020-05-29 14:39:34 -07:00
|
|
|
import { getCurrentApiCall, rewriteErrorMessage } from './debug/stackTrace';
|
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
export interface Progress {
|
|
|
|
readonly apiName: string;
|
|
|
|
readonly aborted: Promise<void>;
|
2020-06-05 15:53:30 -07:00
|
|
|
timeUntilDeadline(): number;
|
2020-06-04 16:43:48 -07:00
|
|
|
isRunning(): boolean;
|
|
|
|
cleanupWhenAborted(cleanup: () => any): void;
|
|
|
|
log(log: Log, message: string | Error): void;
|
|
|
|
}
|
2020-06-01 08:54:18 -07:00
|
|
|
|
2020-06-05 15:53:30 -07:00
|
|
|
export async function runAbortableTask<T>(task: (progress: Progress) => Promise<T>, logger: InnerLogger, timeout: number, apiName?: string): Promise<T> {
|
|
|
|
const controller = new ProgressController(logger, timeout, apiName);
|
2020-06-04 16:43:48 -07:00
|
|
|
return controller.run(task);
|
|
|
|
}
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
export class ProgressController {
|
|
|
|
// Promise and callback that forcefully abort the progress.
|
|
|
|
// This promise always rejects.
|
|
|
|
private _forceAbort: (error: Error) => void = () => {};
|
|
|
|
private _forceAbortPromise: Promise<any>;
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
// Promise and callback that resolve once the progress is aborted.
|
|
|
|
// This includes the force abort and also rejection of the task itself (failure).
|
|
|
|
private _aborted = () => {};
|
|
|
|
private _abortedPromise: Promise<void>;
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
// Cleanups to be run only in the case of abort.
|
|
|
|
private _cleanups: (() => any)[] = [];
|
2020-05-29 14:39:34 -07:00
|
|
|
|
|
|
|
private _logger: InnerLogger;
|
|
|
|
private _logRecording: string[] = [];
|
2020-06-04 16:43:48 -07:00
|
|
|
private _state: 'before' | 'running' | 'aborted' | 'finished' = 'before';
|
|
|
|
private _apiName: string;
|
|
|
|
private _deadline: number;
|
|
|
|
private _timeout: number;
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2020-06-05 15:53:30 -07:00
|
|
|
constructor(logger: InnerLogger, timeout: number, apiName?: string) {
|
2020-06-04 16:43:48 -07:00
|
|
|
this._apiName = apiName || getCurrentApiCall();
|
2020-05-29 14:39:34 -07:00
|
|
|
this._logger = logger;
|
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
this._timeout = timeout;
|
2020-06-05 15:53:30 -07:00
|
|
|
this._deadline = timeout ? monotonicTime() + timeout : 0;
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
this._forceAbortPromise = new Promise((resolve, reject) => this._forceAbort = reject);
|
|
|
|
this._forceAbortPromise.catch(e => null); // Prevent unhandle promsie rejection.
|
|
|
|
this._abortedPromise = new Promise(resolve => this._aborted = resolve);
|
2020-05-29 14:39:34 -07:00
|
|
|
}
|
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
async run<T>(task: (progress: Progress) => Promise<T>): Promise<T> {
|
|
|
|
assert(this._state === 'before');
|
|
|
|
this._state = 'running';
|
|
|
|
|
|
|
|
const progress: Progress = {
|
|
|
|
apiName: this._apiName,
|
|
|
|
aborted: this._abortedPromise,
|
2020-06-05 15:53:30 -07:00
|
|
|
timeUntilDeadline: () => this._deadline ? this._deadline - monotonicTime() : 2147483647, // 2^31-1 safe setTimeout in Node.
|
2020-06-04 16:43:48 -07:00
|
|
|
isRunning: () => this._state === 'running',
|
|
|
|
cleanupWhenAborted: (cleanup: () => any) => {
|
|
|
|
if (this._state === 'running')
|
|
|
|
this._cleanups.push(cleanup);
|
|
|
|
else
|
|
|
|
runCleanup(cleanup);
|
|
|
|
},
|
|
|
|
log: (log: Log, message: string | Error) => {
|
2020-06-06 20:59:06 -07:00
|
|
|
if (this._state === 'running') {
|
2020-06-04 16:43:48 -07:00
|
|
|
this._logRecording.push(message.toString());
|
2020-06-06 20:59:06 -07:00
|
|
|
this._logger._log(log, ' ' + message);
|
|
|
|
} else {
|
|
|
|
this._logger._log(log, message);
|
|
|
|
}
|
2020-06-04 16:43:48 -07:00
|
|
|
},
|
|
|
|
};
|
2020-06-06 20:59:06 -07:00
|
|
|
this._logger._log(apiLog, `=> ${this._apiName} started`);
|
2020-06-04 16:43:48 -07:00
|
|
|
|
|
|
|
const timeoutError = new TimeoutError(`Timeout ${this._timeout}ms exceeded during ${this._apiName}.`);
|
2020-06-05 15:53:30 -07:00
|
|
|
const timer = setTimeout(() => this._forceAbort(timeoutError), progress.timeUntilDeadline());
|
2020-06-04 16:43:48 -07:00
|
|
|
try {
|
|
|
|
const promise = task(progress);
|
|
|
|
const result = await Promise.race([promise, this._forceAbortPromise]);
|
|
|
|
clearTimeout(timer);
|
|
|
|
this._state = 'finished';
|
|
|
|
this._logRecording = [];
|
2020-06-06 20:59:06 -07:00
|
|
|
this._logger._log(apiLog, `<= ${this._apiName} succeeded`);
|
2020-05-29 14:39:34 -07:00
|
|
|
return result;
|
2020-06-04 16:43:48 -07:00
|
|
|
} catch (e) {
|
|
|
|
this._aborted();
|
|
|
|
rewriteErrorMessage(e, e.message + formatLogRecording(this._logRecording, this._apiName));
|
|
|
|
clearTimeout(timer);
|
|
|
|
this._state = 'aborted';
|
|
|
|
this._logRecording = [];
|
2020-06-06 20:59:06 -07:00
|
|
|
this._logger._log(apiLog, `<= ${this._apiName} failed`);
|
2020-06-04 16:43:48 -07:00
|
|
|
await Promise.all(this._cleanups.splice(0).map(cleanup => runCleanup(cleanup)));
|
|
|
|
throw e;
|
|
|
|
}
|
2020-05-29 14:39:34 -07:00
|
|
|
}
|
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
abort(error: Error) {
|
|
|
|
this._forceAbort(error);
|
2020-05-29 14:39:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runCleanup(cleanup: () => any) {
|
|
|
|
try {
|
|
|
|
await cleanup();
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatLogRecording(log: string[], name: string): string {
|
2020-06-01 08:54:18 -07:00
|
|
|
if (!log.length)
|
|
|
|
return '';
|
2020-05-29 14:39:34 -07:00
|
|
|
name = ` ${name} logs `;
|
|
|
|
const headerLength = 60;
|
|
|
|
const leftLength = (headerLength - name.length) / 2;
|
|
|
|
const rightLength = headerLength - name.length - leftLength;
|
|
|
|
return `\n${'='.repeat(leftLength)}${name}${'='.repeat(rightLength)}\n${log.join('\n')}\n${'='.repeat(headerLength)}`;
|
|
|
|
}
|
2020-06-05 15:53:30 -07:00
|
|
|
|
|
|
|
function monotonicTime(): number {
|
|
|
|
const [seconds, nanoseconds] = process.hrtime();
|
|
|
|
return seconds * 1000 + (nanoseconds / 1000000 | 0);
|
|
|
|
}
|