Skip to content

Commit

Permalink
fix scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
jueri committed Nov 22, 2024
1 parent a7156ee commit e1d0c7d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 51 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
"gesis_rec_pyterrier": {"type": "recommender", "base": true},
"gesis_rec_pyserini": {"type": "recommender"},
"gesis_rank_pyserini_base": {"type": "ranker", "base": true},
"gesis_rank_pyserini": {"type": "recommender"},
"gesis_rank_pyserini": {"type": "recommender"}
}
# Stella Server
STELLA_SERVER_ADDRESS: http://host.docker.internal:8000
Expand Down
50 changes: 25 additions & 25 deletions web/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def create_app(config_name=None):
:param config_object: The configuration object or dictionary to use.
"""
config_name_environment = os.getenv("FLASK_CONFIG") or "default"
print("Create app from:", __name__)
app = Flask(__name__.split(".")[0])

# Load the default configuration
Expand All @@ -38,25 +37,35 @@ def create_app(config_name=None):

def configure_logger(app):
"""Configure loggers."""
log_format = "%(asctime)s %(levelname)s %(filename)s %(funcName)s - %(message)s"
if app.config["DEBUG"]:
log_format = "[%(asctime)s] %(levelname)s in %(module)s: %(message)s"

logging.basicConfig(level=logging.DEBUG, format=log_format)
logging.basicConfig(level=logging.DEBUG, format=log_format)

# StreamHandler for logging to console
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(logging.Formatter(log_format))
if not any(isinstance(h, logging.StreamHandler) for h in app.logger.handlers):
app.logger.addHandler(stream_handler)
# StreamHandler for logging to console
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(logging.Formatter(log_format))

# FileHandler for logging to a file
# file_handler = logging.FileHandler(
# "data/log/stella-app.log"
# ) # Define your log file name
# file_handler.setFormatter(logging.Formatter(log_format))
# if not any(isinstance(h, logging.FileHandler) for h in app.logger.handlers):
# app.logger.addHandler(file_handler)
if not any(isinstance(h, logging.StreamHandler) for h in app.logger.handlers):
app.logger.addHandler(stream_handler)

app.logger.info("Logging setup complete.")
# FileHandler for logging to a file
# file_handler = logging.FileHandler(
# "data/log/stella-app.log"
# ) # Define your log file name
# file_handler.setFormatter(logging.Formatter(log_format))
# if not any(isinstance(h, logging.FileHandler) for h in app.logger.handlers):
# app.logger.addHandler(file_handler)

app.logger.info("Logging setup for debug complete.")

else:
del app.logger.handlers[:]
gunicorn_logger = logging.getLogger("gunicorn.error")
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)

app.logger.info("Logging setup complete.")


def register_extensions(app):
Expand All @@ -65,15 +74,6 @@ def register_extensions(app):
migrate.init_app(app, db)
bootstrap.init_app(app)
cache.init_app(app)

'''
if scheduler.state == 0:
print("Scheduler init app")
scheduler.init_app(app)
logging.getLogger("apscheduler").setLevel(logging.INFO)
print("Scheduler start")
scheduler.start()
'''
return app


Expand Down
19 changes: 3 additions & 16 deletions web/app/services/cron_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,33 +252,20 @@ def post_sessions(sessions_exited):
delete_exited_session(session)


@scheduler.task(
"interval",
id="job_sync",
seconds=10,
max_instances=1,
)
def check_db_sessions():
print("Scheduler enters task.")
with scheduler.app.app_context():
current_app.logger.info("Scheduler enters task.")
scheduler.app.logger.info("Scheduler enters task.")

sessions_not_exited = Session.query.filter_by(exit=False, sent=False).all()
current_app.logger.info(time.strftime("%A, %d. %B %Y %I:%M:%S %p"))
current_app.logger.info(

scheduler.app.logger.info(
"There is/are " + str(len(sessions_not_exited)) + " running session(s)."
)
if current_app.config["DEBUG"]:
print(
"There is/are " + str(len(sessions_not_exited)) + " running session(s)."
)

# set expired sessions to 'exit'
update_expired_sessions(sessions_not_exited)

sessions_exited = Session.query.filter_by(exit=True, sent=False).all()
if current_app.config["DEBUG"]:
print("There is/are " + str(len(sessions_exited)) + " exited session(s).")

if (
current_app.config["STELLA_SERVER_TOKEN"] is None
Expand Down
14 changes: 8 additions & 6 deletions web/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ class Config:
BULK_INDEX = True if os.environ.get("BULK_INDEX") == "True" else False
SESSION_EXPIRATION = os.environ.get("SESSION_EXPIRATION") or 6
SESSION_EXPIRATION = int(SESSION_EXPIRATION)
INTERVAL_DB_CHECK = os.environ.get("INTERVAL_DB_CHECK") or 3 # seconds
INTERVAL_DB_CHECK = int(INTERVAL_DB_CHECK)

SENDFEEDBACK = False if os.environ.get("SENDFEEDBACK") == "False" else True # Opt out
INTERVAL_DB_CHECK = int(os.environ.get("INTERVAL_DB_CHECK") or 3) # seconds

SENDFEEDBACK = (
False if os.environ.get("SENDFEEDBACK") == "False" else True
) # Opt out
DELETE_SENT_SESSION = (
True if os.environ.get("DELETE_SENT_SESSION") == "True" else False
)
Expand Down Expand Up @@ -141,12 +142,13 @@ class PostgresConfig(Config):
user=POSTGRES_USER, pw=POSTGRES_PW, url=POSTGRES_URL, db=POSTGRES_DB
)

SCHEDULER_API_ENABLED = True
JOBS = [
{
"id": "update_server",
"func": 'app.services.cron_service:check_db_sessions',
"func": "app.services.cron_service:check_db_sessions",
"trigger": "interval",
"seconds": os.environ.get("INTERVAL_DB_CHECK") or 3,
"seconds": Config.INTERVAL_DB_CHECK,
}
]

Expand Down
5 changes: 2 additions & 3 deletions web/gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@


def on_starting(server):
print("Gunicorn master process is starting...")


def when_ready(server):
from app.app import create_app, scheduler

app = create_app() # Create an instance of the app
with app.app_context():
if app.config["SENDFEEDBACK"]:
print("Initializing and starting scheduler inside Gunicorn master process")
scheduler.init_app(app) # Initialize the scheduler
scheduler.start() # Start the scheduler

print("Scheduler started in Gunicorn master process")

0 comments on commit e1d0c7d

Please sign in to comment.