mirror of
				https://github.com/actions/cache.git
				synced 2025-11-04 13:29:10 +08:00 
			
		
		
		
	Consume latest toolkit and fix dangling promise bug (#1217)
* Consume latest toolkit and fix dangling promise bug * Pass earlyExit parameter to run method so tests don't hang * Pass earlyExit parameter to run method so tests don't hang * Refactor restore files to have better patterns for testing * style
This commit is contained in:
		@@ -1,10 +1,3 @@
 | 
			
		||||
import restoreImpl from "./restoreImpl";
 | 
			
		||||
import { StateProvider } from "./stateProvider";
 | 
			
		||||
import { restoreRun } from "./restoreImpl";
 | 
			
		||||
 | 
			
		||||
async function run(): Promise<void> {
 | 
			
		||||
    await restoreImpl(new StateProvider());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
run();
 | 
			
		||||
 | 
			
		||||
export default run;
 | 
			
		||||
restoreRun(true);
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,14 @@ import * as cache from "@actions/cache";
 | 
			
		||||
import * as core from "@actions/core";
 | 
			
		||||
 | 
			
		||||
import { Events, Inputs, Outputs, State } from "./constants";
 | 
			
		||||
import { IStateProvider } from "./stateProvider";
 | 
			
		||||
import {
 | 
			
		||||
    IStateProvider,
 | 
			
		||||
    NullStateProvider,
 | 
			
		||||
    StateProvider
 | 
			
		||||
} from "./stateProvider";
 | 
			
		||||
import * as utils from "./utils/actionUtils";
 | 
			
		||||
 | 
			
		||||
async function restoreImpl(
 | 
			
		||||
export async function restoreImpl(
 | 
			
		||||
    stateProvider: IStateProvider
 | 
			
		||||
): Promise<string | undefined> {
 | 
			
		||||
    try {
 | 
			
		||||
@@ -82,4 +86,37 @@ async function restoreImpl(
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default restoreImpl;
 | 
			
		||||
async function run(
 | 
			
		||||
    stateProvider: IStateProvider,
 | 
			
		||||
    earlyExit: boolean | undefined
 | 
			
		||||
): Promise<void> {
 | 
			
		||||
    try {
 | 
			
		||||
        await restoreImpl(stateProvider);
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
        console.error(err);
 | 
			
		||||
        if (earlyExit) {
 | 
			
		||||
            process.exit(1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // node will stay alive if any promises are not resolved,
 | 
			
		||||
    // which is a possibility if HTTP requests are dangling
 | 
			
		||||
    // due to retries or timeouts. We know that if we got here
 | 
			
		||||
    // that all promises that we care about have successfully
 | 
			
		||||
    // resolved, so simply exit with success.
 | 
			
		||||
    if (earlyExit) {
 | 
			
		||||
        process.exit(0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function restoreOnlyRun(
 | 
			
		||||
    earlyExit?: boolean | undefined
 | 
			
		||||
): Promise<void> {
 | 
			
		||||
    await run(new NullStateProvider(), earlyExit);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function restoreRun(
 | 
			
		||||
    earlyExit?: boolean | undefined
 | 
			
		||||
): Promise<void> {
 | 
			
		||||
    await run(new StateProvider(), earlyExit);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,3 @@
 | 
			
		||||
import restoreImpl from "./restoreImpl";
 | 
			
		||||
import { NullStateProvider } from "./stateProvider";
 | 
			
		||||
import { restoreOnlyRun } from "./restoreImpl";
 | 
			
		||||
 | 
			
		||||
async function run(): Promise<void> {
 | 
			
		||||
    await restoreImpl(new NullStateProvider());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
run();
 | 
			
		||||
 | 
			
		||||
export default run;
 | 
			
		||||
restoreOnlyRun(true);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user