Package: gonk
GonkSetupInstall the library:pipinstallgonkYou can add contribution add-ons:For Mercure support:pipinstallgonk[mercure]For Django Rest Framework support:pipinstallgonk[drf]Or both of them:pipinstallgonk[drf,mercure]Add the application toINSTALLED_APPSin Djangosettings:INSTALLED_APPS=[# ...'gonk',]Launch migrations:pythonmanage.pymigrateUsageCreate taskrunner# taskrunners.pyfromgonk.taskrunnersimportTaskRunnerfromgonk.decoratorsimportregister,register_beatfromcelery.schedulesimportcrontab# Register taskrunner@register('my_taskrunner')classMyTaskRunner(TaskRunner):defrevert(self):# Specific implementationdefrun(self):# Specific implementation# Register scheduled taskrunner@register_beat('scheduled_taskrunner',crontab(minute='*'))classScheduledTaskRunner(TaskRunner):defrevert(self):# Specific implementationdefrun(self):# Specific implementationWe have to import the taskrunner within every app.
The best way to do so is inapps.pyclassMyAppConfig(AppConfig):# ...defready(self):from.importtaskrunnersLaunch taskfromgonk.tasksimportTaskargs={}Task.create_task('my_taskrunner',args)Revert taskfromgonk.tasksimportTaskt=Task.objects.last()t.revert()Cancel taskfromgonk.tasksimportTaskt=Task.objects.last()terminate:bool=Falset.cancel(terminate=terminate)CheckpointsYou can add checkpoints to register transcendent events within the task. Every checkpoint can send a notification
to the user to get feedback of the status and progress of the task.# taskrunners.pyfromgonk.taskrunnersimportTaskRunnerclassMyTaskRunner(TaskRunner):defrun(self):# Specific implementationself.task.log_status('STARTED',checkpoint=False)self.task.log_status('Checkpoint 1',checkpoint=True)self.task.log_status('FINISHED')Command to list registered taskrunnersWe can list the registered taskrunner with the commandlist_taskrunners.pythonmanage.pylist_taskrunnersCommand to launch tasks manuallyWe can create tasks using the commandcreate_tasks.pythonmanage.pycreate_task--help
usage:manage.pycreate_task[-h][--inputINPUT][--raw-inputRAW_INPUT][--queueQUEUE][--whenWHEN][--version][-v{0,1,2,3}][--settingsSETTINGS][--pythonpathPYTHONPATH][--traceback][--no-color][--force-color][--skip-checks]task_type

positionalarguments:task_typeTasktypeidentifier

options:-h,--helpshowthishelpmessageandexit--inputINPUTFileinput--canberedirectedfromstandardoutput--raw-inputRAW_INPUTRawstringinput--Mustbeinjsonformat--queueQUEUECeleryqueuenameinwhichthetaskwillberun--whenWHENScheduledtaskrundate--ISOFormatExamples:pythonmanage.pycreate_task<task_type>--raw-input='{}'catfile.json|pythonmanage.pycreate_task<task_type>--queue="celery"--input-SetupEnvironment variableTypeDescriptionKEEP_TASK_HISTORY_DAYSintNumber of days to keep the tasksDEFAULT_NOTIFICATION_EMAILstrDefault e-mail to notifyDjango Rest FrameworkTo use Django Rest Framework extension we have to install with thedrfextra.In our projecturls.pywe have to add the Gonk urls:fromdjango.urlsimportpath,includeurlpatterns=[# ...path('tasks/',include('gonk.contrib.rest_framework.urls')),]Notifications with MercureTo use Mercure extension we have to install with themercureextra.To send notifications with Mercure we have to setup the following environment variables:VariableTypeDescriptionMERCURE_HUB_URLstrMercure service URLMERCURE_JWT_KEYstrMercure's JWT Token to publish events# taskrunners.pyfromgonk.taskrunnersimportTaskRunnerfromgonk.contrib.notifications.mercureimportMercureNotificationMixinclassMyTaskRunner(MercureNotificationMixin,TaskRunner):# Specific implementationDevelopmentClone repositorygitclonegit@github.com:kasfactory/gonk.git&&cdgonkInstall poetrypipinstallpoetryInstall dependenciespoetryinstallRun docker-composedocker-composeup-dLaunch celery workerpoetryruncelery-Atest_appworkerLaunch celery beatpoetryruncelery-Atest_appbeatAt this point, we have to ensure thatgonk.tasks.to_run,gonk.tasks.to_revertandgonk.tasks.to_scheduletasks are detectedCreditsAuthorsFrancisco Javier LendínezPablo Moreno