Autor Zpráva
pavan
Profil
Moderátor Kajman: Vyhozeno z Diskuse JPW: Unable to use django-kronos. — Smazat 29. 9. 2022.
Moderátor Kajman: Piš prosím česky nebo slovensky.

I've been testing django-kronos with the simple example listed in the github readme:

I did pip3 install django-kronos,

myproject/myapp/cron.py:

import kronos
import random

@kronos.register('* * * * *')
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)
In my myproject/myproject/settings.py:

INSTALLED_APPS = [
'appointments.apps.AppointmentsConfig',
'clinic.apps.ClinicConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'bootstrap4',
'widget_tweaks',
'parsley',
'session_security',
'kronos'
]
Ran:

./manage.py runserver
I expected the dev server to throw up a message every minute. Nothing seems to be happening.

I also created myproject/myapp/management/commands/task.py:

from django.core.management.base import BaseCommand
import kronos
@kronos.register('* * * * *')
class Command(BaseCommand):
def handle(self, *args, **options):
print('Hello, world! KRONOS is running!!')
This is also not running.
RastyAmateur
Profil
The kronos just registeres some tasks and when you call python manage.py installtasks, it just updates your linux crontab so it contains registered tasks. Later on, all these registered commands run on their own inside the cron so no results ever appear in the terminal.

It is the same as if you created some basic management commands and added them into your crontab.

For better understanding, I strongly recommend some research on the topic "cron" or "crontab".

And at the end, I also strongly recommend not to use django-kronos app. It is old (made for django 1.7, now we have 4.1), deprecated and (in my point of view) useless - you can manage the crontab on your own, it should be independent of the django script.

Good luck with that.
pavan
Profil
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 [url=https://hkrtrainings.com/what-is-kronos-workforce]kronos[/https://hkrtrainings.com/what-is-kronos-workforce] 



@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

Vaše odpověď


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0