Skip to content

Commit

Permalink
crawler_status api
Browse files Browse the repository at this point in the history
  • Loading branch information
hiltay committed Oct 11, 2022
1 parent 77ca0c4 commit 868acff
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
35 changes: 32 additions & 3 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from api_dependencies.items import PassWord, GitHubEnv, VercelEnv, ServerEnv, FcSettings as item_fc_settings
from api_dependencies.utils.github_upload import bulk_create_or_update_secret, create_or_update_file, \
get_b64encoded_data, crawl_now
from api_dependencies.utils.vercel_upload import bulk_create_or_update_env, get_envs
from api_dependencies.utils.github_interface import bulk_create_or_update_secret, create_or_update_file, \
get_b64encoded_data, crawl_now, check_crawler_status
from api_dependencies.utils.vercel_interface import bulk_create_or_update_env, get_envs
from api_dependencies import format_response, tools

settings = get_user_settings()
Expand Down Expand Up @@ -315,6 +315,35 @@ async def run_crawl_now(payload: str = Depends(login_with_token_)):
return format_response.standard_response(code=resp["code"], message=resp["message"])


@app.get("/crawler_status", tags=["Manage"])
async def crawler_status(payload: str = Depends(login_with_token_)):
"""
查看crawler运行状态
status: 运行中;未运行;未知
"""
if settings["DEPLOY_TYPE"] == "github":
# 获取gh_access_token
gh_access_token = os.environ.get("GH_TOKEN")
gh_name = os.environ.get("GH_NAME")
if not gh_access_token or not gh_name:
return format_response.standard_response(code=400, message="缺少环境变量GH_TOKEN或GH_NAME")
repo_name = "hexo-circle-of-friends"
resp = await check_crawler_status(gh_access_token, gh_name, repo_name)
else:
# docker/server
resp = {"code": 200, "message": "检查成功"}
res = os.popen("ps -ef | egrep 'hexo_circle_of_friends/run.py' | grep -v grep | wc -l").read().strip()
if res == "1":
resp["status"] = "未运行"
elif res == "2":
resp["status"] = "运行中"
else:
resp["code"] = 500
resp["message"] = "检查运行状态失败"
resp["status"] = "未知"
return format_response.standard_response(**resp)


if __name__ == "__main__":
if settings["DEPLOY_TYPE"] == "docker":
uvicorn.run("main:app", host="0.0.0.0")
Expand Down
6 changes: 3 additions & 3 deletions api_dependencies/sql/security.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from . import db_interface
from hexo_circle_of_friends.models import Secret
from api_dependencies.utils import vercel_upload
from api_dependencies.utils.vercel_interface import get_env, create_or_update_env
from api_dependencies import tools


Expand All @@ -15,11 +15,11 @@ async def get_secret_key():
project_name = "hexo-circle-of-friends"
env_name = "SECRET_KEY"
env_value = secret_key
res = await vercel_upload.get_env(vercel_acces_token, project_name, env_name)
res = await get_env(vercel_acces_token, project_name, env_name)
if res:
secret_key = res.get("value")
else:
await vercel_upload.create_or_update_env(vercel_acces_token, project_name, env_name, env_value)
await create_or_update_env(vercel_acces_token, project_name, env_name, env_value)
else:
session = db_interface.db_init()
secret = session.query(Secret).all()
Expand Down
2 changes: 1 addition & 1 deletion api_dependencies/sql/sqlapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sqlalchemy.sql.expression import desc, func
from hexo_circle_of_friends.utils.process_time import time_compare
from api_dependencies.utils.validate_params import start_end_check
from api_dependencies.utils.github_upload import create_or_update_file, get_b64encoded_data
from api_dependencies.utils.github_interface import create_or_update_file, get_b64encoded_data
from api_dependencies.sql import db_interface, security
from api_dependencies import format_response, tools, dependencies as dep

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ async def crawl_now(gh_access_token: str, gh_name: str, repo_name: str):
return resp


async def check_crawler_status(gh_access_token: str, gh_name: str, repo_name: str):
"""
Check github workflow run status.
api: https://docs.github.com/cn/rest/actions/workflow-runs#list-workflow-runs-for-a-repository
"""
resp = {}
headers = {"Authorization": f"Bearer {gh_access_token}"}
content_url = f"https://api.github.com/repos/{gh_name}/{repo_name}/actions/runs"
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(content_url, verify_ssl=False) as response:
if response.status == 200:
content = await response.json()
resp["message"] = "检查运行状态成功"
resp["code"] = 200
# in_progress(运行中);completed(已完成);queued(队列中)
status = content["workflow_runs"][0]["status"]
if status == "in_progress" or "queued":
resp["status"] = "运行中"
else:
resp["status"] = "未运行"
else:
resp["message"] = "检查运行状态失败"
resp["code"] = 500
resp["status"] = "未知"
return resp


if __name__ == '__main__':
# import asyncio
# loop = asyncio.get_event_loop()
Expand Down
File renamed without changes.

0 comments on commit 868acff

Please sign in to comment.