-
Notifications
You must be signed in to change notification settings - Fork 12
Extract RealmServer class methods into per-concern handler modules #4846
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
54a9421
Extract RealmServer class methods into per-concern handler modules
habdelra e189a72
Drop unused virtualNetwork from ServeIndexDeps
habdelra be11127
Re-expose retrieveIndexHTML from createServeIndex; drive test via fac…
habdelra b62b6e8
createRealm: enqueue exactly one priority-10 index job
habdelra acd64ac
createRealm: mount before publishing the index job
habdelra 392ea9d
createRealm: thread fromScratchIndexPriority instead of skipping
habdelra 0c9a30c
Merge pull request #4849 from cardstack/cs-11157-skip-mount-indexing
habdelra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import type { DBAdapter } from '@cardstack/runtime-common'; | ||
| import { fetchSessionRoom } from '@cardstack/runtime-common'; | ||
| import type { MatrixClient } from '@cardstack/runtime-common/matrix-client'; | ||
| import { APP_BOXEL_REALM_SERVER_EVENT_MSGTYPE } from '@cardstack/runtime-common/matrix-constants'; | ||
|
|
||
| export type SendEventDeps = { | ||
| matrixClient: MatrixClient; | ||
| dbAdapter: DBAdapter; | ||
| }; | ||
|
|
||
| export type SendEvent = ( | ||
| user: string, | ||
| eventType: string, | ||
| data?: Record<string, any>, | ||
| ) => Promise<void>; | ||
|
|
||
| export function createSendEvent({ | ||
| matrixClient, | ||
| dbAdapter, | ||
| }: SendEventDeps): SendEvent { | ||
| return async function sendEvent(user, eventType, data) { | ||
| if (!matrixClient.isLoggedIn()) { | ||
| await matrixClient.login(); | ||
| } | ||
| let roomId = await fetchSessionRoom(dbAdapter, user); | ||
| if (!roomId) { | ||
| console.error( | ||
| `Failed to send event: ${eventType}, cannot find session room for user: ${user}`, | ||
| ); | ||
| } | ||
|
|
||
| await matrixClient.sendEvent(roomId!, 'm.room.message', { | ||
| body: JSON.stringify({ eventType, data }), | ||
| msgtype: APP_BOXEL_REALM_SERVER_EVENT_MSGTYPE, | ||
| }); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import type Koa from 'koa'; | ||
| import type { | ||
| DBAdapter, | ||
| Realm, | ||
| VirtualNetwork, | ||
| } from '@cardstack/runtime-common'; | ||
| import { logger } from '@cardstack/runtime-common'; | ||
| import { fetchRequestFromContext, setContextResponse } from '../middleware'; | ||
| import { setupCloseHandler } from '../node-realm'; | ||
| import { findOrMountRealm } from '../lib/realm-routing'; | ||
| import type { RealmRegistryReconciler } from '../lib/realm-registry-reconciler'; | ||
|
|
||
| export type ServeFromRealmDeps = { | ||
| realms: Realm[]; | ||
| reconciler: RealmRegistryReconciler; | ||
| dbAdapter: DBAdapter; | ||
| virtualNetwork: VirtualNetwork; | ||
| }; | ||
|
|
||
| const log = logger('realm-server'); | ||
|
|
||
| export function createServeFromRealm( | ||
| deps: ServeFromRealmDeps, | ||
| ): (ctxt: Koa.Context, next: Koa.Next) => Promise<void> { | ||
| let { virtualNetwork } = deps; | ||
| return async function serveFromRealm(ctxt: Koa.Context, _next: Koa.Next) { | ||
| if (ctxt.request.path === '/_boom') { | ||
| throw new Error('boom'); | ||
| } | ||
| let request = await fetchRequestFromContext(ctxt); | ||
| // Phase 3 lazy mount: trigger findOrMountRealm before dispatching to | ||
| // virtualNetwork.handle so non-pinned realms (source/published) mount | ||
| // on first request. virtualNetwork.handle returns 404 for any URL | ||
| // whose handle isn't registered, which is exactly what happens for | ||
| // a realm that the reconciler knows about (knownByUrl) but hasn't | ||
| // mounted yet. findOrMountRealm walks knownByUrl, calls | ||
| // reconciler.lookupOrMount() on a prefix match, and that | ||
| // synchronously publishes the realm into virtualNetwork before the | ||
| // dispatch below. Mount failures throw — the catch turns them into | ||
| // 503 so the next request retries from scratch (ensureMounted's | ||
| // failure path clears mounted/pendingMounts). | ||
| let requestURL = new URL( | ||
| `${ctxt.protocol}://${ctxt.host}${ctxt.originalUrl}`, | ||
| ); | ||
| try { | ||
| await findOrMountRealm(requestURL, deps); | ||
| } catch (err: any) { | ||
| log.warn( | ||
| `failed to mount realm for request ${requestURL.href}: ${err?.message ?? err}`, | ||
| ); | ||
| ctxt.status = 503; | ||
| ctxt.body = `Realm mount failed: ${err?.message ?? err}`; | ||
| return; | ||
| } | ||
| let realmResponse = await virtualNetwork.handle( | ||
| request, | ||
| (mappedRequest) => { | ||
| // Setup this handler only after the request has been mapped because | ||
| // the *mapped request* is the one that gets closed, not the original one | ||
| setupCloseHandler(ctxt.res, mappedRequest); | ||
| }, | ||
| ); | ||
|
|
||
| await setContextResponse(ctxt, realmResponse); | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.