chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Tests for the workspace-events no-activity polling cron route.
|
||||
*
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { createMockRequest, redisConfigMock, redisConfigMockFns } from '@sim/testing'
|
||||
import { sleep } from '@sim/utils/helpers'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { mockVerifyCronAuth, mockPollNoActivityEvents } = vi.hoisted(() => ({
|
||||
mockVerifyCronAuth: vi.fn().mockReturnValue(null),
|
||||
mockPollNoActivityEvents: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ subscriptions: 0, checked: 0, fired: 0, skipped: 0 }),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/auth/internal', () => ({
|
||||
verifyCronAuth: mockVerifyCronAuth,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/core/config/redis', () => redisConfigMock)
|
||||
|
||||
vi.mock('@/lib/workspace-events/no-activity', () => ({
|
||||
pollNoActivityEvents: mockPollNoActivityEvents,
|
||||
}))
|
||||
|
||||
import { GET } from './route'
|
||||
|
||||
function createRequest() {
|
||||
return createMockRequest('GET', undefined, {}, 'http://localhost:3000/api/workspace-events/poll')
|
||||
}
|
||||
|
||||
const flushMicrotasks = () => sleep(0)
|
||||
|
||||
describe('workspace events polling route (fire-and-forget)', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
redisConfigMockFns.mockAcquireLock.mockResolvedValue(true)
|
||||
redisConfigMockFns.mockReleaseLock.mockResolvedValue(true)
|
||||
mockVerifyCronAuth.mockReturnValue(null)
|
||||
mockPollNoActivityEvents.mockResolvedValue({
|
||||
subscriptions: 0,
|
||||
checked: 0,
|
||||
fired: 0,
|
||||
skipped: 0,
|
||||
})
|
||||
})
|
||||
|
||||
it('returns the auth error when cron auth fails', async () => {
|
||||
mockVerifyCronAuth.mockReturnValueOnce(new Response(null, { status: 401 }) as never)
|
||||
|
||||
const response = await GET(createRequest())
|
||||
|
||||
expect(response.status).toBe(401)
|
||||
expect(mockPollNoActivityEvents).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('acknowledges with 202 and polls in the background after acquiring the lock', async () => {
|
||||
const response = await GET(createRequest())
|
||||
|
||||
expect(response.status).toBe(202)
|
||||
const data = await response.json()
|
||||
expect(data).toMatchObject({ status: 'started' })
|
||||
expect(redisConfigMockFns.mockAcquireLock).toHaveBeenCalledWith(
|
||||
'workspace-events-no-activity-poll-lock',
|
||||
expect.any(String),
|
||||
expect.any(Number)
|
||||
)
|
||||
|
||||
await flushMicrotasks()
|
||||
expect(mockPollNoActivityEvents).toHaveBeenCalledTimes(1)
|
||||
expect(redisConfigMockFns.mockReleaseLock).toHaveBeenCalledWith(
|
||||
'workspace-events-no-activity-poll-lock',
|
||||
expect.any(String)
|
||||
)
|
||||
})
|
||||
|
||||
it('skips with 202 when the lock is already held', async () => {
|
||||
redisConfigMockFns.mockAcquireLock.mockResolvedValueOnce(false)
|
||||
|
||||
const response = await GET(createRequest())
|
||||
|
||||
expect(response.status).toBe(202)
|
||||
const data = await response.json()
|
||||
expect(data).toMatchObject({ status: 'skip' })
|
||||
expect(mockPollNoActivityEvents).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('releases the lock even when polling throws', async () => {
|
||||
mockPollNoActivityEvents.mockRejectedValueOnce(new Error('poll failed'))
|
||||
|
||||
const response = await GET(createRequest())
|
||||
|
||||
expect(response.status).toBe(202)
|
||||
await flushMicrotasks()
|
||||
expect(redisConfigMockFns.mockReleaseLock).toHaveBeenCalledWith(
|
||||
'workspace-events-no-activity-poll-lock',
|
||||
expect.any(String)
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,77 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { getErrorMessage } from '@sim/utils/errors'
|
||||
import { generateShortId } from '@sim/utils/id'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { noInputSchema } from '@/lib/api/contracts/primitives'
|
||||
import { validationErrorResponse } from '@/lib/api/server'
|
||||
import { verifyCronAuth } from '@/lib/auth/internal'
|
||||
import { acquireLock, releaseLock } from '@/lib/core/config/redis'
|
||||
import { runDetached } from '@/lib/core/utils/background'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import { pollNoActivityEvents } from '@/lib/workspace-events/no-activity'
|
||||
|
||||
const logger = createLogger('WorkspaceEventsPoll')
|
||||
|
||||
export const maxDuration = 120
|
||||
|
||||
const LOCK_KEY = 'workspace-events-no-activity-poll-lock'
|
||||
const LOCK_TTL_SECONDS = 120
|
||||
|
||||
export const GET = withRouteHandler(async (request: NextRequest) => {
|
||||
const requestId = generateShortId()
|
||||
logger.info(`Workspace events no-activity polling triggered (${requestId})`)
|
||||
const queryValidation = noInputSchema.safeParse(
|
||||
Object.fromEntries(request.nextUrl.searchParams.entries())
|
||||
)
|
||||
if (!queryValidation.success) return validationErrorResponse(queryValidation.error)
|
||||
|
||||
try {
|
||||
const authError = verifyCronAuth(request, 'Workspace events polling')
|
||||
if (authError) {
|
||||
return authError
|
||||
}
|
||||
|
||||
const lockAcquired = await acquireLock(LOCK_KEY, requestId, LOCK_TTL_SECONDS)
|
||||
|
||||
if (!lockAcquired) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: 'Polling already in progress – skipped',
|
||||
requestId,
|
||||
status: 'skip',
|
||||
},
|
||||
{ status: 202 }
|
||||
)
|
||||
}
|
||||
|
||||
runDetached('workspace-events-no-activity-polling', async () => {
|
||||
try {
|
||||
await pollNoActivityEvents()
|
||||
} finally {
|
||||
await releaseLock(LOCK_KEY, requestId).catch(() => {})
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: 'Workspace events polling started',
|
||||
requestId,
|
||||
status: 'started',
|
||||
},
|
||||
{ status: 202 }
|
||||
)
|
||||
} catch (error) {
|
||||
logger.error(`Error during workspace events polling (${requestId}):`, error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Workspace events polling failed',
|
||||
error: getErrorMessage(error, 'Unknown error'),
|
||||
requestId,
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user