-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
background asset wipe #26128
base: master
Are you sure you want to change the base?
background asset wipe #26128
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.
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'; | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
} | ||
Comment on lines
+47
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid setting state or performing side effects within the render path. These should occur in callbacks or effects. |
||
|
||
const wipeAssets = async (assetPartitionRanges: PartitionsByAssetSelector[]) => { | ||
if (!assetPartitionRanges.length) { | ||
return; | ||
|
@@ -66,13 +105,43 @@ export function useWipeAssets({ | |
setIsWiping(false); | ||
}; | ||
|
||
const backgroundWipeAssets = async (assetPartitionRanges: PartitionsByAssetSelector[]) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming nit: maybe |
||
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; | ||
Comment on lines
+118
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be helpful to surface the errors.
|
||
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` | ||
|
@@ -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 | ||
} | ||
} | ||
} | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This object should also include functions
startPolling
andstopPolling
that you can use. You might not needstartPolling
since you can provide thepollInterval
at the callsite, but I think you will want to explicitly stop polling when the wipe has succeeded or failed.