|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { useQueryClient } from "@tanstack/react-query"; |
| 20 | +import { useEffect, useRef } from "react"; |
| 21 | + |
| 22 | +import { |
| 23 | + useDagRunServiceGetDagRuns, |
| 24 | + useDagServiceGetDagDetailsKey, |
| 25 | + UseDagRunServiceGetDagRunsKeyFn, |
| 26 | + UseDagServiceGetDagDetailsKeyFn, |
| 27 | + useDagsServiceRecentDagRunsKey, |
| 28 | + UseGridServiceGridDataKeyFn, |
| 29 | + UseTaskInstanceServiceGetTaskInstancesKeyFn, |
| 30 | +} from "openapi/queries"; |
| 31 | + |
| 32 | +import { useConfig } from "./useConfig"; |
| 33 | + |
| 34 | +export const useRefreshOnNewDagRuns = (dagId: string, hasPendingRuns: boolean | undefined) => { |
| 35 | + const queryClient = useQueryClient(); |
| 36 | + const previousDagRunIdRef = useRef<string>(); |
| 37 | + const autoRefreshInterval = useConfig("auto_refresh_interval") as number; |
| 38 | + |
| 39 | + const { data } = useDagRunServiceGetDagRuns({ dagId, limit: 1, orderBy: "-run_after" }, undefined, { |
| 40 | + enabled: Boolean(dagId) && !hasPendingRuns, |
| 41 | + refetchInterval: Boolean(autoRefreshInterval) ? autoRefreshInterval * 1000 : 5000, |
| 42 | + }); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + const latestDagRun = data?.dag_runs[0]; |
| 46 | + |
| 47 | + const latestDagRunId = latestDagRun?.dag_run_id; |
| 48 | + |
| 49 | + if ((latestDagRunId ?? "") && previousDagRunIdRef.current !== latestDagRunId) { |
| 50 | + previousDagRunIdRef.current = latestDagRunId; |
| 51 | + |
| 52 | + const queryKeys = [ |
| 53 | + [useDagsServiceRecentDagRunsKey], |
| 54 | + [useDagServiceGetDagDetailsKey], |
| 55 | + UseDagServiceGetDagDetailsKeyFn({ dagId }, [{ dagId }]), |
| 56 | + UseDagRunServiceGetDagRunsKeyFn({ dagId }, [{ dagId }]), |
| 57 | + UseTaskInstanceServiceGetTaskInstancesKeyFn({ dagId, dagRunId: "~" }, [{ dagId, dagRunId: "~" }]), |
| 58 | + UseGridServiceGridDataKeyFn({ dagId }, [{ dagId }]), |
| 59 | + ]; |
| 60 | + |
| 61 | + queryKeys.forEach((key) => { |
| 62 | + void queryClient.invalidateQueries({ queryKey: key }); |
| 63 | + }); |
| 64 | + } |
| 65 | + }, [data, dagId, queryClient]); |
| 66 | +}; |
0 commit comments