Auto increment variable?

jaymart95
PRO

a year ago

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

0 Replies

jaymart95
PRO

a year ago

N/A


a year ago

there isnt a way to do that natively through the dashboard, but would you mind telling us your usecase?


jaymart95
PRO

a year 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.


jaymart95
PRO

a year ago

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?


a year 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


a year ago

railway also already adds on the replica id to the logs

1247597026917749000


jaymart95
PRO

a year ago

and I can just access just access the .env from within the code right?


a year ago

yep it's just a regular environment variable


jaymart95
PRO

a year ago

gotcha, thanks!


jaymart95
PRO

a year ago

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,
    }

jaymart95
PRO

a year ago

nvm, got it. again, thank for the help!


a year ago

we'll do share your solution!


Auto increment variable? - Railway Help Station