R
Railway6mo ago
Jaymart

Auto increment variable?

is it possible to set variables to auto increment based on the replicas spawned? i.e, I have a variable cluster set to 1 but if another replica is added that increases to 2
Solution:
each replica would get a unique RAILWAY_REPLICA_ID environment variable available to it, its a UUID not an Int but it sounds like it should work for your usecase
Jump to solution
10 Replies
Percy
Percy6mo ago
Project ID: N/A
Jaymart
JaymartOP6mo ago
N/A
Brody
Brody6mo ago
there isnt a way to do that natively through the dashboard, but would you mind telling us your usecase?
Jaymart
JaymartOP6mo ago
it's a discord bot, and I was kinda hoping I can set this up natively without explicitly doing so myself. I'm spawning multiple instances of the bot and I use the cluster variable to identify them accordingly for logs. but then again, does railway already do this in a way when u spawn replicas? i.e, having an identifier for them that I can use within my code instead?
Solution
Brody
Brody6mo ago
each replica would get a unique RAILWAY_REPLICA_ID environment variable available to it, its a UUID not an Int but it sounds like it should work for your usecase
Brody
Brody6mo ago
railway also already adds on the replica id to the logs
No description
Jaymart
JaymartOP6mo ago
and I can just access just access the .env from within the code right?
Brody
Brody6mo ago
yep it's just a regular environment variable
Jaymart
JaymartOP6mo ago
gotcha, thanks! another question for you, I have some logic that more or less sets shards to a specific cluster. got any idea on how using the replica id I could do such thing as the replica IDs aren't exaclty single digits or incremental.
cluster_kwargs = {}
if os.environ.get("IS_PRODUCTION"):
total_shards = 36
cluster_id = int(os.environ["CLUSTER"])
offset = cluster_id - 1 # As we start at 1
number_of_shards_per_cluster = 3
# Calculate the shard id's this cluster should handle
# For example on cluster 1 this would be equal to
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
shard_ids = [
i
for i in range(
offset * number_of_shards_per_cluster,
(offset * number_of_shards_per_cluster) + number_of_shards_per_cluster,
)
if i < total_shards
]
cluster_kwargs = {
"shard_ids": shard_ids,
"shard_count": total_shards,
}
cluster_kwargs = {}
if os.environ.get("IS_PRODUCTION"):
total_shards = 36
cluster_id = int(os.environ["CLUSTER"])
offset = cluster_id - 1 # As we start at 1
number_of_shards_per_cluster = 3
# Calculate the shard id's this cluster should handle
# For example on cluster 1 this would be equal to
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
shard_ids = [
i
for i in range(
offset * number_of_shards_per_cluster,
(offset * number_of_shards_per_cluster) + number_of_shards_per_cluster,
)
if i < total_shards
]
cluster_kwargs = {
"shard_ids": shard_ids,
"shard_count": total_shards,
}
nvm, got it. again, thank for the help!
Brody
Brody6mo ago
share your solution!
Want results from more Discord servers?
Add your server