Ticker Connection

rupanshu
I have a microservice that receives the access_token and starts a ticker connection to receive ticks for 10 companies.

i am facing an issue.
when i start the service and give a fresh access token, the ticker connection starts.
next day when i provide a new access token to the already started microservice and recreate the KiteTicker object with new access_token the object is created but the ticker doesnt get connected.

Am i missing something ?
  • rupanshu
    def on_ticks(ws, tick): # noqa
    x = threading.Thread(target=processTicks, args=(tick,))
    x.start()


    def on_connect(ws, response):
    instrument_tokens = instrument_token_list()
    logger.info(
    "Connection response: {}. Receiving ticks for: {}".format(
    response, instrument_tokens
    )
    )
    ws.subscribe(instrument_tokens)
    ws.set_mode(ws.MODE_FULL, instrument_tokens)


    def on_order_update(ws, data):
    logger.info("Order update : {}".format(data))


    ############################################################


    def instrument_token_list():
    ###################
    return instruments


    def KiteTickerSetup(accessToken):
    logger.info("Setting up Kite Ticker.")
    global access_token
    global ticker
    global api_key, access_token

    stop_ticker_thread()

    access_token = accessToken

    # Initialise
    ticker = None
    ticker = KiteTicker(api_key, access_token)

    logger.info("Assigning the callbacks.")
    # Assign the callbacks.
    ticker.on_ticks = on_ticks # type: ignore
    ticker.on_connect = on_connect # type: ignore
    ticker.on_order_update = on_order_update # type: ignore

    ticker.connect()
    logger.info("Connection Started.")


    def start_ticker_thread(accessToken):
    global tickerThread

    if accessToken == None or accessToken == "":
    logger.info("Empty access token.")
    return
    tickerThread = threading.Thread(target=KiteTickerSetup, args=(accessToken,))
    tickerThread.start()


    def stop_ticker_thread():
    logger.info("Trying to stop existing thread.")
    try:
    tickerThread.stop() # type: ignore
    except:
    logger.error("No ticker thread exists.")
  • rupanshu
    i used Threaded=true in the ticker library.
    but still the same behaviour.

    before starting the new connection i am invalidating the previous accessToken also.
  • tahseen
    @rupanshu this is a badly written code. Before writing your code, you should make a sequence diagram so that you know what you are doing.

    You should have
    1. Token generation program that starts everyday at 8:55am and stores token in an inmemory store like Redis

    2. Ticker application that started everyday at 9am, fetches token from Redis and the start working. And exits at 3:30pm. Also, it should not call thread inside on_tick, instead submit to a message broker queue

    3. Your computing micro service service should read from the queue to do the task

    You can segregate these as 3 different services
  • rupanshu
    1 token generation program is already in place which generates it and pushes it to REDIS PUBSUB and other services subscribe to it.
    2 "submitting a message to the broker queue" is done by the new thread that is executed. it is a processing thread. it filters the undesired fields and then pushes to the REDIS db.
    3 the Computing microservice is already reading from the REDIS db.

    these are already different microservices and irrelevant to question asked.

    i want to know if i push a new access_token in the pubsub while the service is started new, the ticker gets connected.
    if i send a new access_token on top of another, then it doesnt get connected.

    how can this be the case ?
Sign In or Register to comment.