Spaces:
Sleeping
Sleeping
hf-actions
commited on
Commit
·
b42d9d1
1
Parent(s):
3513553
fix: retry FB token validation and add FORCE_RUN_DAILY override
Browse files
app.py
CHANGED
|
@@ -97,12 +97,25 @@ if __name__ == "__main__":
|
|
| 97 |
logger.error("Facebook token or page id missing")
|
| 98 |
ok = False
|
| 99 |
else:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
logger.exception("Facebook token validation failed")
|
| 107 |
ok = False
|
| 108 |
|
|
@@ -128,7 +141,8 @@ if __name__ == "__main__":
|
|
| 128 |
run_daily = os.getenv("RUN_DAILY_REPLICATE", "true").lower() in ("1", "true", "yes")
|
| 129 |
if run_daily:
|
| 130 |
logger.info("RUN_DAILY_REPLICATE enabled — validating tokens before starting background job")
|
| 131 |
-
|
|
|
|
| 132 |
def _bg():
|
| 133 |
interval_hours = int(os.getenv("DAILY_INTERVAL_HOURS", "24"))
|
| 134 |
while True:
|
|
@@ -158,7 +172,7 @@ Ultra-realistic photography style, fine-art cinematic composition, calming mood,
|
|
| 158 |
t.start()
|
| 159 |
logger.info("Started background daily generate_and_post thread")
|
| 160 |
else:
|
| 161 |
-
logger.error("Token validation failed — daily job will not start")
|
| 162 |
|
| 163 |
# Optionally run one immediate generation on startup (headless/autostart)
|
| 164 |
run_on_start = os.getenv("RUN_ON_START", "false").lower() in ("1", "true", "yes")
|
|
|
|
| 97 |
logger.error("Facebook token or page id missing")
|
| 98 |
ok = False
|
| 99 |
else:
|
| 100 |
+
# Retry DNS / network transient errors a few times before failing
|
| 101 |
+
import time as _time
|
| 102 |
+
_tries = 3
|
| 103 |
+
_ok = False
|
| 104 |
+
for _i in range(_tries):
|
| 105 |
+
try:
|
| 106 |
+
r = requests.get(f"https://graph.facebook.com/me?access_token={fb_token}", timeout=10)
|
| 107 |
+
if r.status_code == 200:
|
| 108 |
+
_ok = True
|
| 109 |
+
break
|
| 110 |
+
else:
|
| 111 |
+
logger.warning("Facebook token validation returned %s: %s", r.status_code, r.text)
|
| 112 |
+
# non-200 likely token issue — no retry
|
| 113 |
+
break
|
| 114 |
+
except Exception:
|
| 115 |
+
logger.warning("Facebook token validation attempt %s failed, retrying...", _i + 1)
|
| 116 |
+
_time.sleep(2)
|
| 117 |
+
continue
|
| 118 |
+
if not _ok:
|
| 119 |
logger.exception("Facebook token validation failed")
|
| 120 |
ok = False
|
| 121 |
|
|
|
|
| 141 |
run_daily = os.getenv("RUN_DAILY_REPLICATE", "true").lower() in ("1", "true", "yes")
|
| 142 |
if run_daily:
|
| 143 |
logger.info("RUN_DAILY_REPLICATE enabled — validating tokens before starting background job")
|
| 144 |
+
force_run = os.getenv("FORCE_RUN_DAILY", "false").lower() in ("1", "true", "yes")
|
| 145 |
+
if validate_tokens() or force_run:
|
| 146 |
def _bg():
|
| 147 |
interval_hours = int(os.getenv("DAILY_INTERVAL_HOURS", "24"))
|
| 148 |
while True:
|
|
|
|
| 172 |
t.start()
|
| 173 |
logger.info("Started background daily generate_and_post thread")
|
| 174 |
else:
|
| 175 |
+
logger.error("Token validation failed — daily job will not start (set FORCE_RUN_DAILY=true to override)")
|
| 176 |
|
| 177 |
# Optionally run one immediate generation on startup (headless/autostart)
|
| 178 |
run_on_start = os.getenv("RUN_ON_START", "false").lower() in ("1", "true", "yes")
|