Code Monkey home page Code Monkey logo

django-kronos's Introduction

https://raw.githubusercontent.com/jgorset/django-kronos/master/docs/banner.png

https://coveralls.io/repos/github/jgorset/django-kronos/badge.svg?branch=master https://travis-ci.org/jgorset/django-kronos.svg?branch=master

Usage

Define tasks

Kronos collects tasks from cron modules in your project root and each of your applications:

# app/cron.py

import kronos
import random

@kronos.register('0 0 * * *')
def complain():
    complaints = [
        "I forgot to migrate our applications's cron jobs to our new server! Darn!",
        "I'm out of complaints! Damnit!"
    ]

    print random.choice(complaints)

Kronos works with Django management commands, too:

# app/management/commands/task.py

from django.core.management.base import BaseCommand

import kronos

@kronos.register('0 0 * * *')
class Command(BaseCommand):
    def handle(self, *args, **options):
        print('Hello, world!')

If your management command accepts arguments, just pass them in the decorator:

# app/management/commands/task.py

from django.core.management.base import BaseCommand

import kronos

@kronos.register('0 0 * * *', args={'-l': 'nb'})
class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument(
            '-l', '--language',
            dest='language',
            type=str,
            default='en',
        )

    def handle(self, *args, **options):
        if options['language'] == 'en':
          print('Hello, world!')

        if options['language'] == 'nb':
          print('Hei, verden!')

Run tasks manually

$ python manage.py runtask complain
I forgot to migrate our applications's cron jobs to our new server! Darn!

Keep in mind that if the registered task is a django command you have to run it in the normal way:

$ python manage.py task

List all registered tasks

$ python manage.py showtasks
* List of tasks registered in Kronos *
>> Kronos tasks
    >> my_task_one
    >> my_task_two
>> Django tasks
    >> my_django_task

Register tasks with cron

$ python manage.py installtasks
Installed 1 task.

You can review the crontab with a crontab -l command:

$ crontab -l
0 0 * * * /usr/bin/python /path/to/manage.py runtask complain --settings=myprpoject.settings $KRONOS_BREAD_CRUMB
0 0 * * * /usr/bin/python /path/to/manage.py task --settings=myprpoject.settings $KRONOS_BREAD_CRUMB

Usually this line will work pretty well for you, but there can be some rare cases when it requires modification. You can achieve it with a number of settings variables used by kronos:

KRONOS_PYTHON
Python interpreter to build a crontab line (defaults to the interpreter you used to invoke the management command).
KRONOS_MANAGE
Management command to build a crontab line (defaults to manage.py in the current working directory).
KRONOS_PYTHONPATH
Extra path which will be added as a --pythonpath option to the management command.
KRONOS_POSTFIX
Extra string added at the end of the command. For dirty things like > /dev/null 2>&1
KRONOS_PREFIX
Extra string added at the beginning of the command. For dirty things like source /path/to/env &&. If you use the virtualenv, you can add the environment path by echo "KRONOS_PREFIX = 'source `echo $VIRTUAL_ENV`/bin/activate && '" >> myprpoject/settings.py

Define these variables in your settings.py file if you wish to alter crontab lines.

The env variable $KRONOS_BREAD_CRUMB is defined to detect which tasks have to be deleted after being installed.

Installation

$ pip install django-kronos

... and add kronos to INSTALLED_APPS.

Contribute

  • Fork the repository.
  • Do your thing.
  • Open a pull request.
  • Receive cake.

I love you

Johannes Gorset made this. You should tweet me if you can't get it to work. In fact, you should tweet me anyway.

django-kronos's People

Contributors

atmb4u avatar carrerasrodrigo avatar csabbey avatar davidmoreno avatar dbarden avatar helenst avatar j4mie avatar jgorset avatar joshblum avatar julzhk avatar mightypixel avatar mikluko avatar pawelad avatar ruimartinsptl avatar sih4sing5hong5 avatar stefanfoulis avatar thomwiggers avatar whusterj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-kronos's Issues

Change kronos interval (Django Admin)

I have a little problem with call_command in Django. This is my Kronos decorator:

@kronos.register('*/{} * * * *'.format(config.CRON_MIN))
In admin.py I have a function which returns new config.CRON_MIN value and runs call_command to reload cron jobs:

