-
Notifications
You must be signed in to change notification settings - Fork 973
aio-interface: show sub-steps for starting containers #7458
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
Draft
szaimen
wants to merge
4
commits into
main
Choose a base branch
from
enh/6877/show-sub-steps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| class ContainerEventsLogClient { | ||
| overlayElem; | ||
| overlayLogElem; | ||
| pollingFrequencySec = 5; | ||
| pollingIntervalId = null; | ||
| etag = ''; | ||
| debugLogging = false; | ||
|
|
||
| constructor() { | ||
| this.overlayElem = document.getElementById('overlay'); | ||
| this.fetchAndShow(); | ||
| this.pollingIntervalId = setInterval(() => this.fetchAndShow(), this.pollingFrequencySec * 1000); | ||
| } | ||
|
|
||
| #debug(message) { | ||
| if (this.debugLogging) { | ||
| console.debug(message); | ||
| } | ||
| } | ||
|
|
||
| stopPolling() { | ||
| if (this.pollingIntervalId) { | ||
| clearInterval(this.pollingIntervalId); | ||
| } | ||
| } | ||
|
|
||
| async storeEtag(response) { | ||
| const newEtag = response.headers.get('etag'); | ||
| if (newEtag) { | ||
| this.etag = newEtag; | ||
| } | ||
| return response; | ||
| } | ||
|
|
||
| async getTextFromResponse(response) { | ||
| if (response.status >= 200 && response.status < 300) { | ||
| return response.text(); | ||
| } else if (response.status === 304) { | ||
| this.#debug('Cache hit, nothing to do'); | ||
| return Promise.reject(); | ||
| // Cache hit, nothing to do. | ||
| } else { | ||
| console.error(`Got response status ${response.status}, cannot continue`); | ||
| return Promise.reject(); | ||
| } | ||
| } | ||
|
|
||
| showLoggedEventsInOverlay(loggedEvents) { | ||
| this.overlayLogElem ||= document.getElementById('overlay-log'); | ||
| this.overlayLogElem.classList.add('visible'); | ||
| loggedEvents.forEach((loggedEvent) => { | ||
| const elem = this.overlayLogElem.querySelector(`.${loggedEvent.id}`); | ||
| if (elem) { | ||
| elem.lastElementChild.textContent = loggedEvent.message; | ||
| } else { | ||
| const capitalizedContainerName = loggedEvent.id.replace('nextcloud-aio-', '').replace('-', ' ').replace(/(^|\s)[a-z]/gi, (letter) => letter.toUpperCase()); | ||
| const newElem = document.createElement('div'); | ||
| newElem.className = loggedEvent.id; | ||
| const nameElem = document.createElement('span'); | ||
| nameElem.textContent = `${capitalizedContainerName}:`; | ||
| const messageElem = document.createElement('span'); | ||
| messageElem.textContent = loggedEvent.message; | ||
| newElem.append(nameElem, messageElem); | ||
| this.overlayLogElem.append(newElem); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| showLoggedEventsInContainerList(loggedEvents) { | ||
| this.containerElems ||= new Map(Array.from(document.getElementsByClassName('container-elem')).map((elem) => [elem.dataset.containerId, elem.querySelector('.events-log')])); | ||
| loggedEvents.forEach((loggedEvent) => { | ||
| const textElem = this.containerElems.get(loggedEvent.id); | ||
| // Check if the element exists, the event list might contain events for containers that are | ||
| // not contained in our list. | ||
| if (textElem) { | ||
| textElem.textContent = loggedEvent.message; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| async showLoggedEvents(text) { | ||
| const loggedEvents = new Map(); | ||
| this.#debug({ text }); | ||
| // Split text into logged-events and filter out empty lines. | ||
| const lines = text.split('\n').filter((line) => line); | ||
| // Reduce the list of events to the last of each container. | ||
| lines.forEach((line) => { | ||
| const loggedEvent = JSON.parse(line); | ||
| loggedEvents.set(loggedEvent.id, loggedEvent); | ||
| }); | ||
| if (this.overlayElem && this.overlayElem.checkVisibility()) { | ||
| this.showLoggedEventsInOverlay(loggedEvents); | ||
| } else { | ||
| this.showLoggedEventsInContainerList(loggedEvents); | ||
| } | ||
| } | ||
|
|
||
| fetchAndShow(args = { forceReloading: false}) { | ||
| if (args.forceReloading) { | ||
| this.etag = ''; | ||
| } | ||
| this.#debug('Fetching logged events from server'); | ||
| fetch('/api/events/containers', { | ||
| cache: 'no-cache', | ||
| headers: { | ||
| 'If-None-Match': this.etag, | ||
| }, | ||
| }) | ||
| .then((response) => this.storeEtag(response)) | ||
| .then((response) => this.getTextFromResponse(response)) | ||
| .then((text) => this.showLoggedEvents(text)) | ||
| .catch((error) => { | ||
| if (error instanceof Error) { | ||
| throw error; | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', () => { | ||
| window.containerEventsLogClient = new ContainerEventsLogClient(); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| document.addEventListener("DOMContentLoaded", function(event) { | ||
| function displayOverlayLogMessage(message) { | ||
| const overlayLogElement = document.getElementById('overlay-log'); | ||
| if (!overlayLogElement) { | ||
| return; | ||
| } | ||
| overlayLogElement.textContent = message; | ||
| } | ||
|
|
||
| // Attempt to connect to Server-Sent Events at /events/containers and listen for 'container-start' events | ||
| if (typeof EventSource !== 'undefined') { | ||
| try { | ||
| const serverSentEventSource = new EventSource('events/containers'); | ||
| serverSentEventSource.addEventListener('container-start', function(serverSentEvent) { | ||
| try { | ||
| let parsedPayload = JSON.parse(serverSentEvent.data); | ||
| displayOverlayLogMessage(parsedPayload.name || serverSentEvent.data); | ||
| } catch (parseError) { | ||
| displayOverlayLogMessage(serverSentEvent.data); | ||
| } | ||
| }); | ||
| serverSentEventSource.onerror = function() { serverSentEventSource.close(); }; | ||
| } catch (connectionError) { | ||
| /* ignore if Server-Sent Events are not available */ | ||
|
Collaborator
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. Should we log something in case there is another unexpected source of an exception? |
||
| } | ||
| } | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| namespace AIO\Controller; | ||
|
|
||
| use AIO\Container\ContainerState; | ||
| use AIO\ContainerDefinitionFetcher; | ||
| use Psr\Http\Message\ResponseInterface as Response; | ||
| use Psr\Http\Message\ServerRequestInterface as Request; | ||
| use AIO\Data\ConfigurationManager; | ||
| use AIO\Data\DataConst; | ||
| use AIO\Data\ContainerEventsLog; | ||
|
|
||
| readonly class ContainerEventsController { | ||
| public function __construct( | ||
| private ContainerDefinitionFetcher $containerDefinitionFetcher, | ||
| private ConfigurationManager $configurationManager | ||
| ) { | ||
| } | ||
|
|
||
| public function getEventsLog(Request $request, Response $response, array $args) : Response | ||
| { | ||
| $eventsLog = new ContainerEventsLog(); | ||
| $currentMtime = $eventsLog->lastModified(); | ||
| if ($currentMtime === false) { | ||
| error_log("Error: Could not get mtime of file '{$eventsLog->filename}', something is wrong. Responding with status 502."); | ||
| return $response->withStatus(502); | ||
| } | ||
| $currentMtimeHash = md5($currentMtime); | ||
| $knownMtimeHash = $request->getHeaderLine('If-None-Match'); | ||
| if ($knownMtimeHash === $currentMtimeHash) { | ||
| return $response->withStatus(304); | ||
| } | ||
|
|
||
| return $response | ||
| ->withStatus(200) | ||
| ->withHeader('Content-Type', 'application/json; charset=utf-8') | ||
| ->withHeader('Content-Disposition', 'inline') | ||
| ->withHeader('Cache-Control', 'no-cache') | ||
| ->withHeader('Etag', $currentMtimeHash) | ||
| ->withBody(\GuzzleHttp\Psr7\Utils::streamFor(fopen($eventsLog->filename, 'rb'))); | ||
| } | ||
| } |
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.
https://developer.mozilla.org/en-US/docs/Web/API/EventSource says that this has significant limitations when used over HTTP1. Are we concerned about that at all?
Uh oh!
There was an error while loading. Please reload this page.
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.
We disallow multiple tabs so that should not be a problem but we should probably add a check here as well to abort any connection attempt if a second tab was opened like done here:
all-in-one/php/public/second-tab-warning.js
Line 6 in 5752556