-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(core): add logging and progress message types to daemon #35342
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ecca529
feat(core): add streaming update-progress/emit-log daemon messages
claude 2f94962
feat(core): forward plugin worker log/progress to daemon client
claude b12a456
feat(core): surface plugin progress and cache warnings from daemon
claude a598127
refactor(core): tighten streaming helpers to daemon-only
claude 0c31a66
refactor(core): relocate streaming helpers per review feedback
claude a7bab9e
feat(core): route daemon streaming progress through named topics
AgentEnder 201e12a
chore(core): extract ProgressTopics into utils/progress-topics
AgentEnder 7d19437
fix(core): stop subscribing HASH_TASKS to graph-construction progress
AgentEnder 7cc774d
fix(core): route daemon streaming progress through per-request spinner
AgentEnder f64e9e8
fix(core): code review feedback
AgentEnder 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
36 changes: 36 additions & 0 deletions
36
packages/nx/src/daemon/message-types/streaming-messages.ts
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,36 @@ | ||
| export const UPDATE_PROGRESS_MESSAGE = 'UPDATE_PROGRESS_MESSAGE' as const; | ||
|
|
||
| export type UpdateProgressMessage = { | ||
| type: typeof UPDATE_PROGRESS_MESSAGE; | ||
| message: string; | ||
| }; | ||
|
|
||
| export function isUpdateProgressMessage( | ||
| message: unknown | ||
| ): message is UpdateProgressMessage { | ||
| return ( | ||
| typeof message === 'object' && | ||
| message !== null && | ||
| 'type' in message && | ||
| message['type'] === UPDATE_PROGRESS_MESSAGE | ||
| ); | ||
| } | ||
|
|
||
| export const EMIT_LOG = 'EMIT_LOG' as const; | ||
|
|
||
| export type EmitLogLevel = 'log' | 'warn' | 'error'; | ||
|
|
||
| export type EmitLogMessage = { | ||
| type: typeof EMIT_LOG; | ||
| level: EmitLogLevel; | ||
| message: string; | ||
| }; | ||
|
|
||
| export function isEmitLogMessage(message: unknown): message is EmitLogMessage { | ||
| return ( | ||
| typeof message === 'object' && | ||
| message !== null && | ||
| 'type' in message && | ||
| message['type'] === EMIT_LOG | ||
| ); | ||
| } |
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,120 @@ | ||
| import type { Socket } from 'net'; | ||
| import { MESSAGE_END_SEQ } from '../../utils/consume-messages-from-socket'; | ||
| import { ProgressTopic } from '../../utils/progress-topics'; | ||
| import { isOnDaemon } from '../is-on-daemon'; | ||
| import { serverLogger } from '../logger'; | ||
| import { | ||
| EMIT_LOG, | ||
| EmitLogLevel, | ||
| EmitLogMessage, | ||
| UPDATE_PROGRESS_MESSAGE, | ||
| } from '../message-types/streaming-messages'; | ||
| import { serialize } from '../socket-utils'; | ||
|
|
||
| const topicSubscribers = new Map<ProgressTopic, Set<Socket>>(); | ||
|
|
||
| export function subscribeClientToTopic( | ||
| socket: Socket, | ||
| topic: ProgressTopic | ||
| ): void { | ||
| let subscribers = getTopicSubscribers(topic); | ||
| if (!subscribers) { | ||
| subscribers = new Set(); | ||
| topicSubscribers.set(topic, subscribers); | ||
| } | ||
| subscribers.add(socket); | ||
| } | ||
|
|
||
| export function unsubscribeClientFromTopic( | ||
| socket: Socket, | ||
| topic: ProgressTopic | ||
| ): void { | ||
| const subscribers = getTopicSubscribers(topic); | ||
| if (!subscribers) return; | ||
| subscribers.delete(socket); | ||
| } | ||
|
|
||
| export function getTopicSubscribers(topic: ProgressTopic): Set<Socket> { | ||
| const subscribers = topicSubscribers.get(topic); | ||
| if (!subscribers) { | ||
| const set = new Set<Socket>(); | ||
| topicSubscribers.set(topic, set); | ||
| return set; | ||
| } | ||
| return subscribers; | ||
| } | ||
|
|
||
| export function assertOnDaemon(helperName: string) { | ||
| if (!isOnDaemon()) { | ||
| throw new Error( | ||
| `${helperName} can only be called from the Nx daemon process.` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Writes a streaming message over the given socket using the daemon's | ||
| * configured serialization format and terminated with MESSAGE_END_SEQ. | ||
| * Errors are logged to the daemon's stdout (redirected to the daemon | ||
| * log) rather than propagated — a disconnected client shouldn't tear | ||
| * down the current request handler or other subscribers. | ||
| */ | ||
| export function writeStreamingMessage( | ||
| socket: Socket, | ||
| payload: unknown, | ||
| description: string | ||
| ) { | ||
| try { | ||
| serverLogger.log('Streaming message to client:', description); | ||
| socket.write(serialize(payload) + MESSAGE_END_SEQ, (err) => { | ||
| if (err) { | ||
| console.log( | ||
| `Streaming message write error (client likely disconnected): ${err.message}` | ||
| ); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| console.log( | ||
| `Failed to send streaming message to client: ${ | ||
| e instanceof Error ? e.message : String(e) | ||
| }` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Broadcasts a progress message to every client currently subscribed to | ||
| * the given topic. No-op when there are no subscribers. | ||
| * | ||
| * Must only be invoked from inside the Nx daemon process. | ||
| */ | ||
| export function sendProgressMessageToTopic( | ||
| topic: ProgressTopic, | ||
| message: string | ||
| ): void { | ||
| assertOnDaemon('sendProgressMessageToTopic'); | ||
| const subscribers = getTopicSubscribers(topic); | ||
| if (!subscribers?.size) return; | ||
| const payload = { type: UPDATE_PROGRESS_MESSAGE, message }; | ||
| for (const socket of subscribers) { | ||
| writeStreamingMessage( | ||
| socket, | ||
| payload, | ||
| 'progress update for topic ' + topic | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export function sendEmitLogMessageToTopic( | ||
| topic: ProgressTopic, | ||
| message: string, | ||
| level: EmitLogLevel | ||
| ): void { | ||
| assertOnDaemon('sendEmitLogMessageToTopic'); | ||
| const subscribers = getTopicSubscribers(topic); | ||
| if (!subscribers?.size) return; | ||
| const payload: EmitLogMessage = { type: EMIT_LOG, message, level }; | ||
| for (const socket of subscribers) { | ||
| writeStreamingMessage(socket, payload, 'emit log message to ' + topic); | ||
| } | ||
| } | ||
7 changes: 5 additions & 2 deletions
7
packages/nx/src/daemon/server/handle-request-project-graph.ts
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look at
respondToClientfromshutdown-utilsand borrow some ideas like logging this into the server logs etc.