Skip to content

Commit

Permalink
feat: yappi profiling
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed Nov 5, 2023
1 parent 6a07b76 commit fe4883e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/publish-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ jobs:
cache-from: type=gha,scope=docker-release
cache-to: type=gha,scope=docker-release,mode=max
- name: Set Docker meta (exp-deps)
if: ${{ endsWith(github.ref, 'dev') }}
id: meta-fpe
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKER_USERNAME }}/rss-to-telegram
flavor: latest=false
tags: type=raw,value=exp-deps,enable=true
tags: type=raw,value=${{ github.ref_name != 'dev' && format('{0}-', 'github.ref_name') || '' }}exp-deps,enable=true
- name: Push to Docker Hub (exp-deps)
if: ${{ endsWith(github.ref, 'dev') }}
uses: docker/build-push-action@v5
with:
push: true
Expand All @@ -69,8 +67,8 @@ jobs:
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta-fpe.outputs.tags }}
labels: ${{ steps.meta-fpe.outputs.labels }}
cache-from: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/rss-to-telegram:exp-deps
cache-to: type=inline,ref=${{ secrets.DOCKER_USERNAME }}/rss-to-telegram:exp-deps
cache-from: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/rss-to-telegram:${{ github.ref_name != 'dev' && format('{0}-', 'github.ref_name') || '' }}exp-deps
cache-to: type=inline,ref=${{ secrets.DOCKER_USERNAME }}/rss-to-telegram:${{ github.ref_name != 'dev' && format('{0}-', 'github.ref_name') || '' }}exp-deps

description:
name: Update repository description on Docker Hub
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ cachetools==5.3.1
CJKwrap==2.2
typing-extensions==4.7.1
uvloop==0.17.0; sys_platform!='win32' and sys_platform!='cygwin' and sys_platform!='cli'

# dev requirements
# TODO: drop requirements.txt and use pyproject.toml
yappi==1.4.0
19 changes: 17 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,22 @@
from . import log, db, command
from .i18n import i18n, ALL_LANGUAGES, get_commands_list
from .parsing import tgraph
from .compat import nullcontext

# log
logger = log.getLogger('RSStT')

if env.PROFILING:
try:
import yappi
yappi.set_clock_type(env.PROFILING if isinstance(env.PROFILING, str) else 'cpu')
logger.info(f'Profiling mode, clock type: {yappi.get_clock_type()}')
except ImportError:
logger.warning('yappi is not installed, profiling is disabled.')
yappi = None
else:
yappi = None

loop = env.loop
bot: Optional[TelegramClient] = None
pre_tasks = []
Expand Down Expand Up @@ -308,16 +320,19 @@ def main():
trigger=CronTrigger(minute='*', second=env.CRON_SECOND, timezone='UTC'),
max_instances=10,
misfire_grace_time=10)
scheduler.start()
with yappi.run() if yappi else nullcontext():
scheduler.start()
loop.run_until_complete(bot.disconnected)

loop.run_until_complete(bot.disconnected)
except (KeyboardInterrupt, SystemExit) as e:
logger.error(f'Received {type(e).__name__}, exiting...', exc_info=e)
exit_code = e.code if isinstance(e, SystemExit) and e.code is not None else 0
except Exception as e:
logger.critical('Uncaught error:', exc_info=e)
exit_code = 99
finally:
if yappi:
yappi.get_func_stats().print_all()
try:
if getattr(signal, 'SIGALRM', None):
signal.alarm(15)
Expand Down
11 changes: 11 additions & 0 deletions src/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def __list_parser(var: Optional[str]) -> list[str]:
LAZY_MEDIA_VALIDATION: Final = __bool_parser(os.environ.get('LAZY_MEDIA_VALIDATION'))
NO_UVLOOP: Final = __bool_parser(os.environ.get('NO_UVLOOP'))
MULTIPROCESSING: Final = __bool_parser(os.environ.get('MULTIPROCESSING'))

DEBUG: Final = __bool_parser(os.environ.get('DEBUG'))
__configure_logging( # config twice to make .env file work
level=colorlog.DEBUG if DEBUG else colorlog.INFO,
Expand All @@ -258,6 +259,16 @@ def __list_parser(var: Optional[str]) -> list[str]:
if DEBUG:
logger.debug('DEBUG mode enabled')

_profiling = os.environ.get('PROFILING')
if _profiling is None:
PROFILING: Final = False
else:
_profiling = _profiling.lower()
if _profiling in ('cpu', 'wall'):
PROFILING: Final = _profiling
else:
PROFILING: Final = __bool_parser(_profiling)

# ----- environment config -----
RAILWAY_STATIC_URL: Final = os.environ.get('RAILWAY_STATIC_URL')
PORT: Final = int(os.environ.get('PORT', 0)) or (8080 if RAILWAY_STATIC_URL else None)
Expand Down

0 comments on commit fe4883e

Please sign in to comment.