Skip to content

Improve builds stability #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/build-deck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ jobs:
python-version: 3.9
- name: Install requirements
run: pip install -r requirements.txt
- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d_%H:%M:%S')"
- name: Get current timestamp
id: timestamp
run: echo "::set-output name=timestamp::$(date +'%s')"
- name: Test build Anki Deck
run: python generate.py --stop 3
env:
LEETCODE_CSRF_TOKEN: ${{ secrets.LEETCODE_CSRF_TOKEN }}
LEETCODE_SESSION_ID: ${{ secrets.LEETCODE_SESSION_ID }}
- name: Build Anki Deck
run: python generate.py
if: github.ref == 'refs/heads/master'
env:
LEETCODE_CSRF_TOKEN: ${{ secrets.LEETCODE_CSRF_TOKEN }}
LEETCODE_SESSION_ID: ${{ secrets.LEETCODE_SESSION_ID }}
Expand All @@ -29,8 +36,9 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
tag_name: ${{ github.ref }}-${{ steps.timestamp.outputs.timestamp }}
release_name: >
Anki Deck from ${{ github.ref }} on ${{ steps.date.outputs.date }}
draft: true
prerelease: true
- name: Upload release asset
Expand Down
31 changes: 29 additions & 2 deletions generate.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/usr/bin/env python3

import argparse
import asyncio
import functools
import json
import logging
import os
import time
from functools import lru_cache
from typing import Any, Coroutine, Dict, Iterator, List, Tuple
from typing import Any, Callable, Coroutine, Dict, Iterator, List, Tuple

import diskcache
# https://github.com/kerrickstaley/genanki
import genanki # type: ignore
# https://github.com/prius/python-leetcode
import leetcode # type: ignore
import urllib3
from tqdm import tqdm

cookies = {
Expand Down Expand Up @@ -47,6 +48,31 @@ def parse_args() -> argparse.Namespace:
return args


def retry(times: int, exceptions: Tuple[Exception], delay: float) -> Callable:
"""
Retry Decorator
Retries the wrapped function/method `times` times if the exceptions listed
in `exceptions` are thrown
"""

def decorator(func):
@functools.wraps(func)
async def wrapper(*args, **kwargs):
for attempt in range(times - 1):
try:
return await func(*args, **kwargs)
except exceptions:
logging.exception(f"Exception occured, try {attempt + 1}/{times}")
time.sleep(delay)

logging.error("Last try")
return await func(*args, **kwargs)

return wrapper

return decorator


class LeetcodeData:
def __init__(self) -> None:

Expand All @@ -70,6 +96,7 @@ def __init__(self) -> None:
os.mkdir(CACHE_DIR)
self._cache = diskcache.Cache(CACHE_DIR)

@retry(times=3, exceptions=(urllib3.exceptions.ProtocolError,), delay=5)
async def _get_problem_data(self, problem_slug: str) -> Dict[str, str]:
if problem_slug in self._cache:
return self._cache[problem_slug]
Expand Down