Line data Source code
1 : import { until, useEventListener } from '@vueuse/core'
2 : import { storeToRefs } from 'pinia'
3 : import type { Ref } from 'vue'
4 : import { onMounted, onScopeDispose, ref } from 'vue'
5 : import { useI18n } from 'vue-i18n'
6 :
7 : import type {
8 : LogEntry,
9 : LogsWsMessage
10 : } from '@comfyorg/api-client/executionTypes'
11 : import { api } from '@/scripts/api'
12 : import { useExecutionStore } from '@/stores/executionStore'
13 :
14 : type TerminalLike = {
15 : write: (data: string) => void
16 : reset: () => void
17 : scrollToBottom: () => void
18 : }
19 :
20 : /**
21 : * Drives the built-in logs terminal: initial load, live `logs` stream, and
22 : * full resync when the backend WebSocket reconnects (e.g., after a reboot).
23 : *
24 : * Listeners are registered synchronously so we cannot miss a `reconnected`
25 : * event during the mount-time fetch/subscribe awaits. In-flight fetches are
26 : * tied to AbortControllers so that:
27 : * - rapid double-reconnects don't interleave writes / double-subscribe
28 : * - unmount mid-fetch never writes to a disposed terminal
29 : */
30 24 : export function useLogsTerminal(
31 : terminal: Readonly<Ref<TerminalLike | undefined>>
32 : ) {
33 24 : const { t } = useI18n()
34 24 : const errorMessage = ref('')
35 24 : const loading = ref(true)
36 :
37 24 : let mountController: AbortController | undefined
38 24 : let resyncController: AbortController | undefined
39 :
40 24 : const writeEntries = (entries: LogEntry[]) => {
41 31 : terminal.value?.write(entries.map((e) => e.m).join(''))
42 : }
43 :
44 24 : const resyncLogs = async () => {
45 : // Cancel both the in-flight mount fetch and any prior resync so a late
46 : // mount response can't write a stale snapshot on top of a freshly-reset
47 : // terminal after we've already written the post-reconnect view.
48 2 : mountController?.abort()
49 2 : resyncController?.abort()
50 2 : const controller = new AbortController()
51 2 : resyncController = controller
52 2 : const { signal } = controller
53 2 : const isAborted = () => signal.aborted
54 :
55 2 : try {
56 2 : const logs = await api.getRawLogs()
57 2 : if (signal.aborted || !terminal.value) return
58 2 : terminal.value.reset()
59 2 : writeEntries(logs.entries)
60 2 : terminal.value.scrollToBottom()
61 : // Backend lost the per-client log subscription across the restart;
62 : // re-subscribe so new runtime logs stream over the fresh WebSocket.
63 2 : await api.subscribeLogs(true)
64 2 : if (isAborted()) return
65 2 : errorMessage.value = ''
66 2 : loading.value = false
67 : } catch (err) {
68 0 : if (signal.aborted) return
69 0 : console.error('Error resyncing logs after reconnect', err)
70 0 : errorMessage.value = t('logsTerminal.resyncError')
71 : }
72 : }
73 :
74 : // Register listeners synchronously, before any awaits, so a reconnect
75 : // fired during mount cannot be missed. useEventListener handles cleanup
76 : // on scope dispose.
77 24 : useEventListener(api, 'logs', (e: CustomEvent<LogsWsMessage>) => {
78 9 : writeEntries(e.detail.entries)
79 : })
80 24 : useEventListener(api, 'reconnected', () => {
81 2 : void resyncLogs()
82 : })
83 :
84 24 : onMounted(async () => {
85 24 : if (!terminal.value) await until(terminal).toBeTruthy()
86 :
87 24 : const controller = new AbortController()
88 24 : mountController = controller
89 24 : const { signal } = controller
90 24 : const isAborted = () => signal.aborted
91 :
92 24 : try {
93 24 : const logs = await api.getRawLogs()
94 24 : if (signal.aborted || !terminal.value) return
95 20 : writeEntries(logs.entries)
96 : } catch (err) {
97 1 : if (signal.aborted) return
98 1 : console.error('Error loading logs', err)
99 1 : errorMessage.value = t('logsTerminal.loadError')
100 1 : loading.value = false
101 1 : return
102 : }
103 :
104 20 : const { clientId } = storeToRefs(useExecutionStore())
105 20 : if (!clientId.value) await until(clientId).not.toBeNull()
106 20 : if (isAborted()) return
107 :
108 20 : try {
109 20 : await api.subscribeLogs(true)
110 : } catch (err) {
111 0 : if (isAborted()) return
112 0 : console.error('Error subscribing to logs', err)
113 : }
114 :
115 20 : if (!isAborted()) loading.value = false
116 : })
117 :
118 24 : onScopeDispose(() => {
119 6 : mountController?.abort()
120 6 : resyncController?.abort()
121 6 : if (!api.clientId) return
122 6 : api.subscribeLogs(false).catch((err) => {
123 0 : console.error('Error unsubscribing from logs', err)
124 : })
125 : })
126 :
127 24 : return { errorMessage, loading }
128 : }
|