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

Ensure npm install runs once at a time #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zakj
Copy link

@zakj zakj commented Dec 7, 2016

On starting up the Lektor server, the server_spawn and before_build_all events both fire, each one spawning npm install. This ensures that only one runs at a time.

@codecov-io
Copy link

codecov-io commented Dec 7, 2016

Current coverage is 97.82% (diff: 100%)

Merging #9 into master will increase coverage by 0.26%

@@             master         #9   diff @@
==========================================
  Files             1          1          
  Lines            41         46     +5   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits             40         45     +5   
  Misses            1          1          
  Partials          0          0          

Powered by Codecov. Last update 677c839...f33edf4

Copy link
Member

@singingwolfboy singingwolfboy left a comment

Choose a reason for hiding this comment

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

Could you add a test for this change?

@zakj zakj force-pushed the single-npm-process branch 2 times, most recently from 74d5d2c to 8a84200 Compare December 7, 2016 05:14
@zakj
Copy link
Author

zakj commented Dec 7, 2016

Ha, you caught me trying to avoid threaded tests. ;) I'm glad you did, though—I think I've arrived at a slightly better implementation while making it testable.

Let me know if there's anything else I can fix up, and thanks so much for this plugin (and your work on Lektor itself)!

reporter.report_generic('Running npm install')
webpack_root = os.path.join(self.env.root_path, 'webpack')
portable_popen(['npm', 'install'], cwd=webpack_root).wait()
if self.npm_lock.acquire(False):
Copy link
Member

Choose a reason for hiding this comment

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

Can you specify blocking=False here? I had to look up the documentation for threading.Lock to understand what this False was for.

Copy link
Author

Choose a reason for hiding this comment

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

Sadly, no:

>>> import threading
>>> threading.Lock().acquire(blocking=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: acquire() takes no keyword arguments

Would an inline comment be useful?

if self.npm_lock.acquire(False):  # non-blocking

webpack_root = os.path.join(self.env.root_path, 'webpack')
portable_popen(['npm', 'install'], cwd=webpack_root).wait()
else:
self.npm_lock.acquire()
Copy link
Member

Choose a reason for hiding this comment

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

Why are you acquiring the lock here, if you're not doing anything with it?

Copy link
Author

Choose a reason for hiding this comment

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

To maintain the behavior that this method does not exit until the npm install process is complete, since callers expect to have a complete set of installed dependencies after calling it. (I.e., if you can acquire the lock, run npm and then release it. If you can't acquire the lock, wait for it to be released before exiting.) Otherwise you might end up attempting to run webpack before it's installed.

On starting up the Lektor server, the server_spawn and before_build_all
events both fire, each one spawning `npm install`. This ensures that
only one runs at a time.
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.

None yet

3 participants