Skip to content

Commit

Permalink
fix(vscode): stop polling for recent cipes after 5 failed auth attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless committed Dec 11, 2024
1 parent c7bc53d commit 2c4b4d9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
73 changes: 67 additions & 6 deletions libs/vscode/nx-cloud-view/src/lib/cloud-view-state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
CloudOnboardingInfo,
} from '@nx-console/shared/types';
import { getRecentCIPEData } from '@nx-console/vscode/nx-workspace';
import { getOutputChannel } from '@nx-console/vscode/output-channels';
import {
and,
AnyEventObject,
Expand All @@ -18,20 +19,47 @@ import {
spawnChild,
} from 'xstate';

const COLD_POLLING_TIME = 180_000;
const HOT_POLLING_TIME = 10_000;
const COLD_POLLING_TIME = 5_000; //180_000;
const HOT_POLLING_TIME = 5_000; //10_000;

const pollingMachine = setup({
types: {
context: {} as {
pollingFrequency: number;
emptyCounter: number;
authErrorCounter: number;
},
},
delays: {
pollingFrequency: ({ context }) => context.pollingFrequency,
},
actions: {
adjustAuthErrorCounter: assign(({ context, event }) => {
const recentCIPEData = event['output'] as
| {
info?: CIPEInfo[];
error?: CIPEInfoError;
}
| undefined;

if (
recentCIPEData?.error &&
recentCIPEData.error.type === 'authentication'
) {
return {
...context,
authErrorCounter: context.authErrorCounter + 1,
};
} else {
return {
...context,
authErrorCounter: 0,
};
}
}),
resetAuthErrorCounter: assign(({ context }) => ({
...context,
authErrorCounter: 0,
})),
sendCIPEUpdate: sendTo(
({ system }) => system.get('cloud-view'),
({ event }) => ({
Expand Down Expand Up @@ -64,11 +92,16 @@ const pollingMachine = setup({
return await getRecentCIPEData();
}),
},
guards: {
isAuthErrorCounterTooHigh: ({ context }) => {
return context.authErrorCounter > 4;
},
},
}).createMachine({
/** @xstate-layout N4IgpgJg5mDOIC5QAcD2AbdBLAdlAdLOmGMrlAMQDqAggNICiA+gKoAKA2gAwC6iKqWFgAuW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HIfoz2S1yYZDPS-oY9A0-o0pIJBoY5DZbCAcKgIHBtUqoNriliyogALTfa4m4wuiSsjpacPM2lSOT8e4DS6mQSNXnygjZFyBvE6gmhhCmAF6Ywm0wtET-esu+niQTMtpiRlyK2PQx5gP4WbzRZ4IO68sNET4ST8fjO9PyHoGelt-Aui31+SCQwDHmevlIsdlokIK3ifDZnP-KpMkR3+lCOjGy8cuQmuRMqQeqxAA */
context: {
pollingFrequency: COLD_POLLING_TIME,
emptyCounter: 0,
authErrorCounter: 0,
},
id: 'polling',
initial: 'polling',
Expand All @@ -79,12 +112,40 @@ const pollingMachine = setup({
target: 'polling',
},
},
always: {
guard: 'isAuthErrorCounterTooHigh',
target: 'sleeping',
},
},
sleeping: {
on: {
WAKE_UP: {
target: 'polling',
actions: ['resetAuthErrorCounter'],
},
},
entry: [
() => {
getOutputChannel().appendLine(
'Stopping cloud polling after 5 failed authentication attempts'
);
},
],
exit: [
() => {
getOutputChannel().appendLine('Restarting cloud polling');
},
],
},
polling: {
invoke: {
src: 'getRecentCIPEData',
onDone: {
actions: ['sendCIPEUpdate', 'setPollingFrequency'],
actions: [
'sendCIPEUpdate',
'setPollingFrequency',
'adjustAuthErrorCounter',
],
target: 'waiting',
},
onError: {
Expand Down
1 change: 1 addition & 0 deletions libs/vscode/nx-cloud-view/src/lib/init-nx-cloud-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export function initNxCloudView(context: ExtensionContext) {
const subscription = tasks.onDidEndTaskProcess(
(e: TaskProcessEndEvent) => {
if (e.execution.task.name === command) {
actor.system.get('polling').send({ type: 'WAKE_UP' });
updateRecentCIPE();
subscription.dispose();
}
Expand Down

0 comments on commit 2c4b4d9

Please sign in to comment.