Package: django-dkron
django-dkronManage and run jobs in Dkron from your django projectCommand to launch dkron agentrun_dkrondjango-admin integration for managing dkron jobsnotifications (django-notification-sender) on failed jobsreverse proxy for dkron dashboard to leverage django authenticated user permissionsrun_asyncutility method to launch long running tasks (in a one-time temporary dkron job)SetupAdddkrontoINSTALLED_APPSin yoursettings.py.The following app settings are available for customization (fromdkron/apps.py)NameDefaultDescriptionDKRON_URLhttp://localhost:8888dkron server URLDKRON_PATHused to build browser-visible URLs to dkron - can be a full URL if no reverse proxy is being usedDKRON_BIN_DIRdirectory to store and execute the dkron binaries, defaults to temporary one - hardly optimal, do set one up!DKRON_VERSION3.2.7dkron version to (download and) useDKRON_DOWNLOAD_URL_TEMPLATEhttps://github.com/distribworks/dkron/releases/download/v{version}/dkron_{version}_{system}_{machine}.tar.gzcan be changed in case a dkron fork is meant to be usedDKRON_SERVERFalsealwaysrun_dkronin server modeDKRON_TAGS[]tags for the agent/server created byrun_dkron-label=tag is not required as it is added byDKRON_JOB_LABELDKRON_JOB_LABELlabel for the jobs managed by this app, used to make this app agent run only jobs created by this appDKRON_JOIN[]--join when usingrun_dkronDKRON_WORKDIRworkdir ofrun_dkronDKRON_ENCRYPTgossip encrypt key forrun_dkronDKRON_API_AUTHHTTP Basic auth header value, if dkron instance is protected with it (really recommended, if instance is exposed)DKRON_TOKENToken used byrun_dkronfor webhook calls into this appDKRON_PRE_WEBHOOK_URLURL called by dkron webhooks to post job start to this app - passed as--pre-webhook-urlto dkron, so you need to mapdkron.views.pre_webhookin your project urls.py and this should be full URL to that route and reachable by dkron. Requires DKRON_SENTRY_CRON_URL otherwise nothing would happen.DKRON_WEBHOOK_URLURL called by dkron webhooks to post job status to this app - passed as--webhook-urlto dkron, so you need to mapdkron.views.webhookin your project urls.py and this should be full URL to that route and reachable by dkronDKRON_NAMESPACEstring to be prefixed to each job created by this app in dkron so the same dkron cluster can be used by different apps/instances without conflicting job names (assuming unique namespaces ^^)DKRON_SENTRY_CRON_URLOptional Sentry URL used for monitoring jobs. Use placeholder<monitor_slug>in URL for job name.Besides starting the django app (with./manage.py runserver,gunicornor similar) also start dkron agent with./manage.py run_dkron:$ ./manage.py run_dkron -h
usage: manage.py run_dkron [-h] [-s] [-p HTTP_ADDR] [-j JOIN] [-e ENCRYPT] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
                           [--skip-checks]

Run dkron agent

optional arguments:
  -h, --help            show this help message and exit
  -s, --server          Run in server mode
  -p HTTP_ADDR, --http-addr HTTP_ADDR
                        Port used by the web UI
  -j JOIN, --join JOIN  Initial agent(s) to join with (can be used multiple times)
  -e ENCRYPT, --encrypt ENCRYPT
                        Key for encrypting network traffic. Must be a base64-encoded 16-byte key
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
  --force-color         Force colorization of the command output.
  --skip-checks         Skip system checks.Background tasksBesides managing the scheduled jobs in django-admin, this app also has therun_asyncutility method to run one-time temporary jobs.fromdkron.utilsimportrun_asyncjob_name,job_link=utils.run_async('some_management_command','arg1',kwarg='value',enable=True)This will return thejob_name(tmp_some_management_command_1in example) created in Dkron andjob_link(/dkron/proxy/ui/#/jobs/tmp_somecommand_1/show/executionsin example) as the direct link to the job executions in Dkron UI (this uses the settingDKRON_PATHto build the link).If dkron is not running,run_asyncfalls back toafter-responseto simplify the dev setup of your project.AuthenticationDkron does not have authorization (nor authentication). TheProversion does (and you should definitely get it if you're using it in a paid product/service :)) but this app provides a way to authenticate seamlessly to the Dkron dashboard from your project, by proxying access.There are two options: native django (default) and using nginx (or similar)djangoThis is the simplest way to implement it. Do not setDKRON_PATHsetting, as it will default to this but do setDKRON_URLproperly so the app can access dkron instance.This is will usedkron.views.proxyto forward any requests to/dkron/_/to Dkron URL, but not before requiring a valid django session with the permissiondkron.can_use_dashboard(or superuser).This does make every user access to Dkron UI to go through the full django project stack (andMIDDLEWAREs). If that's an issue (shouldn't be...), the old approach (usingnginxwithproxy_passandauth_request) might interest you.nginxThis was the original setup to leverage django sessions to access Dkron UI.nginxmight already be part of your production stack for caching and serving static files, so it's just adding an extra location to it.http {
  ...
  upstream dkronserver {
    server DKRON_SERVER_IP:DKRON_SERVER_PORT fail_timeout=0;
  }
  upstream appserver {
    server DJANGO_SERVER_IP:DJANGO_SERVER_PORT fail_timeout=0;
  }

  # IMPORTANT: cache nginx auth requests!
  proxy_cache_path /var/cache/nginx/auth_cache levels=1:2 keys_zone=auth_cache:1m max_size=100m inactive=60m;
  ...
  server {
    ...
    # path for django static files
    location /static/ {
      ...
    }

    location = /dkronauth {
      internal;
      # point to django app on `/dkron/auth`, this will validate the existing django session
      proxy_pass              appserver/dkron/auth/;
      proxy_cache             auth_cache;
      proxy_cache_key         "$host$request_uri $cookie_sessionid";
      proxy_pass_request_body off;
      proxy_set_header        Content-Length "";
      proxy_set_header        X-Original-URI $request_uri;
      proxy_set_header        Host $host;
      proxy_cache_valid       403 30s;
      proxy_cache_valid       200 5m;
    }

    location /dkron/ui/ {
      error_page 401 @dkronerror401;
      error_page 403 /403.html;
      error_page 404 /404.html;
      error_page 500 502 503 504 /500.html;
      auth_request     /dkronauth;
      auth_request_set $auth_status $upstream_status;
      proxy_pass http://dkronserver/;
      proxy_set_header Host $host;
      # if dkron is behind an nginx with basic auth required, uncomment to inject authorization header
      # proxy_set_header Authorization "Basic XXXXXX";
      proxy_redirect / $real_scheme://$host/dkron/ui/;
    }

    location @dkronerror401 {
      # force relative redirect - https://stackoverflow.com/a/39462409
	    return 302 " /login?next=$request_uri";
    }

    location / {
      ...
      proxy_pass $appserver;
    }
    ...ToDoMake notifications dependency optional?Document reverse proxy usage (for authentication) or create the JWT/oauth app (and recommend it from here).document WEBHOOK configuration (add tourls.py) as done in testapp.how to set DKRON_TOKEN.find anyFIXME/TODOin code