Skip to content

Workflows tips and patterns

This page contains helpful patterns and tips for using Cloud Workflows with django-gcp.

Invoking Cloud Run Jobs from workflows

Tip

A powerful pattern is using workflows to invoke Cloud Run Jobs that run Django management commands. This is particularly useful for long-running or resource-intensive management commands that should not run in your web server.

If you have a Django app deployed as a Cloud Run Job (in addition to, or instead of, a Cloud Run Service), you can create workflows that invoke these jobs to run management commands.

An example workflow definition (workflows/run-management-command.yaml):

main:
  params: [args]
  steps:
    - init:
        assign:
          - project_id: 'my-project'
          - job_location: 'europe-west1'
          - job_name: ${"namespaces/" + project_id + "/jobs/django-job"}

    - run_django_command:
        call: googleapis.run.v1.namespaces.jobs.run
        args:
          name: ${job_name}
          location: ${job_location}
          body:
            overrides:
              containerOverrides:
                args:
                  # You may not need "python" if it's already defined as your container command
                  - 'python'
                  - 'manage.py'
                  - ${args.command}
                  - ${args.command_args}
        result: job_execution

    - finish:
        return: ${job_execution}

The Django workflow class:

from django_gcp.workflows import Workflow

class RunManagementCommandWorkflow(Workflow):
    workflow_name = "run-management-command"
    location = "europe-west1"

Invoking from Django:

# Run a management command via Cloud Run Job
execution = RunManagementCommandWorkflow().invoke(
    command="import_data",
    command_args="--source=production",
)

# Track the execution
print(f"Job started: {execution.id}")
print(f"Monitor: {RunManagementCommandWorkflow().get_console_url(execution.id)}")

Benefits of this pattern:

  • Run management commands without blocking web requests.
  • Handle commands that need more memory or CPU than web instances have.
  • Leverage workflow features like retries, error handling, and conditional logic.
  • Orchestrate multiple management commands in sequence.
  • Keep web servers optimised for HTTP traffic while jobs handle batch processing.

Example use cases include data imports and exports, database migrations on large datasets, report generation, batch email sending, cache warming, and cleanup operations.