admin管理员组

文章数量:1122846

I have a python application in flask that has a function which requests data from the Instagram graph API and saves them in a database in intervals of 43200 second (12 hours). It then saves the result of the query (either SUCESS or ERROR) in a text file along with a time-stamp and the specification of the error if the request fails. When the request fails, I send an email using the simplegmail library to my gmail account to notify me of the error.

The function seems to be working fine. However, every day at around 18:30 I get an Error email from my app (even when the app is not running), saying that my request failed and that the error is [Errno 32] Broken pipe. How is this possible? Is my function running even when my app is not? I'm struggling to resolve the issue. Below is a snippet of code that handles the periodic requests.

def my_scheduled_task():
    with app.app_context():  # Push the app context
        file_path = "static/instagram_api_log.txt"
        token_record = Token.query.first()

        if token_record:
            access_token = token_record.access_token

            params = {'access_token': access_token}
            user_data_df = get_media(source_url, page_id, params)

            if user_data_df is not None:
                try:
                    save_media_to_database(user_data_df)

                    missing_days_profile_visits_len = check_for_missing_dates(ProfileVisit)
                    profile_views_data = get_profile_views(source_url, page_id, params, missing_days_profile_visits_len)
                    save_profile_visits(profile_views_data)

                    missing_days_profile_reach_len = check_for_missing_dates(ProfileReach)
                    profile_reach_data = get_profile_reach_per_day(source_url, page_id, params,
                                                                   missing_days_profile_reach_len)
                    save_profile_reach(profile_reach_data)

                    profile_follows_data = get_follows_per_day(source_url, page_id, params)
                    save_profile_follows(profile_follows_data)

                    success_message = "SUCCESS!"
                    current_time = datetime.now()
                    with open(file_path, 'a') as file:
                        # Append the current date and time to the file
                        file.write(str(current_time) + " " + success_message + "\n")

                except Exception as e:
                    error_message = f"ERROR! - Unknown error occurred in {e.__class__.__name__}: {str(e)}"
                    current_time = datetime.now()
                    with open(file_path, 'a') as file:
                        # Append the current date and time to the file
                        file.write(str(current_time) + " " + error_message + "\n")

                    admin_emails = User.get_admin_emails()
                    for email in admin_emails:
                        send_mail(email, "Instagram API Query Error", str(current_time) + " " + error_message + "\n")

            else:
                error_message = "ERROR! - get_media function returned None!"
                current_time = datetime.now()
                with open(file_path, 'a') as file:
                    # Append the current date and time to the file
                    file.write(str(current_time) + " " + error_message + "\n")

                admin_emails = User.get_admin_emails()
                for email in admin_emails:
                    send_mail(email, "Instagram API Query Error", str(current_time) + " " + error_message + "\n")

        else:
            error_message = "ERROR! - No access token found! Check the database or update your auth token!"
            current_time = datetime.now()
            with open(file_path, 'a') as file:
                # Append the current date and time to the file
                file.write(str(current_time) + " " + error_message + "\n")

            admin_emails = User.get_admin_emails()
            for email in admin_emails:
                send_mail(email, "Instagram API Query Error", str(current_time) + " " + error_message + "\n")


# Create a scheduler instance
scheduler = BackgroundScheduler()

# Call the task immediately for debugging purposes
my_scheduled_task()

# Add a job to the scheduler
scheduler.add_job(my_scheduled_task, IntervalTrigger(seconds=43200))

# Start the scheduler
scheduler.start()

本文标签: flaskPython code executing when app is shut downStack Overflow