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-08-24 06:51:51 -07:00
|
|
|
import { TimeoutError } from '../utils/errors';
|
2020-08-28 10:51:55 -07:00
|
|
|
import { assert, monotonicTime } from '../utils/utils';
|
2021-02-10 21:50:29 -08:00
|
|
|
import { LogName } from '../utils/debugLogger';
|
2021-02-09 14:44:48 -08:00
|
|
|
import { CallMetadata, Instrumentation, SdkObject } from './instrumentation';
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
export interface Progress {
|
2020-08-17 14:12:31 -07:00
|
|
|
log(message: string): 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;
|
2020-06-09 18:57:11 -07:00
|
|
|
throwIfAborted(): void;
|
2021-02-10 21:55:46 -08:00
|
|
|
beforeInputAction(): Promise<void>;
|
|
|
|
afterInputAction(): Promise<void>;
|
2021-02-12 10:11:30 -08:00
|
|
|
metadata: CallMetadata;
|
2021-01-25 18:44:46 -08: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
|
|
|
// Cleanups to be run only in the case of abort.
|
|
|
|
private _cleanups: (() => any)[] = [];
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2021-02-10 21:50:29 -08:00
|
|
|
private _logName = 'api';
|
2020-06-04 16:43:48 -07:00
|
|
|
private _state: 'before' | 'running' | 'aborted' | 'finished' = 'before';
|
2020-09-17 09:32:54 -07:00
|
|
|
private _deadline: number = 0;
|
|
|
|
private _timeout: number = 0;
|
2021-02-09 14:44:48 -08:00
|
|
|
readonly metadata: CallMetadata;
|
|
|
|
readonly instrumentation: Instrumentation;
|
|
|
|
readonly sdkObject: SdkObject;
|
|
|
|
|
|
|
|
constructor(metadata: CallMetadata, sdkObject: SdkObject) {
|
|
|
|
this.metadata = metadata;
|
|
|
|
this.sdkObject = sdkObject;
|
|
|
|
this.instrumentation = sdkObject.instrumentation;
|
2020-06-04 16:43:48 -07:00
|
|
|
this._forceAbortPromise = new Promise((resolve, reject) => this._forceAbort = reject);
|
2021-02-09 14:44:48 -08:00
|
|
|
this._forceAbortPromise.catch(e => null); // Prevent unhandled promise rejection.
|
2020-05-29 14:39:34 -07:00
|
|
|
}
|
|
|
|
|
2020-09-10 15:34:13 -07:00
|
|
|
setLogName(logName: LogName) {
|
|
|
|
this._logName = logName;
|
|
|
|
}
|
|
|
|
|
2020-09-17 09:32:54 -07:00
|
|
|
async run<T>(task: (progress: Progress) => Promise<T>, timeout?: number): Promise<T> {
|
|
|
|
if (timeout) {
|
|
|
|
this._timeout = timeout;
|
|
|
|
this._deadline = timeout ? monotonicTime() + timeout : 0;
|
|
|
|
}
|
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
assert(this._state === 'before');
|
|
|
|
this._state = 'running';
|
|
|
|
|
|
|
|
const progress: Progress = {
|
2020-08-17 14:12:31 -07:00
|
|
|
log: message => {
|
2021-02-10 21:50:29 -08:00
|
|
|
if (this._state === 'running') {
|
|
|
|
this.metadata.log.push(message);
|
2021-02-10 21:55:46 -08:00
|
|
|
this.instrumentation.onCallLog(this._logName, message, this.sdkObject, this.metadata);
|
2021-02-10 21:50:29 -08:00
|
|
|
}
|
2020-08-17 14:12:31 -07:00
|
|
|
},
|
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);
|
|
|
|
},
|
2020-06-09 18:57:11 -07:00
|
|
|
throwIfAborted: () => {
|
|
|
|
if (this._state === 'aborted')
|
|
|
|
throw new AbortedError();
|
|
|
|
},
|
2021-02-10 21:55:46 -08:00
|
|
|
beforeInputAction: async () => {
|
|
|
|
await this.instrumentation.onBeforeInputAction(this.sdkObject, this.metadata);
|
|
|
|
},
|
|
|
|
afterInputAction: async () => {
|
|
|
|
await this.instrumentation.onAfterInputAction(this.sdkObject, this.metadata);
|
2021-01-25 18:44:46 -08:00
|
|
|
},
|
2021-02-12 10:11:30 -08:00
|
|
|
metadata: this.metadata
|
2020-06-04 16:43:48 -07:00
|
|
|
};
|
|
|
|
|
2020-07-15 14:04:39 -07:00
|
|
|
const timeoutError = new TimeoutError(`Timeout ${this._timeout}ms exceeded.`);
|
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]);
|
|
|
|
this._state = 'finished';
|
2020-05-29 14:39:34 -07:00
|
|
|
return result;
|
2020-06-04 16:43:48 -07:00
|
|
|
} catch (e) {
|
2020-09-17 09:32:54 -07:00
|
|
|
this._state = 'aborted';
|
|
|
|
await Promise.all(this._cleanups.splice(0).map(cleanup => runCleanup(cleanup)));
|
2020-06-04 16:43:48 -07:00
|
|
|
throw e;
|
2021-02-10 21:55:46 -08:00
|
|
|
} finally {
|
|
|
|
clearTimeout(timer);
|
2020-06-04 16:43:48 -07:00
|
|
|
}
|
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) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 18:57:11 -07:00
|
|
|
class AbortedError extends Error {}
|