Repository navigation
Add Callbacks for Workflow lifecycle events #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Added user-registered callbacks that fire on `Workflow` lifecycle events. A new `CallbackService` | ||
| resource (modeled after pulpcore's `SigningService`) points at an absolute path to an executable; it | ||
| is attached to a workflow via a `WorkflowCallback` whose `callback_type` selects the event | ||
| (`running`, `completed`, etc). The script runs as a Pulp task with workflow context available as | ||
| environment variables. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # Demo: sync + publish a file repo via a Workflow, with a messaging callback | ||
|
|
||
| Drives `pulp_workflow` end-to-end: register a `CallbackService` (a script that | ||
| POSTs to a webhook, e.g. Discord/Slack), create a `file` repo + remote, POST a | ||
| `Workflow` that syncs then publishes with a `finished` callback, and watch it | ||
| run. | ||
|
|
||
| Assumes a running dev stack (`oci-env compose up`), a configured pulp-cli, and | ||
| `NOTIFY_WEBHOOK` available to the Pulp worker. With `oci-env`, add it to | ||
| `compose.env` and bounce the stack so the new value is picked up. For testing, | ||
| [`httpbin`](https://httpbin.org) returns 200 for any POST: | ||
|
|
||
| ```bash | ||
| echo 'NOTIFY_WEBHOOK=https://httpbin.org/post' >> ../oci_env/compose.env | ||
| oci-env compose up -d # recreate the pulp container so env_file is re-read | ||
| ``` | ||
|
|
||
| The demo uses fixed resource names, so run `oci-env pdbreset` between runs to | ||
| wipe the Pulp DB. | ||
|
|
||
|
|
||
| ## 1. Point at the notify script | ||
|
|
||
| `oci-env` bind-mounts your `pulp-dev/` checkout at `/src` inside the `pulp` | ||
| container, so [notify.sh](notify.sh) is visible to the worker at: | ||
|
|
||
| ```bash | ||
| SCRIPT_PATH=/src/pulp_workflow/docs/demo/notify.sh | ||
| oci-env exec ls -la "$SCRIPT_PATH" # sanity check: file is present and +x | ||
|
daviddavis marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| ## 2. Register the CallbackService | ||
|
|
||
| ```bash | ||
| CB_HREF=$(http POST :5001/pulp/api/v3/workflow/callback-services/ \ | ||
| name=demo-messaging-notify \ | ||
| script=$SCRIPT_PATH \ | ||
| | jq -r .pulp_href) | ||
| echo "callback_service: $CB_HREF" | ||
| ``` | ||
|
|
||
| ## 3. Create the repository and remote | ||
|
|
||
| ```bash | ||
| REPO_NAME=demo-file-repo | ||
| REMOTE_NAME=demo-file-remote | ||
|
|
||
| pulp file repository create --name "$REPO_NAME" | ||
| pulp file remote create \ | ||
| --name "$REMOTE_NAME" \ | ||
| --url https://fixtures.pulpproject.org/file/PULP_MANIFEST \ | ||
| --policy immediate | ||
|
|
||
| REPO_HREF=$(pulp file repository show --name "$REPO_NAME" | jq -r .pulp_href) | ||
| REMOTE_HREF=$(pulp file remote show --name "$REMOTE_NAME" | jq -r .pulp_href) | ||
|
|
||
| # pulpcore tasks take pks, not hrefs, so strip the trailing UUID segment. | ||
| REPO_PK=$(echo "$REPO_HREF" | awk -F/ '{print $(NF-1)}') | ||
| REMOTE_PK=$(echo "$REMOTE_HREF" | awk -F/ '{print $(NF-1)}') | ||
| ``` | ||
|
|
||
| ## 4. Create the workflow | ||
|
|
||
| The `publish` task's `repository_version_pk` is a *dynamic* arg | ||
| (`content_type: core.repositoryversion`) — the workflow engine resolves it at | ||
| dispatch time from the previous task's `created_resources`. One `finished` | ||
| callback fires on any terminal state. | ||
|
|
||
| ```bash | ||
| WF_HREF=$(http POST :5001/pulp/api/v3/workflow/workflows/ <<JSON | jq -r .pulp_href | ||
| { | ||
| "name": "demo-workflow", | ||
| "tasks": [ | ||
| { | ||
| "task_name": "pulp_file.app.tasks.synchronizing.synchronize", | ||
| "reserved_resources": ["$REPO_HREF"], | ||
| "task_kwargs": [ | ||
| {"kwarg_key": "repository_pk", "value": "$REPO_PK"}, | ||
| {"kwarg_key": "remote_pk", "value": "$REMOTE_PK"}, | ||
| {"kwarg_key": "mirror", "value": false} | ||
| ] | ||
| }, | ||
| { | ||
| "task_name": "pulp_file.app.tasks.publish", | ||
| "reserved_resources": ["$REPO_HREF"], | ||
| "task_kwargs": [ | ||
| {"kwarg_key": "repository_version_pk", | ||
| "content_type": "core.repositoryversion"} | ||
| ] | ||
| } | ||
| ], | ||
| "callbacks": [ | ||
| {"callback_service": "$CB_HREF", "callback_type": "finished"} | ||
| ] | ||
| } | ||
| JSON | ||
| ) | ||
| echo "WF_HREF=$WF_HREF" | ||
| ``` | ||
|
|
||
| ## 5. Watch it run | ||
|
|
||
| ```bash | ||
| while :; do | ||
| STATE=$(http :5001"$WF_HREF" | jq -r .state) | ||
| echo "state=$STATE" | ||
| case "$STATE" in | ||
| completed|failed|canceled) break ;; | ||
| esac | ||
| sleep 2 | ||
| done | ||
|
|
||
| http :5001"$WF_HREF" | jq '{state, started_at, finished_at, error, | ||
| tasks: [.tasks[] | {task_name, dispatched_task}], | ||
| callbacks: [.callbacks[] | {callback_type, dispatched_task}]}' | ||
| ``` | ||
|
|
||
| On success: `state: "completed"`, every `dispatched_task` non-null, and a new | ||
| `RepositoryVersion` + `Publication`: | ||
|
|
||
| ```bash | ||
| pulp file repository version list --repository "$REPO_NAME" | ||
| pulp file publication list --repository "$REPO_NAME" | ||
| ``` | ||
|
|
||
| The callback task ending in `completed` means `curl -fsS` got a 2xx from the | ||
| webhook: | ||
|
|
||
| ```bash | ||
| CB_TASK_HREF=$(http :5001"$WF_HREF" | jq -r '.callbacks[0].dispatched_task') | ||
| http :5001"$CB_TASK_HREF" | jq '{name, state, error}' | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #!/bin/bash | ||
| # Demo callback script for `pulp_workflow`. Invoked by a `CallbackService` as a | ||
| # subprocess on the Pulp worker; reads workflow context from `PULP_WORKFLOW_*` | ||
| # env vars and `NOTIFY_WEBHOOK` from the worker's environment (set via | ||
| # oci_env/compose.env in the dev stack). | ||
| set -euo pipefail | ||
| : "${NOTIFY_WEBHOOK:?NOTIFY_WEBHOOK must be set in the worker environment}" | ||
| PAYLOAD=$(jq -nc \ | ||
| --arg name "${PULP_WORKFLOW_NAME:-?}" \ | ||
| --arg state "${PULP_WORKFLOW_STATE:-?}" \ | ||
| --arg cid "${CORRELATION_ID:-?}" \ | ||
| '{content: ("Workflow \($name) finished in state \($state).")}') | ||
| curl -fsS -H 'Content-Type: application/json' -d "$PAYLOAD" "$NOTIFY_WEBHOOK" >/dev/null |
51 changes: 51 additions & 0 deletions
51
pulp_workflow/app/migrations/0004_callbackservice_workflowcallback.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Generated by Django 5.2.13 on 2026-05-08 17:32 | ||
|
|
||
| import django.db.models.deletion | ||
| import django_lifecycle.mixins | ||
| import pulpcore.app.models.base | ||
| import pulpcore.app.util | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('core', '0150_taskschedule_task_kwargs'), | ||
| ('workflow', '0003_workflow_task_group'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='CallbackService', | ||
| fields=[ | ||
| ('pulp_id', models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False)), | ||
| ('pulp_created', models.DateTimeField(auto_now_add=True)), | ||
| ('pulp_last_updated', models.DateTimeField(auto_now=True, null=True)), | ||
| ('name', models.TextField()), | ||
| ('script', models.TextField()), | ||
| ('pulp_domain', models.ForeignKey(default=pulpcore.app.util.get_domain_pk, on_delete=django.db.models.deletion.CASCADE, to='core.domain')), | ||
| ], | ||
| options={ | ||
| 'permissions': [('manage_roles_callbackservice', 'Can manage role assignments on callback services')], | ||
| 'default_permissions': ('add', 'change', 'delete', 'view'), | ||
| 'unique_together': {('pulp_domain', 'name')}, | ||
| }, | ||
| bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model), | ||
| ), | ||
| migrations.CreateModel( | ||
| name='WorkflowCallback', | ||
| fields=[ | ||
| ('pulp_id', models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False)), | ||
| ('pulp_created', models.DateTimeField(auto_now_add=True)), | ||
| ('pulp_last_updated', models.DateTimeField(auto_now=True, null=True)), | ||
| ('callback_type', models.TextField(choices=[('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('canceled', 'Canceled'), ('finished', 'Finished')])), | ||
| ('callback_service', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='workflow_callbacks', to='workflow.callbackservice')), | ||
| ('dispatched_task', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='core.task')), | ||
| ('workflow', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='callbacks', to='workflow.workflow')), | ||
| ], | ||
| options={ | ||
| 'unique_together': {('workflow', 'callback_service', 'callback_type')}, | ||
| }, | ||
| bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model), | ||
| ), | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.