bigviking
bigviking
RRailway
Created by bigviking on 12/6/2023 in #✋|help
Proper Connecting String Syntax for Private Services
I must be going insane. Whats the right connection string format to connect to a private service in a Railway network? I've tried:
service
service.railway.internal
service.railway.internal/graphql
service.railway.internal:port/graphql
http://service.railway.internal/graphql
https://service.railway.internal/graphql
http://service.railway.internal:port/graphql
https://service.railway.internal:port/graphql

service
service.railway.internal
service.railway.internal/graphql
service.railway.internal:port/graphql
http://service.railway.internal/graphql
https://service.railway.internal/graphql
http://service.railway.internal:port/graphql
https://service.railway.internal:port/graphql

I am attempting to call a graphql endpoint in one Node service from another using fetch.
const fetchGQL = async (query, variables) => {
try {
const res = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: query,
variables: variables,
}),
});
const obj = await res.json();
return obj.data;
} catch (err) {
logger.error({ error: err }, 'GraphQL fetch failed.');
}
};

const res = fetchGQL(query, vars);

res = undefined

API_URL = each of the above
const fetchGQL = async (query, variables) => {
try {
const res = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: query,
variables: variables,
}),
});
const obj = await res.json();
return obj.data;
} catch (err) {
logger.error({ error: err }, 'GraphQL fetch failed.');
}
};

const res = fetchGQL(query, vars);

res = undefined

API_URL = each of the above
The service I am attempting to connect to is running on port 7653. I know this because I have it log process.env.PORT on start.
19 replies
RRailway
Created by bigviking on 12/1/2023 in #✋|help
Best Way to Run a Weekly Cron Job
Use case: I need to poll an external API during several windows throughout the week. The times change each week and no two will be the same (or very rarely). I know what the upcoming week's times will be by polling an endpoint at a particular time in the current week. This cron job of mine requests data from an external API to compare to data in my networked database and if it detects changes it does things and then updates the database. Current state: Currently, I have the cron scripts start up and queue in my Express app after it deploys but they do not run on schedule in Railway, nor at all. The jobs run at the appropriate time locally and works with my local services without error. Question: Curious to know the best path ahead or perhaps what I'm doing incorrectly when starting the jobs on Railway. I'd prefer not to but if I have to create a separate service for the cron and use Railway's cron settings in the service settings I'll go down that path. Thanks!
10 replies
RRailway
Created by bigviking on 11/29/2023 in #✋|help
Bind Express App to IPv6?
I deployed my Express API to Railway and its talking to Redis and Postgres nicely. My client is built in Next 13 with the App Router and is having trouble discovering
blackjack.railway.internal:8080
blackjack.railway.internal:8080
I think this has to do with my binding the Express app to an IPv6 port or at least making it accessible through IPv6 however after a few hours of searching I'm unable to find a clear way of doing this? client <> server is communicating through ApolloServer.
const logger = getLogger('express');

logger.warn(`Launching in ${process.env.NODE_ENV.toUpperCase()}`);

const schema = makeExecutableSchema({
typeDefs,
resolvers: {
Query,
Mutation,
Subscription,
},
});

const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 20,
});

// Create an Express app and HTTP server; attach both the WebSocket
// server and the ApolloServer to this HTTP server.
const app = express();
const httpServer = createServer(app);

// Create WebSocket server using the HTTP server we just set up.
const wsServer = new WebSocketServer({
server: httpServer,
path: '/graphql',
});

// Save returned server's info so we can shutdown this server later
const wsServerCleanup = useServer(
{
schema,
context: async (ctx, msg, args) => {
return { pubsub };
},
},
wsServer
);

// Create ApolloServer.
const server = new ApolloServer({
schema,
// just added could cause errors
csrfPrevention: true,
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),

// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
async drainServer() {
await wsServerCleanup.dispose();
},
};
},
},
],
});

const main = async () => {
// Apollo/GraphQL server start.
await server.start();

if (server) {
logger.info(
`Apollo/GraphQL API is now live on endpoint: ${NODE_PORT}/graphql`
);
} else {
logger.fatal(`Could not start Apollo/GraphQL API`);
process.exit(1);
}

// Middleware for the express application.
app.use(
'/graphql',
cors(),
helmet(),
limiter,
bodyParser.json(),
expressMiddleware(server, {
context: async ({ req }) => {
return { prisma, pubsub, req };
},
})
);
app.get('/health', (req, res) => {
res.status(200).send('Okay!');
});

const hostname = '::';

// http server start
httpServer.listen(NODE_PORT, hostname, () => {
logger.info(
`Apollo/GraphQL websocket service is live on endpoint: ${NODE_PORT}/graphql`
);
});

setTimeout(() => {
pingRedis();
}, 1000);
};

main();
const logger = getLogger('express');

logger.warn(`Launching in ${process.env.NODE_ENV.toUpperCase()}`);

const schema = makeExecutableSchema({
typeDefs,
resolvers: {
Query,
Mutation,
Subscription,
},
});

const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 20,
});

// Create an Express app and HTTP server; attach both the WebSocket
// server and the ApolloServer to this HTTP server.
const app = express();
const httpServer = createServer(app);

// Create WebSocket server using the HTTP server we just set up.
const wsServer = new WebSocketServer({
server: httpServer,
path: '/graphql',
});

// Save returned server's info so we can shutdown this server later
const wsServerCleanup = useServer(
{
schema,
context: async (ctx, msg, args) => {
return { pubsub };
},
},
wsServer
);

// Create ApolloServer.
const server = new ApolloServer({
schema,
// just added could cause errors
csrfPrevention: true,
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),

// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
async drainServer() {
await wsServerCleanup.dispose();
},
};
},
},
],
});

const main = async () => {
// Apollo/GraphQL server start.
await server.start();

if (server) {
logger.info(
`Apollo/GraphQL API is now live on endpoint: ${NODE_PORT}/graphql`
);
} else {
logger.fatal(`Could not start Apollo/GraphQL API`);
process.exit(1);
}

// Middleware for the express application.
app.use(
'/graphql',
cors(),
helmet(),
limiter,
bodyParser.json(),
expressMiddleware(server, {
context: async ({ req }) => {
return { prisma, pubsub, req };
},
})
);
app.get('/health', (req, res) => {
res.status(200).send('Okay!');
});

const hostname = '::';

// http server start
httpServer.listen(NODE_PORT, hostname, () => {
logger.info(
`Apollo/GraphQL websocket service is live on endpoint: ${NODE_PORT}/graphql`
);
});

setTimeout(() => {
pingRedis();
}, 1000);
};

main();
This is my app.js in my server root. Thanks for any insights.
45 replies
RRailway
Created by bigviking on 11/28/2023 in #✋|help
Connecting to Private Services Node API <> Redis
My node js sever is having troubling finding the redis/redis-stack-server image in the network. I'm using the traditional 6379 port and the redis-stack-server.railway.internal host name but my node DNS keeps throwing [ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND redis-stack-server.railway.internal at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) When I spin up my node server. This works fine on local and together in a docker compose file with the same redis image locally. What am I doing wrong to connect to the redis-stack-server?
56 replies