@receiver(config_updated)
def constance_updated(sender, key, old_value, new_value, **kwargs):
    call_command('installtasks')
    print(key, old_value, new_value)

In my terminal window I see:

1 tasks removed, 1 installed.
CRON_MIN 3 4

but when I check it by typing: crontab -l I see cron job with old value of CRON_MIN. When I run installtasks command manually it works as It should (python manage.py installtask). It properly changes interval in kronos cron job. Where I make a mistake? Can you help me?

Mock tests

Kronos' test suite is currently overwriting and backing up the user's crontab file. This is really really bad and I should really really mock the system calls.

Cronjob installed but not called

Hi,
I'm using django-kronos with MongoDB locally in my computer, I registered tasks using ./manage.py installtasks i can see them in crontab list and when i run them manually it works fine.

I don't know why tasks are not getting called throw cron.

ModuleNotFoundError: No module named 'django.utils.importlib'

Hi,
i just installed kronos==0.2.2 and it gives me this error when i add it to my project's INSTALLED_APPS:

ModuleNotFoundError: No module named 'django.utils.importlib'

seems that in Django 3.1.4, importlib is not exists anymore and its part of python now. try:

from importlib import import_module

please fix it asap. many thanks!

only first job runs.

Hi there - thanks for the great library. Question . - I have everything setup and registered and it seems to work perfectly to run tasks manually etc. When I review the cron logs though only the first entry in crontab is getting executed.

I added the POSTFIX below to try and resolve the issue, but doesn't seem to help

*/15 * * * * source /opt/python/current/env && /opt/python/run/venv/bin/python /opt/python/bundle/61/app/manage.py runtask subit_vi --settings=ops.settings # kronos:277d8af879df25a388f98ea0034ca4a3
*/20 * * * * source /opt/python/current/env && /opt/python/run/venv/bin/python /opt/python/bundle/61/app/manage.py runtask import_published --settings=ops.settings # kronos:277d8af879df25a388f98ea0034ca4a3
*/30 * * * * source /opt/python/current/env && /opt/python/run/venv/bin/python /opt/python/bundle/61/app/manage.py runtask warm_cache --settings=ops.settings # kronos:277d8af879df25a388f98ea0034ca4a3

I have the following in my settings.
KRONOS_PREFIX = 'source /opt/python/current/env &&'
KRONOS_POSTFIX ='printf "\r"'

Multiple projects overwrite each others cronjobs in the same crontab

When installing tasks from two separate projects into the same crontab the tasks will be removed from the other project. The check for $KRONOS_BREAD_CRUMB when uninstalling tasks removes any task installed from kronos not just the ones for the current project.

Changing the line to check for both the bread crumb and the exp would correct it

if '$KRONOS_BREAD_CRUMB' not in line and exp not in line:

to

if not ('$KRONOS_BREAD_CRUMB' in line and exp in line):

Incorrect manage.py location guessing

Hi,

I just found django-kronos and it looks like a great idea. However, it's not currently working with the way my project is laid out.

I'm using a Django 1.4-style layout, and have a settings package (with settings/common.py and settings/development.py etc) rather than a single file. Kronos tasks end up guessing the location of manage.py incorrectly.

@hourly /home/jamie/pulse/env/bin/python /home/jamie/pulse/pulse/settings/manage.py runtask generatestats

I'm happy to work on this and submit a patch if you like, but I'd rather check your preferred approach.

django-supervisor solves this problem by searching for the manage.py file in a few places in the filesystem, starting from the location of the settings file and working upwards. It also allows you to override the location by passing a --projectdir flag to the management command. This works but seems a bit messy. See: https://github.com/rfk/django-supervisor/blob/master/djsupervisor/config.py#L231

Another option might be to exploit the fact that installtasks is itself run with manage.py and use something like:

os.path.join(os.getcwd(), sys.argv[0])

Cheers

Ability to register command in crontab with arguments

I found this great package but I missed one thing: passing arguments.

Illustrating with the given example: I could used something like

@kronos.register('0 0 * * *', args={"--arg1": None, "-b": "some-arg2"})
def complain():
    # (...)

