Skip to content

Commit 32d7a1d

Browse files
authored
feat: only have a maximum of 2 CodeBuild running (#844)
* feat: only have a maximum of 2 CodeBuild running * fix: perform invalidations when updating page
1 parent 3976773 commit 32d7a1d

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

apps/studio/src/features/editing-experience/components/ComplexEditorStateDrawer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export default function ComplexEditorStateDrawer(): JSX.Element {
6161
trpc.page.updatePageBlob.useMutation({
6262
onSuccess: async () => {
6363
await utils.page.readPageAndBlob.invalidate({ pageId, siteId })
64+
await utils.page.readPage.invalidate({ pageId, siteId })
6465
toast({
6566
title: CHANGES_SAVED_PLEASE_PUBLISH_MESSAGE,
6667
...BRIEF_TOAST_SETTINGS,

apps/studio/src/features/editing-experience/components/HeroEditorDrawer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default function HeroEditorDrawer(): JSX.Element {
5454
trpc.page.updatePageBlob.useMutation({
5555
onSuccess: async () => {
5656
await utils.page.readPageAndBlob.invalidate({ pageId, siteId })
57+
await utils.page.readPage.invalidate({ pageId, siteId })
5758
toast({
5859
title: CHANGES_SAVED_PLEASE_PUBLISH_MESSAGE,
5960
...BRIEF_TOAST_SETTINGS,

apps/studio/src/features/editing-experience/components/MetadataEditorStateDrawer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default function MetadataEditorStateDrawer(): JSX.Element {
4343
const { mutate, isLoading } = trpc.page.updatePageBlob.useMutation({
4444
onSuccess: async () => {
4545
await utils.page.readPageAndBlob.invalidate({ pageId, siteId })
46+
await utils.page.readPage.invalidate({ pageId, siteId })
4647
},
4748
})
4849

apps/studio/src/features/editing-experience/components/RootStateDrawer.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ export default function RootStateDrawer() {
4141
} = useEditorDrawerContext()
4242

4343
const { pageId, siteId } = useQueryParse(editPageSchema)
44+
const utils = trpc.useUtils()
4445
const { mutate } = trpc.page.reorderBlock.useMutation({
46+
onSuccess: async () => {
47+
await utils.page.readPage.invalidate({ pageId, siteId })
48+
},
4549
onError: (error, variables) => {
4650
// NOTE: rollback to last known good state
4751
// @ts-expect-error Our zod validator runs between frontend and backend

apps/studio/src/features/editing-experience/components/TipTapProseComponent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function TipTapProseComponent({ content }: TipTapComponentProps) {
5151
const { mutate, isLoading } = trpc.page.updatePageBlob.useMutation({
5252
onSuccess: async () => {
5353
await utils.page.readPageAndBlob.invalidate({ pageId, siteId })
54+
await utils.page.readPage.invalidate({ pageId, siteId })
5455
toast({
5556
title: CHANGES_SAVED_PLEASE_PUBLISH_MESSAGE,
5657
...BRIEF_TOAST_SETTINGS,

apps/studio/src/server/modules/aws/codebuild.service.ts

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CodeBuildClient,
66
ListBuildsForProjectCommand,
77
StartBuildCommand,
8+
StopBuildCommand,
89
} from "@aws-sdk/client-codebuild"
910

1011
import { getSiteNameAndCodeBuildId } from "../site/site.service"
@@ -73,25 +74,63 @@ export const shouldStartNewBuild = async (
7374
const batchGetBuildsCommand = new BatchGetBuildsCommand({ ids: buildIds })
7475
const batchGetBuildsResponse = await client.send(batchGetBuildsCommand)
7576

77+
// Case 1: Start a new build if there are no existing running builds
78+
const runningBuilds = batchGetBuildsResponse.builds?.filter(
79+
(build) => build.buildStatus === "IN_PROGRESS",
80+
)
81+
82+
if (runningBuilds?.length === 0) {
83+
return true
84+
}
85+
86+
// Case 2: Start a new build if there is only 1 running build, and it is
87+
// not recent
7688
// Find for running builds that are considered recent
77-
const recentRunningBuilds =
78-
batchGetBuildsResponse.builds?.filter((build) => {
89+
const recentRunningBuilds = batchGetBuildsResponse.builds?.filter(
90+
(build) => {
7991
const buildStartTime = new Date(build.startTime ?? "")
8092
return (
8193
build.buildStatus === "IN_PROGRESS" &&
8294
buildStartTime > thresholdTimeAgo
8395
)
84-
}) ?? []
85-
86-
if (recentRunningBuilds.length > 0) {
87-
logger.info(
88-
{ projectId, buildIds: recentRunningBuilds.map((build) => build.id) },
89-
"Not starting a new build as there are recent builds that are still running",
90-
)
91-
return false
96+
},
97+
)
98+
99+
if (runningBuilds?.length === 1 && recentRunningBuilds?.length === 0) {
100+
return true
101+
}
102+
103+
// Case 3: Start a new build if there are 2 running builds, and both are
104+
// not recent. We will stop one of the builds to start a new one.
105+
if (runningBuilds?.length === 2 && recentRunningBuilds?.length === 0) {
106+
// Stop the latest build
107+
const latestBuild = runningBuilds
108+
.sort((a, b) => {
109+
const aStartTime = new Date(a.startTime ?? "")
110+
const bStartTime = new Date(b.startTime ?? "")
111+
return bStartTime.getTime() - aStartTime.getTime()
112+
})
113+
.at(0)
114+
115+
if (!latestBuild) {
116+
logger.error(
117+
{ projectId },
118+
"Unable to determine the latest build to stop",
119+
)
120+
return true
121+
}
122+
123+
const stopBuildCommand = new StopBuildCommand({
124+
id: latestBuild.id,
125+
})
126+
127+
await client.send(stopBuildCommand)
128+
129+
return true
92130
}
93131

94-
return true
132+
// Any other case, we should not start a new build
133+
return false
95134
} catch (error) {
96135
logger.error(
97136
{ projectId, error },

0 commit comments

Comments
 (0)