2020-03-04 12:49:36 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-03-04 12:49:36 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2020-05-14 20:06:12 +01:00
|
|
|
import {Logger} from '../../../src/ngtsc/logging';
|
2020-04-28 12:11:18 +01:00
|
|
|
import {NGCC_TIMED_OUT_EXIT_CODE} from '../constants';
|
|
|
|
|
2020-03-04 12:49:36 +00:00
|
|
|
import {LockFile} from './lock_file';
|
|
|
|
|
2020-04-28 12:11:18 +01:00
|
|
|
class TimeoutError extends Error {
|
|
|
|
code = NGCC_TIMED_OUT_EXIT_CODE;
|
|
|
|
}
|
|
|
|
|
2020-03-04 12:49:36 +00:00
|
|
|
/**
|
|
|
|
* AsyncLocker is used to prevent more than one instance of ngcc executing at the same time,
|
|
|
|
* when being called in an asynchronous context.
|
|
|
|
*
|
|
|
|
* * When ngcc starts executing, it creates a file in the `compiler-cli/ngcc` folder.
|
|
|
|
* * If it finds one is already there then it pauses and waits for the file to be removed by the
|
|
|
|
* other process. If the file is not removed within a set timeout period given by
|
|
|
|
* `retryDelay*retryAttempts` an error is thrown with a suitable error message.
|
|
|
|
* * If the process locking the file changes, then we restart the timeout.
|
|
|
|
* * When ngcc completes executing, it removes the file so that future ngcc executions can start.
|
|
|
|
*/
|
|
|
|
export class AsyncLocker {
|
|
|
|
constructor(
|
|
|
|
private lockFile: LockFile, protected logger: Logger, private retryDelay: number,
|
|
|
|
private retryAttempts: number) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a function guarded by the lock file.
|
|
|
|
*
|
|
|
|
* @param fn The function to run.
|
|
|
|
*/
|
|
|
|
async lock<T>(fn: () => Promise<T>): Promise<T> {
|
|
|
|
await this.create();
|
2020-06-25 16:43:05 +01:00
|
|
|
try {
|
|
|
|
return await fn();
|
|
|
|
} finally {
|
|
|
|
this.lockFile.remove();
|
|
|
|
}
|
2020-03-04 12:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected async create() {
|
|
|
|
let pid: string = '';
|
|
|
|
for (let attempts = 0; attempts < this.retryAttempts; attempts++) {
|
|
|
|
try {
|
|
|
|
return this.lockFile.write();
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'EEXIST') {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
const newPid = this.lockFile.read();
|
|
|
|
if (newPid !== pid) {
|
|
|
|
// The process locking the file has changed, so restart the timeout
|
|
|
|
attempts = 0;
|
|
|
|
pid = newPid;
|
|
|
|
}
|
|
|
|
if (attempts === 0) {
|
2020-10-26 21:12:56 +00:00
|
|
|
this.logger.info(
|
2020-03-04 12:49:36 +00:00
|
|
|
`Another process, with id ${pid}, is currently running ngcc.\n` +
|
2020-10-26 21:12:56 +00:00
|
|
|
`Waiting up to ${this.retryDelay * this.retryAttempts / 1000}s for it to finish.\n` +
|
|
|
|
`(If you are sure no ngcc process is running then you should delete the lock-file at ${
|
|
|
|
this.lockFile.path}.)`);
|
2020-03-04 12:49:36 +00:00
|
|
|
}
|
|
|
|
// The file is still locked by another process so wait for a bit and retry
|
|
|
|
await new Promise(resolve => setTimeout(resolve, this.retryDelay));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we fall out of the loop then we ran out of rety attempts
|
2020-10-26 21:12:56 +00:00
|
|
|
throw new TimeoutError(
|
|
|
|
`Timed out waiting ${
|
|
|
|
this.retryAttempts * this.retryDelay /
|
|
|
|
1000}s for another ngcc process, with id ${pid}, to complete.\n` +
|
|
|
|
`(If you are sure no ngcc process is running then you should delete the lock-file at ${
|
|
|
|
this.lockFile.path}.)`);
|
2020-03-04 12:49:36 +00:00
|
|
|
}
|
|
|
|
}
|