Skip to content

Implement AsyncProgressWorker - #529

Closed
legendecas wants to merge 2 commits into
nodejs:masterfrom
legendecas:async-progress-work
Closed

Implement AsyncProgressWorker#529
legendecas wants to merge 2 commits into
nodejs:masterfrom
legendecas:async-progress-work

Conversation

@legendecas

@legendecaslegendecas commented Aug 27, 2019

Copy link
Copy Markdown
Member

Related: #473

  • npm test passed
  • test included
  • documents are added or changed

Comment threadnapi.h Outdated
Comment threadtest/asyncprogressworker.cc Outdated
@mhdawson

Copy link
Copy Markdown
Member

@mhdawson

Copy link
Copy Markdown
Member

Seems to be a failure due to unused paramter in OnProgress. Will push a change to address that so can test further.

@mhdawson

Copy link
Copy Markdown
Member

@mhdawson

Copy link
Copy Markdown
Member

@legendecas can you take a look, seems to be failing in CI runs across platforms with:

All tests passed!
terminate called after throwing an instance of 'Napi::Error'
what(): The expression evaluated to a falsy value:
assert.ok(e instanceof Error)
Tests aborted with SIGABRT
npm ERR! Test failed. See above for more details.

@mhdawson

Copy link
Copy Markdown
Member

@legendecas sorry, see you are still working on the tests from your checklist in the original post. Let us know when it's ready to review.

One other thing that you'll need to add is docs in the "Async Operations" section of https://github.com/nodejs/node-addon-api

@legendecas

Copy link
Copy Markdown
MemberAuthor

@legendecas can you take a look, seems to be failing in CI runs across platforms with:

All tests passed!
terminate called after throwing an instance of 'Napi::Error'
what(): The expression evaluated to a falsy value:
assert.ok(e instanceof Error)
Tests aborted with SIGABRT
npm ERR! Test failed. See above for more details.

Yes, it's crashed for case https://github.com/nodejs/node-addon-api/blob/master/test/asyncworker.js. It could be reproduced locally.

@legendecas

Copy link
Copy Markdown
MemberAuthor

One other thing that you'll need to add is docs in the "Async Operations" section of https://github.com/nodejs/node-addon-api

Added to checklist

Comment threadnapi-inl.h Outdated
Comment threadnapi-inl.h Outdated
Comment threadnapi.h Outdated
@legendecas
legendecasforce-pushed the async-progress-work branch 2 times, most recently from f7d9fd4 to a9093d9CompareAugust 28, 2019 09:22
Comment threadtest/asyncprogressworker.js Outdated

@NickNasoNickNaso left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@legendecas I downloaded your work and it seems to work on macOS, but to execute the tests you need to add asyncprogressworker to here https://github.com/legendecas/node-addon-api/blob/async-progress-work/test/index.js#L13 and remove it in case the N-API version is less then 4 like we did here https://github.com/legendecas/node-addon-api/blob/async-progress-work/test/index.js#L67

@legendecas

Copy link
Copy Markdown
MemberAuthor

#532 would blocking this PR from their routine work like CI/tests. (Just a note)

@mhdawson

mhdawson commented Sep 27, 2019

Copy link
Copy Markdown
Member

Was a bit worried that some of the locking features might have requuired later compilers. This CI run seems to confirm it can build/run on Node.js 8.X so that is not an issue: https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/675/

@mhdawson

Copy link
Copy Markdown
Member

@legendecas sorry it's taking so long to get the review done. I'm out of time for today but I'll try to continue reviewing next week.

Comment threadnapi.h Outdated
Comment threadnapi.h
Comment threadnapi-inl.h Outdated
@legendecas
legendecasforce-pushed the async-progress-work branch 3 times, most recently from 521711b to 2540375CompareOctober 6, 2019 14:54
Comment threadnapi-inl.h Outdated

@mhdawsonmhdawson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, @legendecas thanks for all of the work on this. Hope to do a pass of landing PRs in the next few days.

@gabrielschulhofgabrielschulhof left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Partial review. Will continue.

Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated

void OnProgress(const uint32_t* data, size_t /* count */) {
HandleScope scope(Env());
Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), data)});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

data here is a pointer to an integer, not an integer. Also, there is no synchronization between the Execute writing to i and OnProgress reading from i. Perhaps above the code should be changed to

progress.Send(newuint32_t(i), 1);

and here it should be

Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
delete data;

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Yes, it's a pointer to an integer. I'll update the doc.

For the later question, as documented above, progress.Send will copy the data pointed to right on the call to the progress.Send. So it is safe to do so without any synchronizations.

