func handleRestore()

in lambda/rapid/handlers.go [802:866]


func handleRestore(execCtx *rapidContext, restore *interop.Restore) (interop.RestoreResult, error) {
	err := execCtx.credentialsService.UpdateCredentials(restore.AwsKey, restore.AwsSecret, restore.AwsSession, restore.CredentialsExpiry)
	restoreStatus := telemetry.RuntimeDoneSuccess

	restoreResult := interop.RestoreResult{}

	defer func() {
		sendRestoreRuntimeDoneLogEvent(execCtx, restoreStatus)
	}()

	if err != nil {
		log.Infof("error when updating credentials: %s", err)
		return restoreResult, interop.ErrRestoreUpdateCredentials
	}

	renderer := rendering.NewRestoreRenderer()
	execCtx.renderingService.SetRenderer(renderer)

	registrationService := execCtx.registrationService
	runtime := registrationService.GetRuntime()

	execCtx.SetLogStreamName(restore.LogStreamName)

	// If runtime has not called /restore/next then just return
	// instead of releasing the Runtime since there is no need to release.
	// Then the runtime should be released only during Invoke
	if runtime.GetState() != runtime.RuntimeRestoreReadyState {
		restoreStatus = telemetry.RuntimeDoneSuccess
		log.Infof("Runtime is in state: %s just returning", runtime.GetState().Name())

		return restoreResult, nil
	}

	deadlineNs := time.Now().Add(time.Duration(restore.RestoreHookTimeoutMs) * time.Millisecond).UnixNano()

	ctx, ctxCancel := context.WithDeadline(context.Background(), time.Unix(0, deadlineNs))

	defer ctxCancel()

	startTime := metering.Monotime()

	runtime.Release()

	initFlow := execCtx.initFlow
	err = initFlow.AwaitRuntimeReadyWithDeadline(ctx)

	fatalErrorType, fatalErrorFound := appctx.LoadFirstFatalError(execCtx.appCtx)

	// If there is an error occured when waiting runtime to complete the restore hook execution,
	// check if there is any error stored in appctx to get the root cause error type
	// Runtime.ExitError is an example to such a scenario
	if fatalErrorFound {
		err = fmt.Errorf("%s", string(fatalErrorType))
	}

	if err != nil {
		restoreStatus = telemetry.RuntimeDoneError
	}

	endTime := metering.Monotime()
	restoreDuration := time.Duration(endTime - startTime)
	restoreResult.RestoreMs = restoreDuration.Milliseconds()

	return restoreResult, err
}