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

Django Q tasks are not executed with ./manage.py test #266

Closed
epinna opened this issue Oct 10, 2017 · 8 comments
Closed

Django Q tasks are not executed with ./manage.py test #266

epinna opened this issue Oct 10, 2017 · 8 comments

Comments

@epinna
Copy link

epinna commented Oct 10, 2017

Hello,

My models have some django Q task starting on certain action such as overridden Model.save() . The async tasks work correctly during functional testing, but the tasks are submitted but not executed if I hit the same code with ./manage.py test.

I just see some message of my 13:07:53 [Q] INFO Enqueued 1, probably of my tasks being queued, but they're not executed.

Is this something expected?

@Eagllus
Copy link
Collaborator

Eagllus commented Oct 10, 2017

Short answer is: Yes it is something expected.

By calling async you queue your task 13:07:53 [Q] INFO Enqueued 1.

There are a few ways you can processed.

  1. Execute the queued task during your test run
    or
  2. (what I have done in my projects) Is write tests for the tasks and test them.

@epinna
Copy link
Author

epinna commented Oct 10, 2017

Hey Eagllus,

Thanks for the clarification.

How to execute the queued tasks? Is something I should write at code-level within my tests or can I trigger that with some manage.py command?

Thanks

@epinna
Copy link
Author

epinna commented Oct 10, 2017

I looked at your testsuite, I think I'll write as the same as you do here:

https://github.com/Koed00/django-q/blob/master/django_q/tests/test_cluster.py#L111

Thanks

@epinna epinna closed this as completed Oct 10, 2017
@epinna
Copy link
Author

epinna commented Oct 11, 2017

I wrote in my tests something like this, which seems working for a single task:

            Model.save() # It submits an async task

            broker.queue_size()
            task_queue = Queue()
            stop_event = Event()
            stop_event.set()
            # push the tasks
            for i in range(task_count):
                pusher(task_queue, stop_event, broker=broker)
            task_queue.put('STOP')

            # let a worker handle them
            result_queue = Queue()
            worker(task_queue, result_queue, Value('f', -1))
            result_queue.put('STOP')

The problem is that I'm submitting a Chain of tasks, and it seems handling only the first one :(

Could you suggest me how to handle chain of tasks manually in order to be executed within TestCases?

Thanks for your help

@epinna epinna reopened this Oct 11, 2017
@Eagllus
Copy link
Collaborator

Eagllus commented Oct 12, 2017

@epinna a very easy solution would be to change a setting during you tests.

In your tests.py settings files

Q_CLUSTER = {
    'sync': True
}

or at the beginning of your test (if you only have a single test that needs it)

from django.conf import settings
settings.Q_CLUSTER['sync'] = True

This will remove the need to copy any code

@epinna
Copy link
Author

epinna commented Oct 12, 2017

That's what I've done at the end :) thanks for your help.

@epinna epinna closed this as completed Oct 12, 2017
@NickVeld
Copy link

In case someone hit this... If you see that async tasks are executed in some cases (functional testing like here or running docker compose up in my case or ...) and just [Q] INFO Enqueued 1 in some cases (unit testing like here or running docker compose run SERVICE_NAME SOME_CUSTOM_COMMAND or ...) , it means that your QCluster is not running. Most probably it is not running because the command starting it python manage.py qcluster intentionally or accidentally skipped in some environments.

@NickVeld
Copy link

P.S. About the same thing I wrote above: #203

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

3 participants