then the respective crontab entry would be

0 0 * * * /path/to/python /path/to/manage.py command --arg1 -b=some-arg2 --settings=geoviewer.settings &> /dev/null$KRONOS_BREAD_CRUMB

Thanks!

sometimes installtasks but tasks cannot run

Django-kronos is a great project.
But a little bug here.
When I install tasks like this:

python src/manage.py installtasks

then will generate cron like this:

*/5 * * * * python /git/turing/manage.py runtask check_sync  --settings=config.local # kronos:3be27dc50a19cd6230f3de732462fa99

The right cron etc should be:

*/5 * * * * python /git/turing/src/manage.py runtask check_sync  --settings=config.local # kronos:3be27dc50a19cd6230f3de732462fa99

The bug is that the path of manage.py calculating wrong

'NoArgsCommand' is removed in Django 1.10

NoArgsCommand is removed in newest Django release (1.10) which makes django-kronos unusable:

$ python manage.py showtasks
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/.../lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "..../lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ".../lib/python3.4/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File ".../lib/python3.4/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File ".../lib/python3.4/site-packages/kronos/management/commands/showtasks.py", line 9, in handle
    kronos.load()
  File ".../lib/python3.4/site-packages/kronos/__init__.py", line 43, in load
    load_command_class(app, cmd)
  File ".../lib/python3.4/site-packages/django/core/management/__init__.py", line 40, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File ".../lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File ".../lib/python3.4/site-packages/kronos/management/commands/installtasks.py", line 1, in <module>
    from django.core.management.base import NoArgsCommand
ImportError: cannot import name 'NoArgsCommand'

Quickly looking at the docs and the source i think it should be pretty easy to fix. I could try to tackle it and do a PR if you're busy.

Only one of two jobs run in a single file

Hi there - thanks for the great library. Question . - I have everything setup and registered and it seems to work perfectly to run tasks manually etc. When I review the cron logs though only two of my tasks are executed. The only thing I can think of is that I have t

I added the POSTFIX below to try and resolve the issue, but doesn't seem to help

*/15 * * * * source /opt/python/current/env && /opt/python/run/venv/bin/python /opt/python/bundle/61/app/manage.py runtask subit_vi --settings=ops.settings # kronos:277d8af879df25a388f98ea0034ca4a3
*/20 * * * * source /opt/python/current/env && /opt/python/run/venv/bin/python /opt/python/bundle/61/app/manage.py runtask import_published --settings=ops.settings # kronos:277d8af879df25a388f98ea0034ca4a3
*/30 * * * * source /opt/python/current/env && /opt/python/run/venv/bin/python /opt/python/bundle/61/app/manage.py runtask warm_cache --settings=ops.settings # kronos:277d8af879df25a388f98ea0034ca4a3

`@kronos.register('*/20 * * * *')
def import_published():
import_data(False, 3)

@kronos.register('10,30,50 * * * *')
def subit_vi():
send_new_video_to_ai(`

I have the following in my settings.
KRONOS_PREFIX = 'source /opt/python/current/env &&'
KRONOS_POSTFIX ='\r\n'

Fails to initialize when settings module resides outside of project

$ DJANGO_SETTINGS_MODULE=local_settings ./manage.py runserver
Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "[...]/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "[...]/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "[...]/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "[...]/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "[...]/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "[...]/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/akabos/Projects/@virtualenvs/rentals/lib/python2.7/site-packages/kronos/__init__.py", line 5, in <module>
    from kronos.settings import PROJECT_MODULE, KRONOS_PYTHON, KRONOS_MANAGE, \
  File "[...]/lib/python2.7/site-packages/kronos/settings.py", line 9, in <module>
    PROJECT_MODULE = sys.modules['.'.join(settings.SETTINGS_MODULE.split('.')[:-1])]
KeyError: ''

Does it work on Django > 3.2?

I am not sure, but the using "pip install django-kronos" worked fine, but when I add the kronos into my DJango apps I get a module not found error, but the cron job is working fine! The thing is I am using Daphne to handle websockets, and its status fails with error module not found. I am not sure what to do in such case.

Can't installtasks because of IOError

