The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Convert restart project and restart project services to RQ and refactor RestartServerButton to use them
Joshen Lim3fcc13530a93
diff --git a/studio/components/interfaces/Settings/General/Infrastructure/RestartServerButton.tsx b/studio/components/interfaces/Settings/General/Infrastructure/RestartServerButton.tsxindex 0c3bd1bd5c8..ba877252f1d 100644--- a/studio/components/interfaces/Settings/General/Infrastructure/RestartServerButton.tsx+++ b/studio/components/interfaces/Settings/General/Infrastructure/RestartServerButton.tsx@@ -6,41 +6,57 @@ import { useRouter } from 'next/router'import { useState } from 'react'import { Button, Dropdown, IconChevronDown } from 'ui'-import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'-import { setProjectPostgrestStatus } from 'data/projects/projects-query'-import { useCheckPermissions, useStore } from 'hooks'-import { post } from 'lib/common/fetch'-import { API_URL } from 'lib/constants'import {useIsProjectActive,useProjectContext,} from 'components/layouts/ProjectLayout/ProjectContext'+import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'+import { useProjectRestartMutation } from 'data/projects/project-restart-mutation'+import { useProjectRestartServicesMutation } from 'data/projects/project-restart-services-mutation'+import { setProjectPostgrestStatus } from 'data/projects/projects-query'+import { useCheckPermissions, useStore } from 'hooks'const RestartServerButton = () => {- const queryClient = useQueryClient()- const { project } = useProjectContext()const { ui } = useStore()const router = useRouter()+ const queryClient = useQueryClient()+ const { project } = useProjectContext()const isProjectActive = useIsProjectActive()- const [loading, setLoading] = useState(false)const [serviceToRestart, setServiceToRestart] = useState<'project' | 'database'>()const projectRef = project?.ref ?? ''- const projectRegion = project?.region+ const projectRegion = project?.region ?? ''const canRestartProject = useCheckPermissions(PermissionAction.INFRA_EXECUTE, 'reboot')- const requestProjectRestart = async () => {+ const { mutate: restartProject, isLoading: isRestartingProject } = useProjectRestartMutation({+ onSuccess: () => {+ onRestartSuccess()+ },+ onError: (error) => {+ onRestartFailed(error, 'project')+ },+ })+ const { mutate: restartProjectServices, isLoading: isRestartingServices } =+ useProjectRestartServicesMutation({+ onSuccess: () => {+ onRestartSuccess()+ },+ onError: (error) => {+ onRestartFailed(error, 'database')+ },+ })++ const isLoading = isRestartingProject || isRestartingServices++ const requestProjectRestart = () => {if (!canRestartProject) {return ui.setNotification({category: 'error',message: 'You do not have the required permissions to restart this project',})}- setLoading(true)- const res = await post(`${API_URL}/projects/${projectRef}/restart`, {})- if (res.error) onRestartFailed(res.error, 'project')- else onRestartSuccess()+ restartProject({ ref: projectRef })}const requestDatabaseRestart = async () => {@@ -50,15 +66,7 @@ const RestartServerButton = () => {message: 'You do not have the required permissions to restart this project',})}- setLoading(true)- const res = await post(`${API_URL}/projects/${projectRef}/restart-services`, {- restartRequest: {- region: projectRegion,- services: ['postgresql'],- },- })- if (res.error) onRestartFailed(res.error, 'database')- else onRestartSuccess()+ restartProjectServices({ ref: projectRef, region: projectRegion, services: ['postgresql'] })}const onRestartFailed = (error: any, type: string) => {@@ -67,15 +75,13 @@ const RestartServerButton = () => {category: 'error',message: `Unable to restart ${type}: ${error.message}`,})- setLoading(false)setServiceToRestart(undefined)}const onRestartSuccess = () => {setProjectPostgrestStatus(queryClient, projectRef, 'OFFLINE')- ui.setNotification({ category: 'success', message: 'Restarting server' })+ ui.setNotification({ category: 'success', message: 'Restarting server...' })router.push(`/project/${projectRef}`)- setLoading(false)setServiceToRestart(undefined)}@@ -99,7 +105,7 @@ const RestartServerButton = () => {overlay={[<Dropdown.Itemkey="database"- disabled={loading}+ disabled={isLoading}onClick={() => {setServiceToRestart('database')}}diff --git a/studio/data/projects/project-restart-mutation.ts b/studio/data/projects/project-restart-mutation.tsnew file mode 100644index 00000000000..13ced13112d--- /dev/null+++ b/studio/data/projects/project-restart-mutation.ts@@ -0,0 +1,45 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import { toast } from 'react-hot-toast'++import { post } from 'data/fetchers'+import { ResponseError } from 'types'++export type ProjectRestartVariables = {+ ref: string+}++export async function restartProject({ ref }: ProjectRestartVariables) {+ const { data, error } = await post('/platform/projects/{ref}/restart', {+ params: { path: { ref } },+ })+ if (error) throw error+ return data+}++type ProjectRestartData = Awaited<ReturnType<typeof restartProject>>++export const useProjectRestartMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<ProjectRestartData, ResponseError, ProjectRestartVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<ProjectRestartData, ResponseError, ProjectRestartVariables>(+ (vars) => restartProject(vars),+ {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to restart project: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/studio/data/projects/project-restart-services-mutation.ts b/studio/data/projects/project-restart-services-mutation.tsnew file mode 100644index 00000000000..c3e0d4f3383--- /dev/null+++ b/studio/data/projects/project-restart-services-mutation.ts@@ -0,0 +1,71 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import { toast } from 'react-hot-toast'++import { post } from 'data/fetchers'+import { ResponseError } from 'types'++export type ProjectRestartServicesVariables = {+ ref: string+ region: string+ services: (+ | 'postgresql'+ | 'adminapi'+ | 'api-gateway'+ | 'functions'+ | 'gotrue'+ | 'kong'+ | 'pgbouncer'+ | 'pgsodium'+ | 'postgrest'+ | 'realtime'+ | 'storage'+ | 'walg'+ | 'autoshutdown'+ )[]+}++export async function restartProjectServices({+ ref,+ region,+ services = ['postgresql'],+}: ProjectRestartServicesVariables) {+ const { data, error } = await post('/platform/projects/{ref}/restart-services', {+ params: { path: { ref } },+ body: {+ restartRequest: {+ region,+ services,+ },+ },+ })+ if (error) throw error+ return data+}++type ProjectRestartServicesData = Awaited<ReturnType<typeof restartProjectServices>>++export const useProjectRestartServicesMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<ProjectRestartServicesData, ResponseError, ProjectRestartServicesVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<ProjectRestartServicesData, ResponseError, ProjectRestartServicesVariables>(+ (vars) => restartProjectServices(vars),+ {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to restart project: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}