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
版权声明:本文标题:flask - Python code executing when app is shut down? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295199a1929520.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论