Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/reconcile Endpoint Fails on Second and Subsequent Requests #1388

Open
craigmediaservices opened this issue Jul 8, 2024 · 0 comments
Open

Comments

@craigmediaservices
Copy link

craigmediaservices commented Jul 8, 2024

I have encountered an issue with the /reconcile endpoint in the Dashboard. The endpoint works correctly on the first request but fails to load on the second and subsequent requests.

Steps to Reproduce:

  • Start the Flask application.
  • Access the /reconcile endpoint for the first time.
  • Observe that the endpoint responds correctly.
  • Access the /reconcile endpoint again.
  • Observe that the endpoint fails to respond or hangs indefinitely.
from flask import Flask
import asyncio

app = Flask(__name__)

# Global variable to store the cached result
cached_result = None

@app.route("/reconcile")
def reconcile():
    global cached_result  # Use the global variable

    # Check if we have a cached result
    if cached_result is not None:
        return cached_result

    retval = {"gateway_ok": True}
    try:
        asyncio.set_event_loop(asyncio.new_event_loop())
        retval["optimal"] = reporting_api.table_of_optimal_positions().Body
        retval["ib"] = reporting_api.table_of_ib_positions().Body
        retval["my"] = reporting_api.table_of_my_positions().Body

        # Reindex the position dataframes
        retval["ib"].set_index(
            ["instrument_code", "contract_date"], inplace=True, drop=False
        )
        retval["my"].set_index(
            ["instrument_code", "contract_date"], inplace=True, drop=False
        )
    except:
        # IB gateway connection failed
        retval["gateway_ok"] = False
    if "optimal" in retval["optimal"].columns:
        retval["optimal"]["optimal"] = retval["optimal"]["optimal"].astype(str)
    
    retval = dict_of_df_to_dict(retval, orient="index")
    
    # Cache the result
    cached_result = retval
    
    return retval

Specifically it's hanging on retval["ib"] = reporting_api.table_of_ib_positions().Body

I can't replicate the issue manually in Python, but seems to be specifically with the Flask application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant