Skip to content

Respect follow option on logs endpoint (default false) - #437

Merged
apocas merged 3 commits into
apocas:masterfrom
conradoqg:master
Feb 26, 2018
Merged

Respect follow option on logs endpoint (default false)#437
apocas merged 3 commits into
apocas:masterfrom
conradoqg:master

Conversation

@conradoqg

Copy link
Copy Markdown
Contributor

By default the follow option in the container and service endpoint is false. This fix follows this definition and sets the isStream variable accordingly.

Best

Comment threadlib/task.js Outdated
path: '/tasks/' + this.id + '/logs?',
method: 'GET',
isStream: args.opts.follow || false,
isStream: (typeof(args.opts.follow) != 'undefined') ? args.opts.follow : false,

@SeikhoSeikhoFeb 26, 2018

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How is this an improvement or different from the original? This won't coalesce to false if something false-y like null is provided here.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Agree, changing.

Comment threadlib/service.js Outdated
path: '/services/' + this.id + '/logs?',
method: 'GET',
isStream: true,
isStream: (typeof(args.opts.follow) != 'undefined') ? args.opts.follow : false,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This could also use the original logic from lib/task.js that you replaced.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Agree, changing.

@conradoqg

conradoqg commented Feb 26, 2018

Copy link
Copy Markdown
ContributorAuthor

Hey. Could you review the last commit?

I changed the three endpoints that can have logs: container, service, and task.

Thanks

@Seikho

Copy link
Copy Markdown

Looks good to me!

@apocas

Copy link
Copy Markdown
Owner

Completely agree on following the Docker's default behavior.

Since we are changing dockerode's default behavior, can't publish this in a minor.
Will ping when published.

@apocas
apocas merged commit b021508 into apocas:masterFeb 26, 2018
@jysperm

Copy link
Copy Markdown

I think this PR introduced a breaking change and published as a patch version (2.5.5).

container.log({follow: false}) will callback with a stream before, but now will callback with a string.

@aguegu

Copy link
Copy Markdown

@jysperm , as discussed in #456

@aguegu

Copy link
Copy Markdown

I understand this merge make dockerode behave more native to docker api. But the outcome is not as good as expected.

With the raw binary output from docker api when follow: false, we have to do hijack the stream format, as https://docs.docker.com/engine/api/v1.37/#operation/ContainerAttach

But this header/body section way does not always bring the right output because the exact body length may not always equal to body size specified in the header later 4 bytes, The body may be cut due to actions like stop or restart, etc. Once that happened, it would require a lot of code to seek back the next header.

This can be verified by sample code like

constresult=awaitcontainer.logs({follow: false,stdout: true,stderr: true,});constbuffer=Buffer.from(result);letp=0;logger.debug(buffer.length);while(p<buffer.length){constheader=buffer.slice(p,p+8);constlen=header.readInt32BE(4);// logger.debug({ p, header, len });p+=8;constcontent=buffer.toString('utf8',p,p+len);console.log(content);p+=len;}

On the other hand, by setting isStream to true in the modem.dial function, in the data in the stream.on('data') event handler args, the first 8 bytes would always be the header, even if the body is shorter than then length it should be.

Sign up for freeto 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.

5 participants

@conradoqg@Seikho@apocas@jysperm@aguegu