graceful shutdown
This commit is contained in:
@@ -27,6 +27,20 @@ from foodie_utils import (
|
||||
)
|
||||
from foodie_hooks import get_dynamic_hook, select_best_cta
|
||||
|
||||
# Flag to indicate if we're in the middle of posting
|
||||
is_posting = False
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
logging.info("Received termination signal, checking if safe to exit...")
|
||||
if is_posting:
|
||||
logging.info("Currently posting, will exit after completion.")
|
||||
else:
|
||||
logging.info("Safe to exit immediately.")
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.INFO)
|
||||
file_handler = logging.FileHandler('/tmp/foodie_automator_google_trends.log', mode='a')
|
||||
@@ -231,6 +245,9 @@ def curate_from_google_trends(geo_list=['US']):
|
||||
cta = select_best_cta(post_data["title"], final_summary, post_url=None)
|
||||
post_data["content"] = f"{final_summary}\n\n{cta}"
|
||||
|
||||
global is_posting
|
||||
is_posting = True
|
||||
try:
|
||||
post_id, post_url = post_to_wp(
|
||||
post_data=post_data,
|
||||
category=category,
|
||||
@@ -243,10 +260,14 @@ def curate_from_google_trends(geo_list=['US']):
|
||||
pixabay_url=pixabay_url,
|
||||
interest_score=interest_score
|
||||
)
|
||||
finally:
|
||||
is_posting = False
|
||||
|
||||
if post_id:
|
||||
cta = select_best_cta(post_data["title"], final_summary, post_url=post_url)
|
||||
post_data["content"] = f"{final_summary}\n\n{cta}"
|
||||
is_posting = True
|
||||
try:
|
||||
post_to_wp(
|
||||
post_data=post_data,
|
||||
category=category,
|
||||
@@ -260,6 +281,8 @@ def curate_from_google_trends(geo_list=['US']):
|
||||
interest_score=interest_score,
|
||||
post_id=post_id
|
||||
)
|
||||
finally:
|
||||
is_posting = False
|
||||
|
||||
timestamp = datetime.now(timezone.utc).isoformat()
|
||||
save_json_file(POSTED_TITLES_FILE, title, timestamp)
|
||||
|
||||
@@ -23,6 +23,20 @@ from foodie_utils import (
|
||||
)
|
||||
from foodie_hooks import get_dynamic_hook, select_best_cta
|
||||
|
||||
# Flag to indicate if we're in the middle of posting
|
||||
is_posting = False
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
logging.info("Received termination signal, checking if safe to exit...")
|
||||
if is_posting:
|
||||
logging.info("Currently posting, will exit after completion.")
|
||||
else:
|
||||
logging.info("Safe to exit immediately.")
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
LOG_FILE = "/home/shane/foodie_automator/foodie_automator_reddit.log"
|
||||
LOG_PRUNE_DAYS = 30
|
||||
|
||||
@@ -257,6 +271,9 @@ def curate_from_reddit():
|
||||
|
||||
post_data["content"] = f"{final_summary}\n\n{cta}"
|
||||
|
||||
global is_posting
|
||||
is_posting = True
|
||||
try:
|
||||
post_id, post_url = post_to_wp(
|
||||
post_data=post_data,
|
||||
category=category,
|
||||
@@ -269,11 +286,15 @@ def curate_from_reddit():
|
||||
pixabay_url=pixabay_url,
|
||||
interest_score=interest_score
|
||||
)
|
||||
finally:
|
||||
is_posting = False
|
||||
|
||||
if post_id:
|
||||
cta = select_best_cta(post_data["title"], final_summary, post_url=post_url)
|
||||
post_data["content"] = f"{final_summary}\n\n{cta}"
|
||||
|
||||
is_posting = True
|
||||
try:
|
||||
post_to_wp(
|
||||
post_data=post_data,
|
||||
category=category,
|
||||
@@ -287,6 +308,8 @@ def curate_from_reddit():
|
||||
interest_score=interest_score,
|
||||
post_id=post_id
|
||||
)
|
||||
finally:
|
||||
is_posting = False
|
||||
|
||||
timestamp = datetime.now(timezone.utc).isoformat()
|
||||
save_json_file(POSTED_TITLES_FILE, title, timestamp)
|
||||
|
||||
@@ -23,6 +23,20 @@ import feedparser
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
# Flag to indicate if we're in the middle of posting
|
||||
is_posting = False
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
logging.info("Received termination signal, checking if safe to exit...")
|
||||
if is_posting:
|
||||
logging.info("Currently posting, will exit after completion.")
|
||||
else:
|
||||
logging.info("Safe to exit immediately.")
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
LOG_FILE = "/home/shane/foodie_automator/foodie_automator_rss.log"
|
||||
LOG_PRUNE_DAYS = 30
|
||||
MAX_WORKERS = 5 # Number of concurrent workers for parallel processing
|
||||
@@ -266,6 +280,9 @@ def curate_from_rss():
|
||||
cta = select_best_cta(post_data["title"], final_summary, post_url=None)
|
||||
|
||||
post_data["content"] = f"{final_summary}\n\n{cta}"
|
||||
global is_posting
|
||||
is_posting = True
|
||||
try:
|
||||
post_id, post_url = post_to_wp(
|
||||
post_data=post_data,
|
||||
category=category,
|
||||
@@ -278,10 +295,14 @@ def curate_from_rss():
|
||||
pixabay_url=pixabay_url,
|
||||
interest_score=interest_score
|
||||
)
|
||||
finally:
|
||||
is_posting = False
|
||||
|
||||
if post_id:
|
||||
cta = select_best_cta(post_data["title"], final_summary, post_url=post_url)
|
||||
post_data["content"] = f"{final_summary}\n\n{cta}"
|
||||
is_posting = True
|
||||
try:
|
||||
post_to_wp(
|
||||
post_data=post_data,
|
||||
category=category,
|
||||
@@ -295,6 +316,8 @@ def curate_from_rss():
|
||||
interest_score=interest_score,
|
||||
post_id=post_id
|
||||
)
|
||||
finally:
|
||||
is_posting = False
|
||||
|
||||
timestamp = datetime.now(timezone.utc).isoformat()
|
||||
save_json_file(POSTED_TITLES_FILE, title, timestamp)
|
||||
|
||||
Reference in New Issue
Block a user