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

Build attempts connection to dependent services #933

Closed
kubejm opened this issue Apr 9, 2021 · 6 comments · Fixed by #2008
Closed

Build attempts connection to dependent services #933

kubejm opened this issue Apr 9, 2021 · 6 comments · Fixed by #2008
Labels
documentation Improvements or additions to documentation

Comments

@kubejm
Copy link

kubejm commented Apr 9, 2021

Describe the bug
I have a SvelteKit application which connects to a dependent service via a socket using createConnection from the net module. During the build process to create production assets via the npm run build script, it appears to try run my modules, which results in attempting to create the socket to the dependent service.

If the service is up, then the socket connection is established and npm run build hangs on the open socket, so it does not finish. However, if the service is down, then the socket connection fails, which results in error outputs of the failed connection during the build, and the build finishes.

I intentionally desired a socket connection on startup of the application, so I did not need to make a new connection upon each request.

Logs
Listed below are outputs during the build process for when the dependent service is up and also for when it is down. To set some context, the name of the relative service is spidey-db and it runs on port 6425.

Output when dependent service is up. "connected to spidey db" is a debug log statement for reference

❯ npm run build

> spidey-db-demo@0.0.1 build
> svelte-kit build

vite v2.1.5 building for production...
✓ 586 modules transformed.
.svelte/output/client/_app/manifest.json                            0.85kb
.svelte/output/client/_app/chunks/index-79739b24.js                 0.48kb / brotli: 0.28kb
.svelte/output/client/_app/assets/start-a8cd1609.css                0.16kb / brotli: 0.10kb
.svelte/output/client/_app/assets/pages/index.svelte-0b0729a2.css   1.95kb / brotli: 0.70kb
.svelte/output/client/_app/chunks/vendor-9102d6d9.js                5.17kb / brotli: 2.01kb
.svelte/output/client/_app/start-b972c043.js                        15.73kb / brotli: 5.36kb
.svelte/output/client/_app/pages/index.svelte-f2643f1e.js           62.10kb / brotli: 17.92kb
vite v2.1.5 building SSR bundle for production...
✓ 38 modules transformed.
.svelte/output/server/app.js   103.22kb

Run npm start to try your app locally.

> Using @sveltejs/adapter-node
  ✔ done
connected to spidey db

Output when dependent service is down.

❯ npm run build

> spidey-db-demo@0.0.1 build
> svelte-kit build

vite v2.1.5 building for production...
✓ 586 modules transformed.
.svelte/output/client/_app/manifest.json                            0.85kb
.svelte/output/client/_app/chunks/index-79739b24.js                 0.48kb / brotli: 0.28kb
.svelte/output/client/_app/assets/start-a8cd1609.css                0.16kb / brotli: 0.10kb
.svelte/output/client/_app/chunks/vendor-9102d6d9.js                5.17kb / brotli: 2.01kb
.svelte/output/client/_app/assets/pages/index.svelte-0b0729a2.css   1.95kb / brotli: 0.70kb
.svelte/output/client/_app/start-b972c043.js                        15.73kb / brotli: 5.36kb
.svelte/output/client/_app/pages/index.svelte-f2643f1e.js           62.10kb / brotli: 17.92kb
vite v2.1.5 building SSR bundle for production...
✓ 38 modules transformed.
.svelte/output/server/app.js   103.22kb

Run npm start to try your app locally.

> Using @sveltejs/adapter-node
  ✔ done
node:events:353
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:6425
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1138:16)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (node:internal/streams/destroy:188:8)
    at emitErrorCloseNT (node:internal/streams/destroy:153:3)
    at processTicksAndRejections (node:internal/process/task_queues:80:21) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6425
}

To Reproduce
This could be reproduced by creating a socket connection within a module, then running build.

This can be replicated with the following folder in the following repository:
https://github.com/kubejm/spidey-db-demo/tree/master/packages/webapp

The socket connection within the repository above can be found here:
https://github.com/kubejm/spidey-db-demo/blob/master/packages/webapp/src/lib/spidey-db/client.ts#L87-L97

The connection is established here:
https://github.com/kubejm/spidey-db-demo/blob/master/packages/webapp/src/lib/spidey-db/index.ts#L6-L9

Expected behavior
Not executing the module within my application code, which attempts to establish a socket connection.

