Skip to content
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

Cometd.pm: enable basic CLI functionality #1146

Merged
merged 1 commit into from
Aug 17, 2024

Conversation

sodface
Copy link

@sodface sodface commented Aug 15, 2024

Leverage existing code to enable basic CLI functionality. This allows /slim/request over CLI port 9090 to return JSON formatted responses.

See also: https://forums.slimdevices.com/forum/developer-forums/developers/1721698-comet-json-over-cli-9090

Copy link
Member

@michaelherger michaelherger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has the potential to break LMS for probably >99% of all users - for a quite esoteric use case. Please provide some more information how this works. I don't see where JSON would even be processed here etc.

Comment on lines +643 to +651
if ($httpResponse) {
# Add any additional pending events
push @{$out}, ( $manager->get_pending_events( $httpClient->clid ) );

# Add special first event for /meta/(re)connect if set
# Note: calling first_event will remove the event from httpClient
if ( my $first = $httpClient->first_event ) {
unshift @{$out}, $first;
}
# Add special first event for /meta/(re)connect if set
# Note: calling first_event will remove the event from httpClient
if ( my $first = $httpClient->first_event ) {
unshift @{$out}, $first;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please describe what you're doing here, why you're doing it, and why it's working without breaking things.

Copy link
Author

@sodface sodface Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I've been able to determine, $httpResponse will always eval to true here for HTTP clients and always to false for CLI clients. The original code that was outside the conditional would fail for CLI clients and result in a response never being returned to the CLI plugin (and thus the client).

This change moves the existing HTTP client specific code under the conditional for $httpResponse so it's not executed for CLI clients.

@sodface
Copy link
Author

sodface commented Aug 16, 2024

This change has the potential to break LMS for probably >99% of all users - for a quite esoteric use case. Please provide some more information how this works. I don't see where JSON would even be processed here etc.

In currently shipping code, the CLI plugin will send any commands beginning with [ to Cometd.pm for processing. Cometd.pm handling of CLI clients is broken and responses are never returned to the CLI plugin.

The basic logic for this PR is to first determine whether the client being handled in sub handler is a CLI client or an HTTP client. The variable $isCLI is set based on whether $conn->[HTTP_RESPONSE] is defined or not. It's undefined for CLI clients and (as far as I can determine) always defined for a HTTP client.

Once $isCLI is set, then if true, skip HTTP specific code through until the response is returned to the CLI plugin for delivery to the client.

I don't see where JSON would even be processed here etc.

Comet/JSON is formatted and returned by existing code outside of this PR. The issue is that this code is never reached. This PR (or something like it) re-enables it.

$out = eval { to_json($out) };
if ( $@ ) {
$out = to_json( [ { successful => JSON::XS::false, error => "$@" } ] );
}

@@ -658,7 +660,7 @@ sub sendResponse {
else {
# For CLI, don't send anything if there are no events
if ( scalar @{$out} ) {
sendCLIResponse( $httpClient, $httpResponse, $out );
sendCLIResponse( $httpClient, $out );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this change? I see that sendCLIRequest() would only accept two parameters. So this change makes sense - except that you're now passing different data to it, not less. How would that now work if it wasn't broken before?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I believe you gave the answer already: that code was never reached. So essentially this should have been supported from the early days. But nobody used it or understood it, therefore nobody complained about it being broken?...

Could you please add a few lines to the CLI documentation, too? Explaining how to use the CLI in JSON mode:

https://github.com/LMS-Community/lms-community.github.io/blob/main/docs/reference/cli/introduction.md
https://github.com/LMS-Community/lms-community.github.io/blob/main/docs/reference/cli/using-the-cli.md

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review and patience on this Michael!

I meant to note that the $httpClient variable is unfortunately named here for CLI requests but I left it as-is to minimize the changes.

I understand your concern on breaking existing HTTP functionality. Looking at my sodface.com web server logs, I have 6 or 7 people using my 8.5.3 LMS package on Alpine. I could push this change to my package first and post in the Alpine forum thread about it, that way there's at least a little more testing done? Or if you consider 9.x testing already then maybe that's not necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, after better understanding the change I'd be willing to merge to LMS9. As it's in such a central piece of LMS, any issue should show up quickly, I think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to clarify this PR only addresses:
/meta/handshake to get a clientID assigned, and
/slim/request to do one off commands/queries with json responses

Some of the other Cometd functions are probably still broken for CLI clients, like subscribe and unsubscribe. I was trying to keep this simple and restore basic /slim/request function. Is that ok for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this PR is about getting JSON structured data over CLI, not about implementing all cometd features in CLI. cometd is a transfer protocol, like the telnet style CLI's connection is. So we're good. You might be the only one to use the CLI this way anyway. Because: why use it when you can use JSON/RPC? 😝

@michaelherger
Copy link
Member

Oh, one more ask: please add a line to the changelog, referring this PR (and thanking yourself 😂). Thanks!

@sodface
Copy link
Author

sodface commented Aug 16, 2024

Oh, one more ask: please add a line to the changelog, referring this PR (and thanking yourself 😂). Thanks!

Updated changelog but not in a separate commit, hope that's ok.

Copy link
Member

@michaelherger michaelherger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get this in. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants