Skip to content
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

Q: Watch entire module, updated via git, with possibly new files added to the module #26

Open
choucavalier opened this issue Aug 29, 2023 · 1 comment

Comments

@choucavalier
Copy link

I'm trying to implement a small application that's going to hot reload its code from a remote repository. What it does is simple: every 5 minutes, it fetches the remote branch origin/main and updates the mymodule code to take the new version of the code into account in the running process.

Q1. How can I make jurigged take into account potentially new files once they're added to my remote git repository and fetched locally?

Q2. Is jurigged going to behave nicely if this runs in an asyncio loop? With a coroutine that fetches the latest git version every 5 minutes?

Thanks!

import pathlib

import git
import jurigged
import mymodule
from loguru import logger

MYMODULE_DIR_PATH = pathlib.Path(mymodule.__path__[0]).parent
if not (MYMODULE_DIR_PATH / ".git").is_dir():
    raise RuntimeError(f"could not find git repo in {MYMODULE_DIR_PATH}")
jurigged.watch(str(MYMODULE_DIR_PATH.absolute()))
MYMODULE_REPO = git.Repo(MYMODULE_DIR_PATH)


def update_code_from_git(branch: str = "main") -> None:
    cur_hash = MYMODULE_REPO.remotes.origin.refs[branch].commit.hexsha
    logger.info(
        f"fetching origin/{branch} for repo mymodule. current hash is {cur_hash}"
    )
    MYMODULE_REPO.remotes.origin.fetch(branch)
    new_hash = MYMODULE_REPO.remotes.origin.refs[branch].commit.hexsha
    if new_hash != cur_hash:
        logger.info(
            f"new hash {new_hash} found. code will be hard reset to origin/{branch}"
            " and hot reloaded"
        )
        MYMODULE_REPO.heads[branch].reset(
            MYMODULE_REPO.remotes.origin.refs[branch], hard=True
        )
    else:
        logger.info("hash did not change")


def main():
    # TODO: implement
    pass


if __name__ == "__main__":
    main()
@breuleux
Copy link
Owner

breuleux commented Sep 7, 2023

I haven't really tested that use case, in principle I think it should just work. Asyncio should not pose any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants