Miksus / rocketry

Modern scheduling library for Python
https://rocketry.readthedocs.io
MIT License
3.25k stars 105 forks source link

BUG set_repo doesn't work, though it is mentioned in the documentation #108

Open vamshiaruru-virgodesigns opened 1 year ago

vamshiaruru-virgodesigns commented 1 year ago

Describe the bug I was going through the documentation here: https://rocketry.readthedocs.io/en/stable/cookbook/robust_applications.html?highlight=email#to-csv , and following the steps to add a repo to an app session, I get the error:

AttributeError: 'Session' object has no attribute 'set_repo'

To Reproduce This simple scripts will produce the error:

from rocketry import Rocketry
from rocketry.log import LogRecord
from redbird.repos import CSVFileRepo

app = Rocketry(config={
    'task_execution': 'async',
    'silence_task_prerun': True,
    'silence_task_logging': True,
    'silence_cond_check': True
})
repo = CSVFileRepo(model=LogRecord, filename="tasks.csv")
repo.create()
app.session.set_repo(repo, delete_existing=True)

Expected behavior The repo is set without any errors

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context What else can I do to set the repo in the meantime?

Miksus commented 1 year ago

Thanks for reporting this! Indeed, it seems the method was forgotten to be implemented. Cannot remember what I though when I wrote that piece of docs.

Rocketry simply extends the logging library and basically you just need a redbird.logging.RepoHandler in the logger called rocketry.task:

import logging
from redbird.repos import CSVFileRepo
from rocketry.log import LogRecord

from redbird.logging import RepoHandler

repo = CSVFileRepo(model=LogRecord, filename="tasks.csv")
repo.create()

task_logger = logging.getLogger("rocketry.task")
task_logger.handlers.insert(0, RepoHandler(repo=repo))

Put this somewhere before starting the app. Note that I used insert to make sure this is the first RepoHandler as the first repo handler is used for read and write from task logs. You can add other handlers as well as you wish.

I'll revisit this on the weekend.

vamshiaruru-virgodesigns commented 1 year ago

Thank you for your help and quick reply. I just tried it out and it works!

Miksus commented 1 year ago

Ye, it seems I meant to merge this to master before creating the latest version: https://github.com/Miksus/rocketry/pull/88

It seems I forgot (and still have some timezone issues with the new tests (my time zone is different than CI's)). It seems I changed them in the docs before doing this.

So I'll fix the PR soon and merge it and then this issue should be fixed as well.

realyashnag commented 1 year ago

Doing the same for MongoRepo fails.