-
Notifications
You must be signed in to change notification settings - Fork 130
refactor: add iOS runner lifecycle protocol #658
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
6 commits
Select commit
Hold shift + click to select a range
eddeb43
refactor: add ios runner lifecycle protocol
thymikee b0328ec
fix: avoid journaling ios runner status probes
thymikee 38dc7a2
fix: cap ios runner journal responses
thymikee fb6b06d
perf: avoid snapshot journal serialization
thymikee 0469f3c
perf: skip ids for ios runner status probes
thymikee da2c3e1
refactor: keep ios runner status off main thread
thymikee 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
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
140 changes: 140 additions & 0 deletions
140
ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandJournal.swift
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,140 @@ | ||
| import Foundation | ||
|
|
||
| enum RunnerCommandLifecycleState: String { | ||
| case notAccepted | ||
| case accepted | ||
| case started | ||
| case completed | ||
| case failed | ||
| } | ||
|
|
||
| struct RunnerCommandJournalEntry { | ||
| let commandId: String | ||
| let command: String | ||
| var state: RunnerCommandLifecycleState | ||
| var responseOk: Bool? | ||
| var responseJson: String? | ||
| var error: ErrorPayload? | ||
| } | ||
|
|
||
| final class RunnerCommandJournal { | ||
| private let lock = NSLock() | ||
| private let maxEntries = 64 | ||
| private let maxResponseJsonBytes = 16 * 1024 | ||
| private var entries: [String: RunnerCommandJournalEntry] = [:] | ||
| private var order: [String] = [] | ||
|
|
||
| func accept(command: Command) { | ||
| guard let commandId = normalizedCommandId(command.commandId) else { return } | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| entries[commandId] = RunnerCommandJournalEntry( | ||
| commandId: commandId, | ||
| command: command.command.rawValue, | ||
| state: .accepted, | ||
| responseOk: nil, | ||
| responseJson: nil, | ||
| error: nil | ||
| ) | ||
| order.removeAll { $0 == commandId } | ||
| order.append(commandId) | ||
| pruneIfNeeded() | ||
| } | ||
|
|
||
| func start(command: Command) { | ||
| update(command: command, state: .started, responseOk: nil, responseJson: nil, error: nil) | ||
| } | ||
|
|
||
| func finish(command: Command, response: Response) { | ||
| update( | ||
| command: command, | ||
| state: response.ok ? .completed : .failed, | ||
| responseOk: response.ok, | ||
| responseJson: encodeResponseJson(response), | ||
| error: response.error | ||
| ) | ||
| } | ||
|
|
||
| func fail(command: Command, error: Error) { | ||
| update( | ||
| command: command, | ||
| state: .failed, | ||
| responseOk: nil, | ||
| responseJson: nil, | ||
| error: ErrorPayload(message: error.localizedDescription) | ||
| ) | ||
| } | ||
|
|
||
| func status(commandId: String) -> DataPayload { | ||
| guard let normalized = normalizedCommandId(commandId) else { | ||
| return DataPayload(lifecycleState: RunnerCommandLifecycleState.notAccepted.rawValue) | ||
| } | ||
| lock.lock() | ||
| let entry = entries[normalized] | ||
| lock.unlock() | ||
| guard let entry else { | ||
| return DataPayload( | ||
| commandId: normalized, | ||
| lifecycleState: RunnerCommandLifecycleState.notAccepted.rawValue | ||
| ) | ||
| } | ||
| return DataPayload( | ||
| commandId: entry.commandId, | ||
| lifecycleState: entry.state.rawValue, | ||
| lifecycleCommand: entry.command, | ||
| lifecycleResponseOk: entry.responseOk, | ||
| lifecycleResponseJson: entry.responseJson, | ||
| lifecycleErrorCode: entry.error?.code, | ||
| lifecycleErrorMessage: entry.error?.message, | ||
| lifecycleErrorHint: entry.error?.hint | ||
| ) | ||
| } | ||
|
|
||
| private func update( | ||
| command: Command, | ||
| state: RunnerCommandLifecycleState, | ||
| responseOk: Bool?, | ||
| responseJson: String?, | ||
| error: ErrorPayload? | ||
| ) { | ||
| guard let commandId = normalizedCommandId(command.commandId) else { return } | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| var entry = entries[commandId] ?? RunnerCommandJournalEntry( | ||
| commandId: commandId, | ||
| command: command.command.rawValue, | ||
| state: .accepted, | ||
| responseOk: nil, | ||
| responseJson: nil, | ||
| error: nil | ||
| ) | ||
| entry.state = state | ||
| entry.responseOk = responseOk | ||
| entry.responseJson = responseJson | ||
| entry.error = error | ||
| entries[commandId] = entry | ||
| order.removeAll { $0 == commandId } | ||
| order.append(commandId) | ||
| pruneIfNeeded() | ||
| } | ||
|
|
||
| private func pruneIfNeeded() { | ||
| while order.count > maxEntries { | ||
| let removed = order.removeFirst() | ||
| entries.removeValue(forKey: removed) | ||
| } | ||
| } | ||
|
|
||
| private func normalizedCommandId(_ value: String?) -> String? { | ||
| guard let value else { return nil } | ||
| let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| return trimmed.isEmpty ? nil : trimmed | ||
| } | ||
|
|
||
| private func encodeResponseJson(_ response: Response) -> String? { | ||
| guard response.data?.nodes == nil else { return nil } | ||
| guard let data = try? JSONEncoder().encode(response) else { return nil } | ||
| guard data.count <= maxResponseJsonBytes else { return nil } | ||
| return String(data: data, encoding: .utf8) | ||
| } | ||
| } |
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.
Because every request is journaled before dispatch,
statusprobes are stored and pruned like normal commands. When the journal is already at its 64-entry limit, a status request appends its own generatedcommandIdandpruneIfNeeded()removes the oldest entry beforecommandJournal.status(statusCommandId)runs, so querying that oldest retained command returnsnotAcceptedsolely because of the probe. Avoid journalingstatuscommands or defer pruning until after the lookup so lifecycle recovery does not consume its own history slot.Useful? React with 👍 / 👎.