Giter Club home page Giter Club logo

Comments (18)

chadgh avatar chadgh commented on July 26, 2024 1

I reduced the task to:

@background
def reindex(a, b):
    print('done')

This works in python 2 when running the process_tasks command. In python 3 it is silent. Num attempts isn't increased.

I'll create a django project from scratch in python 3 to see if I can reproduce in the simplest state.

from django-background-tasks.

chadgh avatar chadgh commented on July 26, 2024 1

settings.py wasn't changed except for the addition of background_task in the INSTALLED_APPS list.

I've reproduced this using the latest from PyPI (django-background-tasks==1.0.5).

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

Could you post your tasks.py please?

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

Regarding the logging mechanism, please see #16.

from django-background-tasks.

chadgh avatar chadgh commented on July 26, 2024
from background_task import background
from django.conf import settings
from django.core.mail import EmailMessage

from .search_indexes import full_index


@background
def reindex(config_slug, email_address):
    subject = '{} reindex complete.'.format(config_slug.title())
    msg = 'Reindex for {} finished successfully.'.format(config_slug)
    try:
        full_index(config_slug)
    except Exception as e:
        subject = '{} reindexing failed.'.format(config_slug.title())
        msg = 'There was an error while reindexing {}:\n{}'.format(config_slug, e.args)

    email = EmailMessage(subject, msg)
    email.to = [email_address]
    if settings.EMAIL_ADMINS_ON_REINDEX:
        email.bcc = list(settings.ADMINS)

    print('Sending email to {e}:\nSubject: {s}:\n{m}'.format(e=email_address, s=subject, m=msg))
    email.send()

This task works fine in Python2.

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

Change the relative import (.search_indexes) to an absolute one and it will work.

from django-background-tasks.

chadgh avatar chadgh commented on July 26, 2024

Made that change and it still isn't working.

The log says, INFO:root:Running Task(myapp.tasks.reindex) when I trigger the task, but the task in the database doesn't get the attempted count incremented and it doesn't move to completed tasks.

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

Could you give it a try with an empty method to test if it's the background_task that has the Py3 issue or something different, eg something related to full_index.

from django-background-tasks.

chadgh avatar chadgh commented on July 26, 2024

I'm not exactly sure what is going on but I did the following to reproduce and this time got an exception (in my actual project, it looks like it is just failing silently).

  1. Created a new django project (called core) in its own virtualenv with Python 3.4 and Django 1.7.10.
  2. Installed django-background-tasks.
  3. Created a tasks.py file with the simple task:
@background
def test():
    print('done')
  1. Imported and ran the test task from the django shell.
  2. Ran ./manage.py process_tasks

I get the following exception:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/background_task/management/commands/process_tasks.py", line 81, in handle
    if not tasks.run_next_task():
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/background_task/tasks.py", line 107, in run_next_task
    return self._runner.run_next_task(self)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/django/db/transaction.py", line 394, in inner
    return func(*args, **kwargs)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/background_task/tasks.py", line 230, in run_next_task
    self.run_task(tasks, task)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/django/db/transaction.py", line 394, in inner
    return func(*args, **kwargs)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/background_task/tasks.py", line 220, in run_task
    tasks.run_task(task.task_name, args, kwargs)
  File "/home/chadgh/.virtualenvs/tmp-3b687c658fb88a5/lib/python3.4/site-packages/background_task/tasks.py", line 100, in run_task
    proxy_task = self._tasks[task_name]
KeyError: 'core.tasks.test'

I also attempted this whole process by adding the task calling in a view and hitting the view in a running runserver, then running the process_tasks command. I got the exact same result.

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

We will try to reproduce this. Did you have specified something in the settings.py and are you using the latest release? Thanks for the additional information @chadgh .

from django-background-tasks.

hnakamur avatar hnakamur commented on July 26, 2024

I have the same issue and I created an example django project to reproduce this:
https://github.com/hnakamur/django-background-tasks-example
I attached results for Python 2 and Python 3 to the README at this page.

@philippeowagner Could you have a look at the example above? Thanks!

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

We fixed the bug, could you @hnakamur @chadgh please give a try to django-background-tasks-1.0.7? It's on PyPI. Thanks.

from django-background-tasks.

hnakamur avatar hnakamur commented on July 26, 2024

@philippeowagner I tried django-background-tasks-1.0.7 with Python 3.4.3 and confirmed it works correctly.
collinmutembei/django-background-tasks-example@feac03e
Thanks for the quick fix!

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

BTW @hnakamur you example project is awesome and could help noobies to set up django-background-tasks. It should be mentioned in the README. Is this in your interest as well (or will you drop it as it's fixed?)?

from django-background-tasks.

hnakamur avatar hnakamur commented on July 26, 2024

@philippeowagner It's my pleasure! Actually I am a newbie too and thought it is helpful if there is an example project using django-background-tasks.

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

Great @hnakamur , I will mention it in the README. :-)

from django-background-tasks.

philippeowagner avatar philippeowagner commented on July 26, 2024

@chadgh feel free if you still perform any issues.

from django-background-tasks.

chadgh avatar chadgh commented on July 26, 2024

Looks like it is working now. Thanks @philippeowagner @hnakamur for the fix!

from django-background-tasks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.