p2
This commit is contained in:
@@ -1,37 +1,37 @@
|
|||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import random
|
||||||
|
|
||||||
# API stabil pentru a evita erorile de rețea anterioare
|
|
||||||
API_URL = os.getenv("QUOTE_API_URL", "https://api.adviceslip.com/advice")
|
API_URL = os.getenv("QUOTE_API_URL", "https://api.adviceslip.com/advice")
|
||||||
|
|
||||||
def get_quote():
|
def get_quote():
|
||||||
|
# Citate de rezervă în caz că rețeaua pică
|
||||||
|
fallback_quotes = [
|
||||||
|
{"q": "The only way to do great work is to love what you do.", "a": "Steve Jobs"},
|
||||||
|
{"q": "Innovation distinguishes between a leader and a follower.", "a": "Steve Jobs"},
|
||||||
|
{"q": "Stay hungry, stay foolish.", "a": "Steve Jobs"}
|
||||||
|
]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Timeout de 20s pentru a preveni blocarea pipeline-ului
|
# Încercăm să luăm de pe net (timeout mic să nu așteptăm mult)
|
||||||
response = requests.get(API_URL, timeout=20)
|
response = requests.get(API_URL, timeout=5)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
# Extragem mesajul (format AdviceSlip)
|
|
||||||
if 'slip' in data:
|
if 'slip' in data:
|
||||||
quote = data['slip'].get('advice')
|
quote, author = data['slip'].get('advice'), "AdviceSlip"
|
||||||
author = "AdviceSlip"
|
|
||||||
else:
|
else:
|
||||||
quote = data.get('content') or data.get('q') or "No message found."
|
quote = data.get('content') or data.get('q')
|
||||||
author = data.get('author') or data.get('a') or "Unknown"
|
author = data.get('author') or data.get('a') or "Unknown"
|
||||||
|
except Exception:
|
||||||
|
# Dacă rețeaua eșuează, alegem unul din listă
|
||||||
|
choice = random.choice(fallback_quotes)
|
||||||
|
quote, author = choice['q'], choice['a']
|
||||||
|
|
||||||
message = f"📜 *\"{quote}\"* \n\n✍️ **{author}**"
|
message = f"📜 *\"{quote}\"* \n\n✍️ **{author}**"
|
||||||
|
|
||||||
# Salvăm fișierul local în container (/app)
|
|
||||||
with open("quote.txt", "w", encoding="utf-8") as f:
|
with open("quote.txt", "w", encoding="utf-8") as f:
|
||||||
f.write(message)
|
f.write(message)
|
||||||
|
|
||||||
print(f"Succes! Mesaj pregătit.")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
with open("quote.txt", "w", encoding="utf-8") as f:
|
|
||||||
f.write(f"⚠️ Eroare la preluare: {str(e)[:80]}")
|
|
||||||
sys.exit(0) # Nu oprim pipeline-ul forțat
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
get_quote()
|
get_quote()
|
||||||
@@ -23,7 +23,7 @@ pipeline {
|
|||||||
dir('project2') {
|
dir('project2') {
|
||||||
try {
|
try {
|
||||||
// 3. Rulăm cu DNS forțat pentru a evita "NameResolutionError"
|
// 3. Rulăm cu DNS forțat pentru a evita "NameResolutionError"
|
||||||
sh "docker run --name quotes-worker --network host --dns 8.8.8.8 quotes-app"
|
sh "docker run --name quotes-worker --dns 8.8.8.8 quotes-app"
|
||||||
|
|
||||||
// 4. Extragem rezultatul
|
// 4. Extragem rezultatul
|
||||||
sh "docker cp quotes-worker:/app/quote.txt ."
|
sh "docker cp quotes-worker:/app/quote.txt ."
|
||||||
|
|||||||
Reference in New Issue
Block a user