def synchronize_unsubscribed_users() -> None: print( "\nPlease execute this script in the same directory as unsubscribed_users.txt" ) print( "To check current path:\nfrom pathlib import Path\nPath().resolve()\n") unsubscribed_users = _get_emails() total_users = len(unsubscribed_users) with app.app_context(): print("Fetching users to update...") users_to_update = User.query.filter(User.email.in_(unsubscribed_users)) users_found_in_db = users_to_update.count() print("Found %s users" % users_found_in_db) print("Updating users:") for index, user in enumerate(users_to_update): user.notificationSubscriptions = ({ **user.notificationSubscriptions, "marketing_email": False } if user.notificationSubscriptions is not None else { "marketing_email": False }) print("%s/%s" % (index + 1, users_found_in_db)) repository.save(*users_to_update) print("Updated %s users out of %s" % (users_found_in_db, total_users)) sendinblue_users_data = format_sendinblue_users(users_to_update.all()) import_contacts_in_sendinblue(sendinblue_users_data, email_blacklist=True)
def wrapper(*args, **kwargs): # The `flask clock` command sets up an application context, # but it gets lost when apscheduler starts a job in a new # thread. So here we must set an application context again. from pcapi.flask_app import app with app.app_context(): return func(*args, **kwargs)
def log_database_connection_status() -> None: with app.app_context(): if check_database_connection(): logger.info("Worker: database connection OK") else: logger.critical("Worker: database connection KO") db.session.remove() db.session.close() db.engine.dispose()
def main(): parser = get_parser() args = parser.parse_args() if not hasattr(args, "callback"): parser.print_usage() sys.exit(os.EX_USAGE) args = dict(vars(args)) callback = args.pop("callback") with app.app_context(): callback()
sentry_sdk.init( dsn=settings.SENTRY_DSN, integrations=[ RedisIntegration(), RqIntegration(), SqlalchemyIntegration() ], release=read_version_from_file(), environment=settings.ENV, traces_sample_rate=settings.SENTRY_SAMPLE_RATE, ) logger.info("Worker : connection to sentry OK") while True: try: with app.app_context(): # This sessions removals are meant to prevent open db connection # to spread through forked children and cause bugs in the jobs # https://python-rq.org/docs/workers/#the-worker-lifecycle # https://docs.sqlalchemy.org/en/13/core/connections.html?highlight=dispose#engine-disposal db.session.remove() db.session.close() db.engine.dispose() with Connection(conn): worker = Worker(list(map(Queue, listen)), exception_handlers=[log_worker_error]) worker.work() except redis.ConnectionError: logger.warning("Worker connection error. Restarting in 5 seconds") time.sleep(5)
def wrapper(*args, **kwargs): with app.app_context(): return func(*args, **kwargs)
def log_database_connection_status(): with app.app_context(): if check_database_connection(): logger.info("Worker: database connection OK") else: logger.critical("Worker: database connection KO")