I'm new to Svelte/SvelteKit, so I'm not aware of the build process to produce the production assets. However, ideally it would not need to instantiate/run my assets with the @sveltejs/adapter-node to produce them.

Information about your SvelteKit Installation:

Diagnostics
❯ npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers
Need to install the following packages:
  envinfo
Ok to proceed? (y) y

  System:
    OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (4) x64 Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz
    Memory: 9.36 GB / 15.60 GB
    Container: Yes
    Shell: 3.2.1 - /usr/bin/fish
  Binaries:
    Node: 15.4.0 - ~/.local/share/nvm/v15.4.0/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 7.7.5 - ~/.local/share/nvm/v15.4.0/bin/npm
  Browsers:
    Chromium: 89.0.4389.114
    Firefox: 87.0
  npmPackages:
    @sveltejs/kit: next => 1.0.0-next.71
    svelte: ^3.29.0 => 3.37.0
    vite: ^2.1.0 => 2.1.5

Adapter: @sveltejs/adapter-node

Severity
This is a side project to learn Svelte and SvelteKit, so no urgency on this for me. I am hoping to run this application in Docker, so I'm trying to work towards a production-like state by building the assets and running it within a container.

@benmccann benmccann added pkg:adapter-node adapters - general Support for functionality general to all adapters labels Apr 9, 2021
@benmccann
Copy link
Member

benmccann commented Apr 9, 2021

That's probably because prerendering happens during build:

await utils.prerender({

I can see if you have no prerendered pages how it would be frustrating not to be able to build your site without a connection to prod database though. Perhaps we need a flag to turn off prerendering? Though even that seems like it will trip up a lot of users because it will be trying to connect to your database by default, which may not be desired for many users. I don't see another way of doing this without moving this option outside the realm of file-system routing though (if we had a traditionally configured router we would not need to import the templates to resolve the options). Something similar is happening with the SSR option in #754 (comment)

Other options for you to be able to deal with this:

  • Don't connect to database until you get a request that needs to access it
  • Build on a CI in your production environment that has access to production dependent services

@benmccann benmccann changed the title Build starts adapter, which attempts connection to dependent service Build attempts connection to dependent services Apr 9, 2021
@benmccann benmccann removed the adapters - general Support for functionality general to all adapters label Apr 9, 2021
@benmccann benmccann mentioned this issue Apr 9, 2021
@kubejm
Copy link
Author

kubejm commented Apr 9, 2021

That's probably because prerendering happens during build:

await utils.prerender({

I can see if you have no prerendered pages how it would be frustrating not to be able to build your site without a connection to prod database though. Perhaps we need a flag to turn off prerendering? Though even that seems like it will trip up a lot of users because it will be trying to connect to your database by default, which may not be desired for many users. I don't see another way of doing this without moving this option outside the realm of file-system routing though (if we had a traditionally configured router we would not need to import the templates to resolve the options). Something similar is happening with the SSR option in #754 (comment)

Other options for you to be able to deal with this:

  • Don't connect to database until you get a request that needs to access it
  • Build on a CI in your production environment that has access to production dependent services

Thanks for the reply and suggestions @benmccann

I think it would be ideal to go towards that route to turn off prerendering during the build process to avoid having to introduce build concerns within the application code. However, I think that's not a deal breaker and I think I'll personally lean towards introducing those concerns, rather than connection per request to favor performance in my instance.

I'm new to Svelte and this project, but if you have thoughts on an improvement that could be made to SvelteKit, then I could try to work through a change and take a swing at it.

@n4bb12

This comment has been minimized.

@benmccann
Copy link
Member

@n4bb12 there's a separate issue for that: #978

@Rich-Harris
Copy link
Member

reopening since it was auto-closed by a 'maybe closes' comment, but it wasn't confirmed

@Rich-Harris Rich-Harris reopened this Jul 31, 2021
@kubejm
Copy link
Author

kubejm commented Sep 11, 2021

I apologize for the delay on vetting this.

I have pulled down the latest versions of @sveltejs/kit (1.0.0-next.165) and @sveltejs/adapter-node (1.0.0-next.46), then converted my svelte.config.cjs to svelte.config.js. I am no longer experiencing the issue I initially raised. It builds successfully and runs as expected.

Thank you!

@kubejm kubejm closed this as completed Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants