question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG - setting `task_execution` to main does not seem to work.

See original GitHub issue

Describe the bug I want to have periodic tasks running in my main thread, but it does not seem to work. if initialized with nothing, rocketry will:

  1. attempt to run the tasks as async
  2. fail on the tasks because they cannot be async

this is ok because the log messages are helpful in suggesting that I should initialize the Rocketry app with `Rocketry(task_execution=‘async’) or something.

Problem is that when I initialize with (task_execution=‘main’), the startup fails. It seems that the log message is not up to date because docs suggest using ```python config={ ‘task_execution’: ‘main’ }


but this has apparently no affect on the configuration (see logs below)

**To Reproduce**
Here is the code I've used to initialize the tasks:
```python
from api.core.management.jobs import something_else
from api.core.management.jobs import something

from django.core.management.base import BaseCommand
import logging

from rocketry import Rocketry
from rocketry.conds import daily

from rocketry import Rocketry, Session
from rocketry.conds import daily, every
from rocketry.log import MinimalRecord
from redbird.repos import CSVFileRepo

class JobLogger(MinimalRecord):
    exc_text: Optional[str] = Field(description="Exception text")


repo = CSVFileRepo(filename="/tmp/api-logs/jobs.csv", model=JobLogger)

scheduler : Rocketry = Rocketry(
        logger_repo=repo,
        config={
            'task_execution': 'main'
        }
    )

# i've attempted to set scheduler.session.config.task_execution to `main` here, but to no avail

@scheduler.task(daily.at("00:10"), execution='main')
def do_something() -> None:
    logging.getLogger(__name__).info("Running do_something()")
    something.run()


@scheduler.task(daily.at("23:55"), execution='main')
def do_something_else() -> None:
    logging.getLogger(__name__).info("Running do_something_else()")
    something_else.run()


class Command(BaseCommand):
    help = "Setup the periodic jobs runner"

    def handle(self, *args, **options):
          scheduler.run()

Expected behavior Run the tasks in my main thread and not have the app crash on startup.

Screenshots

/usr/local/lib/python3.10/site-packages/rocketry/session.py:73: FutureWarning: Default execution will be changed to 'async'. To suppress this warning, specify task_execution, ie. Rocketry(task_execution='async') 

and then later on, when the job starts:

Traceback (most recent call last):
  File ""/usr/local/lib/python3.10/site-packages/rocketry/core/task.py"", line 536, in _run_as_async
    output = await self.execute(**params)
  File ""/usr/local/lib/python3.10/site-packages/rocketry/tasks/func.py"", line 234, in execute
    output = func(**params)
  File ""/server/api/core/management/commands/initjobs.py"", line 40, in do_something
    do_something.run()
  File ""/server/api/core/management/jobs/do_something.py"", line 46, in run
    for value in fetched_from_database:
  File ""/usr/local/lib/python3.10/site-packages/django/db/models/query.py"", line 394, in __iter__
    self._fetch_all()
  File ""/usr/local/lib/python3.10/site-packages/django/db/models/query.py"", line 1866, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/usr/local/lib/python3.10/site-packages/django/db/models/query.py"", line 87, in __iter__
    results = compiler.execute_sql(
  File ""/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py"", line 1393, in execute_sql
    cursor = self.connection.cursor()
  File ""/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py"", line 24, in inner
    raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async."

Desktop (please complete the following information): Python version: 3.10.7 Rocketry version: 2.4.1 Django version: 4.1

Additional context N/A

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Miksuscommented, Oct 3, 2022

That would be awesome! I’m personally not qualified for writing the documentation regarding how to integrate Rocketry with Django (as I haven’t used Django myself) but as it is so used, documentation regarding Rocketry+Django would extend Rocketry’s audience quite a lot.

0reactions
bogdzncommented, Oct 3, 2022

Awesome, will try to work on it by the end of the week. I have exams this week so I might be busy (& therefore a bit late) but i’ll do my best.

Regards

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Android Studio refuses to run main() - Stack Overflow
Usually I expect I can just System.out.printLn("Hello World") but this time, no matter if it's a new project, I get the following error:...
Read more >
An exception occurred during task execution. To see the full ...
Hi team, I am try to use the operator-sdk to create a private ... n mapping values are not allowed here\n\nThe error appears...
Read more >
Troubleshoot Amazon ECS stopped task - AWS
This error indicates that the task execution role being used doesn't have permission to communicate to Amazon ECS. To troubleshoot this issue:.
Read more >
Task executions ‒ Qlik NPrinting - Qlik | Help
Task execution notifications · In the main menu, go to Admin > Settings. · Click the Task button. · Under Notifications, you can...
Read more >
Spring Cloud Task Reference Guide
If we take a look at our original example, we can see that we are not ... Spring Cloud Task provides a ready-to-use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found