Skip to content

Commit

Permalink
background asset wipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Fulwiler committed Dec 9, 2024
1 parent c64a0f8 commit cda4cfb
Show file tree
Hide file tree
Showing 15 changed files with 584 additions and 34 deletions.
2 changes: 2 additions & 0 deletions js_modules/dagster-ui/packages/ui-core/client.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 102 additions & 3 deletions js_modules/dagster-ui/packages/ui-core/src/assets/useWipeAssets.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {useLayoutEffect, useRef, useState} from 'react';

import {RefetchQueriesFunction, gql, useMutation} from '../apollo-client';
import {AssetWipeMutation, AssetWipeMutationVariables} from './types/useWipeAssets.types';
import {RefetchQueriesFunction, gql, useLazyQuery, useMutation} from '../apollo-client';
import {
AssetWipeMutation,
AssetWipeMutationVariables,
BackgroundAssetWipeMutation,
BackgroundAssetWipeMutationVariables,
BackgroundAssetWipeStatusQuery,
} from './types/useWipeAssets.types';
import {showCustomAlert} from '../app/CustomAlertProvider';
import {PYTHON_ERROR_FRAGMENT} from '../app/PythonErrorFragment';
import {PartitionsByAssetSelector} from '../graphql/types';
Expand All @@ -21,6 +27,14 @@ export function useWipeAssets({
ASSET_WIPE_MUTATION,
{refetchQueries},
);
const [requestBackgroundAssetWipe] = useMutation<
BackgroundAssetWipeMutation,
BackgroundAssetWipeMutationVariables
>(BACKGROUND_ASSET_WIPE_MUTATION, {refetchQueries});
const [
getBackgroundWipeStatus,
{error: backgroundWipeStatusError, data: backgroundWipeStatusData},
] = useLazyQuery<BackgroundAssetWipeStatusQuery>(BACKGROUND_ASSET_WIPE_STATUS);

const [isWiping, setIsWiping] = useState(false);
const [wipedCount, setWipedCount] = useState(0);
Expand All @@ -30,6 +44,31 @@ export function useWipeAssets({

const didCancel = useRef(false);

if (isWiping && backgroundWipeStatusError) {
setFailedCount(1);
onComplete?.();
setIsWiping(false);
}

if (isWiping && backgroundWipeStatusData) {
const data = backgroundWipeStatusData.backgroundAssetWipeStatus;
switch (data.__typename) {
case 'BackgroundAssetWipeInProgress':
console.log('Background asset wipe in progress');
break;
case 'BackgroundAssetWipeSuccess':
setWipedCount(1);
onComplete?.();
setIsWiping(false);
break;
case 'BackgroundAssetWipeFailed':
setFailedCount(1);
onComplete?.();
setIsWiping(false);
break;
}
}

const wipeAssets = async (assetPartitionRanges: PartitionsByAssetSelector[]) => {
if (!assetPartitionRanges.length) {
return;
Expand Down Expand Up @@ -66,13 +105,43 @@ export function useWipeAssets({
setIsWiping(false);
};

const backgroundWipeAssets = async (assetPartitionRanges: PartitionsByAssetSelector[]) => {
if (!assetPartitionRanges.length) {
return;
}
setIsWiping(true);
const result = await requestBackgroundAssetWipe({
variables: {assetPartitionRanges},
refetchQueries,
});
const data = result.data?.backgroundWipeAssets;
switch (data?.__typename) {
case 'AssetNotFoundError':
case 'PythonError':
setFailedCount(assetPartitionRanges.length);
onComplete?.();
setIsWiping(false);
return;
case 'AssetWipeInProgress':
getBackgroundWipeStatus({variables: {workToken: data.workToken}, pollInterval: 1000});
break;
case 'UnauthorizedError':
showCustomAlert({
title: 'Could not wipe asset materializations',
body: 'You do not have permission to do this.',
});
onClose();
return;
}
};

useLayoutEffect(() => {
return () => {
didCancel.current = true;
};
}, []);

return {wipeAssets, isWiping, isDone, wipedCount, failedCount};
return {backgroundWipeAssets, wipeAssets, isWiping, isDone, wipedCount, failedCount};
}

export const ASSET_WIPE_MUTATION = gql`
Expand All @@ -95,3 +164,33 @@ export const ASSET_WIPE_MUTATION = gql`
${PYTHON_ERROR_FRAGMENT}
`;

export const BACKGROUND_ASSET_WIPE_MUTATION = gql`
mutation BackgroundAssetWipeMutation($assetPartitionRanges: [PartitionsByAssetSelector!]!) {
backgroundWipeAssets(assetPartitionRanges: $assetPartitionRanges) {
... on AssetWipeInProgress {
workToken
}
...PythonErrorFragment
}
}
${PYTHON_ERROR_FRAGMENT}
`;

export const BACKGROUND_ASSET_WIPE_STATUS = gql`
query BackgroundAssetWipeStatus($workToken: String!) {
backgroundAssetWipeStatus(workToken: $workToken) {
... on BackgroundAssetWipeSuccess {
completedAt
}
... on BackgroundAssetWipeInProgress {
startedAt
}
... on BackgroundAssetWipeFailed {
failedAt
message
}
}
}
`;
Loading

0 comments on commit cda4cfb

Please sign in to comment.