8 months ago
I've deployed a SvelteKit application on Railway using a Dockerfile. Within that project I have a separate service for cron jobs, also deployed with a Dockerfile.
The cron service calls endpoints on my SvelteKit application. This works fine using the public domain of the app service using HTTPS, but when attempting to use private networking it doesn't work.
In my app service, I've configured the server to listen on 0.0.0.0
with Railway's injected port. This builds well and logs Listening on 0.0.0.0:8080
, so I know it's working.
vite.config.ts:
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()],
optimizeDeps: {
include: ['src/lib/components/**/*.svelte', 'src/routes/**/*.svelte']
},
/* Railway requires http:// to be used for private networking, as well as a port to specified. This is injected automatically during run time.
https://docs.railway.app/guides/public-networking#port-variable
https://docs.railway.app/guides/private-networking
*/
server: {
port: Number(process.env.PORT),
host: process.env.HOST || '0.0.0.0'
}
});
0 Replies
8 months ago
95e92fe0-66e9-4eed-86df-2cdc05218e79
8 months ago
Here's my cron-service.ts, which also deploys with no problems.
cron-service.ts:
...
const CRON_SECRET = process.env.CRON_SECRET;
const NODE_ENV = process.env.NODE_ENV || 'development';
const PORT = process.env.PORT;
/* Railway requires http:// to be used for private networking, as well as a port to specified. This is injected automatically during run time.
https://docs.railway.app/guides/public-networking#port-variable
https://docs.railway.app/guides/private-networking
*/
const ORIGIN =
NODE_ENV === 'production'
? `http://${process.env.MAIN_APP_PRIVATE_DOMAIN}:${PORT}`
: `${process.env.MAIN_APP_DEV_ORIGIN}`;
...
Unfortunately, my logs show a connection error Error executing test cron job: request to [http://sveltecareers.railway.internal:8080/api/cron-tester](http://sveltecareers.railway.internal:8080/api/cron-tester) failed, reason: connect ECONNREFUSED fd12:3663:d458::e6:bba7:55ec:8080
. Any idea why this is happening? I believe the ports line up and I'm making a HTTP request.
A collorary question is how to access the ${{sveltecareers.PORT}}
variable as described in https://docs.railway.app/guides/private-networking#use-internal-hostname-and-port. I cannot access the main app service's PORT variable from the cron-schedule service. I've worked around that by referencing process.env.PORT
directly in the cron-service.ts
assuming that the injected PORT would be the same, which it appears to be (8080
).
Thanks! Loving the platform so far.
8 months ago
Private networking in SvelteKit app
8 months ago
the injected port would be the same, but its never good to assume that, you would want to set a PORT service variable on the SvelteKit service so that you can reference it on the cron service.
as for the ECONNREFUSED
error, remove the server
config, and then simply set a HOST
service variable to ::
since the private network is IPv6 only.
8 months ago
Thanks for the quick response @Brody!
When setting the PORT service variable on the SvelteKit service, is there any way to retrieve this as a Railway provided variable? Or is PORT only made available during runtime, but not in the GUI? I wasn't able to select it from the dropdown in the service variables menu. Do I have to set a fixed port, like 8080
now that I know that's the injected one?
I don't understand what you mean with setting a HOST
service. Do you mean to remove the server
property I had defined from the vite.config.ts
altogether?
8 months ago
yeah you are right, its only available during runtime, you can just set a fixed port in the UI though.
8 months ago
Ah sorry I think I'm grokking now. You're suggesting I set a HOST
service variable. On the SvelteKit Railway service or the cron service?
8 months ago
I don't understand what you mean with setting a HOST service. Do you mean to remove the server property I had defined from the vite.config.ts altogether? (edited)
8 months ago
yes, service variable, on the SvelteKit service
8 months ago
and also remove the server property in the config file, its not needed
8 months ago
OK so summarizing:
On the SvelteKit service I remove the service
prop from the vite.config.ts
. I set a fixed PORT
service variable to 8080
instead, and a HOST
service variable to ::
. This allows me to retrieve these on the cron service, where I define the full private origin as: [http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}${{sveltecareers.HOST}}${{sveltecareers.PORT](http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}${{sveltecareers.HOST}}${{sveltecareers.PORT)}}
. Is that right?
8 months ago
Sorry with the prefix of http://
8 months ago
you got most correct.
the url should be made like this [http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}:${{sveltecareers.PORT](http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}:${{sveltecareers.PORT)}}
the HOST
variable is just set so that the server knows to listen on IPv6 is all
8 months ago
Ah I see. But I don't need to specify this in vite for the SvelteKit build, because Railway will inject this during run time?
8 months ago
Do I not use double colons in the private origin? Just single colon?
8 months ago
yeah the SvelteKit server will look for and use a HOST
variable
8 months ago
(First time doing private networking on any hosting service and I'm new to IP standards like IPv6, so bear with me 😅)
8 months ago
not sure what you mean, simply set HOST=::
in the SvelteKit service, no need to overthink this right now
8 months ago
Funny, today I learned that https://
always defaults to port 443
. And that explains why I need to define a port when using http://
.
8 months ago
OK got it, thanks Brody. Will work on implementing this r/n.
8 months ago
set HOST=::
and PORT=8080
on sveltecareers
set MAIN_APP_PRIVATE_DOMAIN="[http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}:${{sveltecareers.PORT](http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}:${{sveltecareers.PORT)}}"
on cron-service
and then simply use MAIN_APP_PRIVATE_DOMAIN
when you call sveltecareers
8 months ago
🫡
8 months ago
This worked, thanks for helping me fix.
It may be worthwhile updating the private networking docs to clarify that while the PORT variable is automatically injected by Railway during runtime, it's not accessible as a reference variable and needs to be set manually.
8 months ago
Unrelated question -- the SvelteKit and the cron scheduler are both in a monorepo. I'm deploying based on Github commits. Is there any way to only deploy one service when a particular file or folder in my repo changes? Right now when I change the cron service, the whole base app re-deploys.
8 months ago
its not specific to private networking, i think its mentioned elsewhere in the docs though.
8 months ago
you want watch paths -
8 months ago
Oooh that's sweet, will try now.
8 months ago
Works like a charm, thanks again!
8 months ago
Tagging this as solved.
8 months ago
no problem!
8 months ago
After implementing your feedback, this branch passed my CI/CD checks and I merged onto my main branch. I then updated the branch to be watched to main in my services.
However since redeploying, they are both showing strange deploy logs. The cron scheduler runs into an import issue:
> sveltecareers-cron-service@1.0.0 start /app
> rm -rf dist && tsc && node dist/cron-service.js
ORIGIN http://sveltecareers.railway.internal:8080
Bree cron scheduler started successfully.
Worker for job "test-logger" online undefined
Worker for job "test-logger" had an error {
err: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/dist/utils' imported from /app/dist/jobs/test-logger.js
at finalizeResolution (node:internal/modules/esm/resolve:265:11)
at moduleResolve (node:internal/modules/esm/resolve:933:10)
at defaultResolve (node:internal/modules/esm/resolve:1169:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:540:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:509:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
at ModuleWrap. (node:internal/modules/esm/module_job:96:40)
at link (node:internal/modules/esm/module_job:95:36) {
code: 'ERR_MODULE_NOT_FOUND',
url: 'file:///app/dist/utils'
}
}
It pertains to this import, which should work fine. I've confirmed the path holds true for the dist
folder which tsconfig.ts
is successfully configured to build into.
Any idea what could be going wrong? The exact same codebase worked when I had it deployed via Github onto Railway in my previous branch.
8 months ago
I'm sorry but we wouldn't be able to provide coding help here
8 months ago
perhaps you have the import named wrong or the file named incorrectly, but beyond this we can't help here, these forums are for platform help
8 months ago
Sure I understand. I'm just trying to ascertain why this deployment would have worked well when on a different branch, but failed now that I merged it. I must have made a mistake somewhere, but struggle to determine what it is.
8 months ago
really sorry but we can't offer support for application level issues
8 months ago
Understood I'll try resolve in the SvelteKit Discord.
8 months ago
Thanks for checking.
8 months ago
sounds good