I'm getting an IOError of "Please specify user or filename to write" when I try to register the tasks with cron. This is raised in the crontab.py file. The crontab app was downloaded along with the installation of kronos.

I got the showtasks and runtasks command to run but I can register the tasks using the installtasks command

Do you guys have an idea how to solve this?.

Django 1.8 warning

I've just tested upgrading to Django 1.8 and got a warning when ran runserver.

/home/paulo/.virtualenvs/data-279-upd/src/django-kronos/kronos/__init__.py:4: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
  from django.utils.importlib import import_module

Line in question: https://github.com/jgorset/django-kronos/blob/master/kronos/__init__.py#L4

Don't know what it means, but I feel you should know about it.

Can't install cron tasks

Hi dude,

I've installed django-kronos and kronos packages via pip successfully.

Then I've add 'kronos' to installed_apps of project settings file.

After than, I create a simple cron in this path (/path/to/project/management/commands/task.py) but when i run these commands, it seems it cant find my jobs.

python manage.py installtasks
0 tasks installed.
python manage.py showtasks
* List of tasks registered in Kronos *
>> Kronos tasks
>> Django tasks

I also tried adding task.py in root project folder but doesn't work (same as above)

My Django version is 1.8.

Would you please help me?

kronos task is logging SQL queries

I just created my first kronos task and while it nearly works like it should .. its printing all SQL queries executed during that task.

SQL logging is not enabled in the Django logging settings and running the same code as a Django management command also does not print SQL statements. So i was wondering if kronos is triggering that and why?

Per server crons?

We run our code on multiple servers, and we want different cron jobs to run on different servers, depending on the server's role. Is there a way to handle this with django-kronos? If not, how about a register function parameter that takes a name from django.conf.settings, and the value of that setting has to be True to register on that server (and default to True if it's missing, for backwards-compatibility)?

pip install failing in Python 3.4

pip install django-kronos is failing for me in Python 3.4 with NameError: name 'execfile' is not defined. I implemented this fix in my fork and have been using that instead.

I've tested this in both 2.7.6 and 3.4.0. Do you see any issue with this fix? If not, would you like a pull request?

Great library. Many thanks.

Give some hints on what's wrong when tasks fail to install due to `ImportError`

When registering a cronjob, if any import is wrong and raises an ImportError, the job won't be registered and there is no indication of what failed after running installtasks:

import kronos

raise ImportError

kronos.register('@hourly'):
def i_wont_install():
    print "I won't install"
$ python manage.py installtasks
Installed 0 tasks

It would be nice to have an error message indicating why registering the task failed.

Thank you for django-kronos!

Does not work with AppConfig

When using AppConfig (https://docs.djangoproject.com/en/1.8/ref/applications/#for-application-authors), kronos does not import the proper cron.py.

As a workaround I import cron from init.py, and it works.

A proper fix I guess should use utils.module_loading.autodiscover_modules('cron'). I have a working patch, but not sure. I do separate pull request that does that.

I used AppConfig as I have two modules with the same directory name. It is the django > 1.7 way to allow it.

Getting Killed And Don't know the reason

I'm running a heavy job and in the middle of it (not necessarily at the same moment) I get "killed" and the job stops.

How do I know what happened?

EDIT: I had a memory leak. Nothing related to Kronos. Sorry

ImportError: cannot import name 'gen_filenames' from 'django.utils.autoreload'

Hi,

i tried using your module in a business app. I run in the following error whenever i try using runtask or showtasks:

python rms/manage.py showtasks Traceback (most recent call last): File "rms/manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute output = self.handle(*args, **options) File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/kronos/management/commands/showtasks.py", line 9, in handle kronos.load() File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/kronos/__init__.py", line 43, in load load_command_class(app, cmd) File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/django/core/management/__init__.py", line 36, in load_command_class module = import_module('%s.management.commands.%s' % (app_name, name)) File "/path_to_project/.virtualenv/django-rms/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/django_extensions/management/commands/runserver_plus.py", line 17, in <module> from django.utils.autoreload import gen_filenames ImportError: cannot import name 'gen_filenames' from 'django.utils.autoreload' (/path_to_project/.virtualenv/django-rms/lib/python3.7/site-packages/django/utils/autoreload.py)

Do you know what i could try to solve this?

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.