-
-
Notifications
You must be signed in to change notification settings - Fork 1
Allow enabling/disabling device sources and known devices #92
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
base: main
Are you sure you want to change the base?
Changes from all commits
ec6e912
2586e6f
6b3088e
a0489e6
f18c599
68da237
cd3e0db
d25ea81
bd140d6
c267f46
0f9d1d3
00088ea
bb40c9d
a03632a
9fea5d7
150ed88
4de46df
833c607
faf22f7
dd3a17b
d472a40
e661512
338aca2
3e16303
7ea3b0d
440a058
249e4a0
d68a8bd
448318b
1a9005c
e2bfc6b
1a6466c
72736a5
3ebf4a3
4dffc96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import ivm from 'isolated-vm'; | ||
| import { transform } from 'sucrase'; | ||
| import Device, { DeviceNotification } from '../device/device.js'; | ||
| import { AnyDevice, DeviceNotification } from '../device/device.js'; | ||
| import DeviceRepositoryInterface from '../repository/deviceRepositoryInterface.js'; | ||
| import fs, { WriteStream } from 'fs'; | ||
| import readLastLines from 'read-last-lines/dist/index.js'; | ||
|
|
@@ -11,8 +11,8 @@ import { AttributeValue } from '../device/attribute/deviceAttribute.js'; | |
| import Logger from '../logging/Logger.js'; | ||
|
|
||
| export type SupportedDeviceEvent = | ||
| | { type: DeviceManagerEvent.deviceConnected | DeviceManagerEvent.deviceDisconnected | DeviceManagerEvent.deviceRefreshed; device: Device; args: [] } | ||
| | { type: DeviceManagerEvent.deviceNotification; device: Device; args: [notification: DeviceNotification] }; | ||
| | { type: DeviceManagerEvent.deviceConnected | DeviceManagerEvent.deviceDisconnected | DeviceManagerEvent.deviceRefreshed; device: AnyDevice; args: [] } | ||
| | { type: DeviceManagerEvent.deviceNotification; device: AnyDevice; args: [notification: DeviceNotification] }; | ||
|
|
||
| type ScriptRuntimeEvents = { | ||
| [AutomationEventType.consoleLog]: (data: string) => void, | ||
|
|
@@ -223,6 +223,25 @@ export default class ScriptRuntime | |
|
|
||
| public async load(scriptCode: string): Promise<void> | ||
| { | ||
| const writer = fs.createWriteStream(`${this.logPath}/automation.log`); | ||
| writer.on('error', (err) => { | ||
| this.logger.error(`Automation log write error: ${err.message}`); | ||
| }); | ||
|
|
||
| // Open the log file before creating the isolate: if this fails, nothing | ||
| // needs to be torn down yet. | ||
| try { | ||
| await new Promise<void>((resolve, reject) => { | ||
| writer.once('open', () => resolve()); | ||
| writer.once('error', (err) => reject(err)); | ||
| }); | ||
| } catch (e) { | ||
| writer.destroy(); | ||
| throw e; | ||
| } | ||
|
|
||
| this.logWriter = writer; | ||
|
Comment on lines
+226
to
+243
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Clean up partial initialization failures. If isolate/context creation, script compilation, or jail setup fails after Line 243, 🤖 Prompt for AI Agents |
||
|
|
||
| this.isolate = new ivm.Isolate({ memoryLimit: 128 }); | ||
| this.vmContext = await this.isolate.createContext(); | ||
|
|
||
|
|
@@ -299,10 +318,6 @@ export default class ScriptRuntime | |
| this.dispatchRef = await this.vmContext.global.get('__dispatchEvent'); | ||
| this.lifecycleRef = await this.vmContext.global.get('__dispatchLifecycle'); | ||
|
|
||
| this.logWriter = fs.createWriteStream(`${this.logPath}/automation.log`); | ||
| this.logWriter.on('error', (err) => { | ||
| this.logger.error(`Automation log write error: ${err.message}`); | ||
| }); | ||
| this.runningSince = new Date(); | ||
|
|
||
| const lifecycleRef = this.lifecycleRef; | ||
|
|
@@ -441,7 +456,17 @@ export default class ScriptRuntime | |
|
|
||
| public async getLog(maxLines: number): Promise<string> | ||
| { | ||
| return readLastLines.read(`${this.logPath}/automation.log`, maxLines); | ||
| try { | ||
| return await readLastLines.read(`${this.logPath}/automation.log`, maxLines); | ||
| } catch (e: unknown) { | ||
| // No script has run yet (or its log file was not created) - treat as empty log | ||
| // rather than an error condition. | ||
| if (e instanceof Error && e.message.includes('file does not exist')) { | ||
| return ''; | ||
| } | ||
|
|
||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| public isRunning(): boolean | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.