Comment threaddoc/async_progress_worker.md Outdated
Comment threaddoc/async_progress_worker.md Outdated
Comment threadnapi-inl.h
@NickNasoNickNaso mentioned this pull request Oct 22, 2019
15 tasks
@legendecas
legendecasforce-pushed the async-progress-work branch 2 times, most recently from 7e0e636 to b9748f2CompareOctober 23, 2019 12:22
Comment threaddoc/async_progress_worker.md Outdated
@@ -0,0 +1,344 @@
# AsyncProgressWorker

`Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
`Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker`
`Napi::AsyncProgressWorker` is an abstract class which implements `Napi::AsyncWorker`

Comment threaddoc/async_progress_worker.md Outdated

## Methods

[`Napi::AsyncWorker`]() provides detailed descriptions for most methods.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
[`Napi::AsyncWorker`]() provides detailed descriptions for most methods.
[`Napi::AsyncWorker`][] provides detailed descriptions for most methods.

Comment threaddoc/async_progress_worker.md Outdated
explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name, const Napi::Object& resource);
```

- `[in] receiver`: The `this` object passed to the called function.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
-`[in] receiver`: The `this` object passed to the called function.
-`[in] receiver`: The `this` object to pass to the called function.

Comment threaddoc/async_progress_worker.md
Comment threaddoc/async_progress_worker.md Outdated
passed in through its constructor.

During the worker thread execution, the first argument of `Napi::AsyncProgressWorker::Execute`
can be used to report the process of the execution.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
can be used to report the process of the execution.
can be used to report the progress of the execution.

Comment threaddoc/async_progress_worker.md Outdated
callback that the `Napi::AsyncProgressWorker` base class will store persistently. When
the work on the `Napi::AsyncProgressWorker::Execute` method is done the
`Napi::AsyncProgressWorker::OnOk` method is called and the results return back to
JavaScript invoking the stored callback with its associated environment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
JavaScript invoking the stored callback with its associated environment.
JavaScript when the stored callback is invoked with its associated environment.

Comment threaddoc/async_progress_worker.md Outdated
The following code shows an example of how to create and use an `Napi::AsyncProgressWorker`

```cpp
#include<napi.h>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
#include<napi.h>
#include<napi.h>

Comment threaddoc/async_progress_worker.md Outdated
Comment on lines +338 to +342
Using the implementation of a `Napi::AsyncProgressWorker` is straight forward. You only
need to create a new instance and pass to its constructor the callback you want to
execute when your asynchronous task ends and other data you need for your
computation. Once created the only other action you have to do is to call the
`Napi::AsyncProgressWorker::Queue` method that will queue the created worker for execution.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We generally avoid using "you" in our documentation. Let's rephrase this as

The implementation of a `Napi::AsyncProgressWorker` can be used by creating a
new instance and passing to its constructore the callback to execute when the
asynchronous task ends and other data needed for the computation. Once created,
the only other action needed is to call the `Napi::AsyncProgressWorker::Queue`
method that will queue the created worker for execution.

Comment threaddoc/async_progress_worker.md Outdated

The first step to use the `Napi::AsyncProgressWorker` class is to create a new class that
inherits from it and implement the `Napi::AsyncProgressWorker::Execute` abstract method.
Typically input to your worker will be saved within the class' fields generally

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
Typically input to your worker will be saved within the class' fields generally
Typically input to the worker will be saved within the class' fields generally

Comment threaddoc/async_progress_worker.md Outdated
use namespace Napi;

Value Echo(const CallbackInfo& info) {
// You need to validate the arguments here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// You need to validate the arguments here
// We need to validate the arguments here

mhdawson pushed a commit that referenced this pull request Nov 1, 2019
PR-URL: #529Fixes: #473
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
@mhdawson

Copy link
Copy Markdown
Member

Landed as 650562c

@mhdawsonmhdawson closed this Nov 1, 2019
@legendecas
legendecas deleted the async-progress-work branch November 2, 2019 02:02
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
PR-URL: nodejs/node-addon-api#529Fixes: nodejs/node-addon-api#473
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
PR-URL: nodejs/node-addon-api#529Fixes: nodejs/node-addon-api#473
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
PR-URL: nodejs/node-addon-api#529Fixes: nodejs/node-addon-api#473
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
PR-URL: nodejs/node-addon-api#529Fixes: nodejs/node-addon-api#473
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
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.

4 participants

@legendecas@mhdawson@NickNaso@gabrielschulhof