Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ updates:
update-types:
- version-update:semver-major

- package-ecosystem: docker
directory: /src/TacticusPlanner.Jobs
multi-ecosystem-group: dotnet-sdk
patterns:
- "*"
ignore:
- dependency-name: "*"
update-types:
- version-update:semver-major

# global.json: SDK feature-band and patch updates within .NET 10.
- package-ecosystem: dotnet-sdk
directory: /
Expand Down
7 changes: 6 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ docs-only:
- "LICENSE"

# Gate: the container image is built on a PR only when something that feeds it changed.
# Only files the API image is built from. Directory.Packages.props is left out on purpose: restore runs
# Only files the API and jobs images are built from. Directory.Packages.props is left out on purpose: restore runs
# in locked mode, so a version bump that reaches the image always changes a src/ lock file too, while a
# test-only bump (xunit, Verify, Testcontainers) touches just tests/ lock files and needs no image build.
area:docker:
Expand Down Expand Up @@ -66,6 +66,11 @@ area:api:
- any-glob-to-any-file:
- "src/TacticusPlanner.Api/**"

area:jobs:
- changed-files:
- any-glob-to-any-file:
- "src/TacticusPlanner.Jobs/**"

area:domain:
- changed-files:
- any-glob-to-any-file:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/_build-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
type: string
default: ""
build-image:
description: Build the container image. CI skips it when no file that feeds the image changed.
description: Build the container images (API and jobs). CI skips them when no file that feeds an image changed.
required: false
type: boolean
default: true
Expand Down Expand Up @@ -110,6 +110,10 @@ jobs:
if: ${{ inputs.build-image }}
run: docker build --file src/TacticusPlanner.Api/Dockerfile --tag tacticus-planner-api:${{ github.sha }} .

- name: Build jobs container image
if: ${{ inputs.build-image }}
run: docker build --file src/TacticusPlanner.Jobs/Dockerfile --tag tacticus-planner-jobs:${{ github.sha }} .

build:
name: Build API
needs: [format, test]
Expand Down
70 changes: 70 additions & 0 deletions .github/workflows/_deploy-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ on:
description: Immutable image reference (<acr>.azurecr.io/tacticus-planner-api@sha256:...).
required: true
type: string
jobs-image:
description: Immutable jobs image reference (<acr>.azurecr.io/tacticus-planner-jobs@sha256:...), set on the Container Apps Job once the API is healthy. Empty skips it.
required: false
type: string
default: ""
allow-missing-app:
description: Report success without deploying when the Container App does not exist yet (first staging bootstrap).
required: false
Expand Down Expand Up @@ -49,6 +54,20 @@ jobs:
fi
echo "reference=$REFERENCE" >> "$GITHUB_OUTPUT"

- name: Check jobs image reference
id: jobs-image
if: inputs.jobs-image != ''
shell: bash
env:
ACR_NAME: ${{ vars.ACR_NAME }}
REFERENCE: ${{ inputs.jobs-image }}
run: |
if [[ ! "$REFERENCE" =~ ^$ACR_NAME\.azurecr\.io/tacticus-planner-jobs@sha256:[0-9a-f]{64}$ ]]; then
echo "::error::$REFERENCE is not an immutable $ACR_NAME jobs image reference."
exit 1
fi
echo "reference=$REFERENCE" >> "$GITHUB_OUTPUT"

- name: Install Container Apps CLI extension
run: az extension add --name containerapp --version 1.3.0b5

Expand Down Expand Up @@ -160,6 +179,57 @@ jobs:
IMAGE_REFERENCE: ${{ steps.image.outputs.reference }}
run: echo "Deployed $IMAGE_REFERENCE" >> "$GITHUB_STEP_SUMMARY"

# The job exists only after a workload deploy with --jobs-image, and the deployment identity can
# read it only after `access post` (tacticus-planner-infra). Until both, the update is skipped
# with a warning; ARM answers a missing resource outside the identity's scope with
# AuthorizationFailed, not ResourceNotFound, so both mean "not set up yet".
- name: Update jobs image
if: steps.check.outputs.exists == 'true' && steps.jobs-image.outputs.reference != ''
shell: bash
env:
IMAGE_REFERENCE: ${{ steps.jobs-image.outputs.reference }}
CONTAINER_APP_JOB_NAME: ${{ vars.CONTAINER_APP_JOB_NAME }}
RESOURCE_GROUP_NAME: ${{ vars.RESOURCE_GROUP_NAME }}
run: |
if [[ -z "$CONTAINER_APP_JOB_NAME" ]]; then
echo "::warning::CONTAINER_APP_JOB_NAME is not set; run access pre in tacticus-planner-infra."
echo "Jobs image not deployed: CONTAINER_APP_JOB_NAME is not set." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi

if ! output="$(az containerapp job show \
--name "$CONTAINER_APP_JOB_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output none 2>&1)"; then
if grep -qE 'ResourceNotFound|AuthorizationFailed' <<< "$output"; then
echo "::warning::Container Apps Job $CONTAINER_APP_JOB_NAME is missing or not readable yet; deploy the workload with --jobs-image, then run access post."
echo "Jobs image not deployed: $CONTAINER_APP_JOB_NAME is missing or not readable yet." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
echo "$output"
echo "::error::Could not read Container Apps Job $CONTAINER_APP_JOB_NAME."
exit 1
fi

# Only the image changes; the next execution (az containerapp job start) runs it.
az containerapp job update \
--name "$CONTAINER_APP_JOB_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--container-name jobs \
--image "$IMAGE_REFERENCE" \
--output none

job_image="$(az containerapp job show \
--name "$CONTAINER_APP_JOB_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query 'properties.template.containers[0].image' \
--output tsv)"
if [[ "$job_image" != "$IMAGE_REFERENCE" ]]; then
echo "::error::Container Apps Job $CONTAINER_APP_JOB_NAME runs $job_image, not $IMAGE_REFERENCE."
exit 1
fi
echo "Deployed jobs image $IMAGE_REFERENCE" >> "$GITHUB_STEP_SUMMARY"

- name: Record skipped deployment
if: steps.check.outputs.exists == 'false'
shell: bash
Expand Down
34 changes: 23 additions & 11 deletions .github/workflows/_publish-api.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
name: Publish API image
name: Publish image

# Builds the API image for a commit and pushes it to the shared registry, tagged with the commit
# SHA. A commit that already has an image (for example a release cut from main, which CD Stage
# built) is not rebuilt, so production runs the same digest staging ran.
# Builds an image (the API, or the jobs container) for a commit and pushes it to the shared
# registry, tagged with the commit SHA. A commit that already has an image (for example a release
# cut from main, which CD Stage built) is not rebuilt, so production runs the same digest staging ran.
on:
workflow_call:
inputs:
ref:
description: Git ref to build.
required: true
type: string
repository:
description: Registry repository (image name).
required: false
type: string
default: tacticus-planner-api
dockerfile:
description: Dockerfile that builds the image.
required: false
type: string
default: src/TacticusPlanner.Api/Dockerfile
outputs:
image:
description: Immutable image reference (<acr>.azurecr.io/tacticus-planner-api@sha256:...).
description: Immutable image reference (<acr>.azurecr.io/<repository>@sha256:...).
value: ${{ jobs.publish.outputs.image }}

permissions:
Expand Down Expand Up @@ -48,6 +58,7 @@ jobs:
shell: bash
env:
ACR_NAME: ${{ vars.ACR_NAME }}
REPOSITORY: ${{ inputs.repository }}
run: |
sha="$(git rev-parse HEAD)"
echo "sha=$sha" >> "$GITHUB_OUTPUT"
Expand All @@ -56,7 +67,7 @@ jobs:
errors="$RUNNER_TEMP/acr-lookup.err"
if digest="$(az acr repository show \
--name "$ACR_NAME" \
--image "tacticus-planner-api:$sha" \
--image "$REPOSITORY:$sha" \
--query digest \
--output tsv 2> "$errors")"; then
:
Expand All @@ -82,19 +93,20 @@ jobs:
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
file: src/TacticusPlanner.Api/Dockerfile
file: ${{ inputs.dockerfile }}
push: true
tags: ${{ vars.ACR_NAME }}.azurecr.io/tacticus-planner-api:${{ steps.source.outputs.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ vars.ACR_NAME }}.azurecr.io/${{ inputs.repository }}:${{ steps.source.outputs.sha }}
# One cache per image, so the two images never evict each other's layers.
cache-from: type=gha,scope=${{ inputs.repository }}
cache-to: type=gha,mode=max,scope=${{ inputs.repository }}

- name: Set immutable image reference
id: image
shell: bash
env:
DIGEST: ${{ steps.source.outputs.digest || steps.build.outputs.digest }}
run: |
reference="${{ vars.ACR_NAME }}.azurecr.io/tacticus-planner-api@$DIGEST"
reference="${{ vars.ACR_NAME }}.azurecr.io/${{ inputs.repository }}@$DIGEST"
echo "reference=$reference" >> "$GITHUB_OUTPUT"
echo "### Published image" >> "$GITHUB_STEP_SUMMARY"
echo "\`$reference\`" >> "$GITHUB_STEP_SUMMARY"
12 changes: 11 additions & 1 deletion .github/workflows/cd-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ jobs:
with:
ref: refs/tags/${{ inputs.ref }}

publish-jobs:
name: Publish jobs
needs: check-release
uses: ./.github/workflows/_publish-api.yml
with:
ref: refs/tags/${{ inputs.ref }}
repository: tacticus-planner-jobs
dockerfile: src/TacticusPlanner.Jobs/Dockerfile

deploy:
name: Deploy Production
needs: publish
needs: [publish, publish-jobs]
uses: ./.github/workflows/_deploy-api.yml
with:
environment: production
image: ${{ needs.publish.outputs.image }}
jobs-image: ${{ needs.publish-jobs.outputs.image }}
15 changes: 14 additions & 1 deletion .github/workflows/cd-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,27 @@ jobs:
with:
ref: ${{ github.sha }}

publish-jobs:
name: Publish jobs
if: github.ref == 'refs/heads/main'
permissions:
contents: read
id-token: write
uses: ./.github/workflows/_publish-api.yml
with:
ref: ${{ github.sha }}
repository: tacticus-planner-jobs
dockerfile: src/TacticusPlanner.Jobs/Dockerfile

deploy:
name: Deploy Stage
needs: publish
needs: [publish, publish-jobs]
permissions:
contents: read
id-token: write
uses: ./.github/workflows/_deploy-api.yml
with:
environment: stage
image: ${{ needs.publish.outputs.image }}
jobs-image: ${{ needs.publish-jobs.outputs.image }}
allow-missing-app: true
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ and .NET Aspire for local orchestration.
re-run. The test hosts set `BackgroundJobs:ServerEnabled=false` (a host
setting via `UseSetting`, since `Program` reads it while registering
services); test jobs by calling them directly. See README "Background jobs".
- `src/TacticusPlanner.Jobs`: the batch jobs console host, its own image
(`tacticus-planner-jobs`) run by a Manual Container Apps Job. One keyed
`IBatchJob` per job name (`JobRegistration.AddBatchJobs`); it refuses to run
with pending migrations and has no column-encryption key. Not for work an
endpoint enqueues (that is Hangfire). See README "Batch jobs container".
- `tests/TacticusPlanner.Api.Tests`: Docker-free pure tests and database-independent catalog HTTP tests.
- `tests/TacticusPlanner.Api.IntegrationTests`: database-backed endpoint tests with production pooled Npgsql, migrations, and external-boundary fakes. Shares a disposable PostgreSQL container; each factory owns an isolated database. Requires Docker.
- `tests/TacticusPlanner.GameCatalog.Tests`: catalog manifest/denormalization tests.
Expand Down Expand Up @@ -147,4 +152,5 @@ dotnet format TacticusPlanner.slnx --verify-no-changes --no-restore
dotnet build TacticusPlanner.slnx -c Release --no-restore
dotnet test TacticusPlanner.slnx -c Release --no-build
docker build -f src/TacticusPlanner.Api/Dockerfile -t tacticus-planner-api:local .
docker build -f src/TacticusPlanner.Jobs/Dockerfile -t tacticus-planner-jobs:local .
```
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,40 @@ Jobs take ids, never entities or secrets (Hangfire stores arguments as plain JSO
twice: a deploy stops in-flight jobs and Hangfire runs them again. A failed job's exception is in the logs;
`hangfire.job` and `hangfire.state` hold its history.

### Batch jobs container

Work too long or too heavy for a request runs in a separate image, `tacticus-planner-jobs`, built from
`src/TacticusPlanner.Jobs`. It is a console host, not a web server: it runs one batch job, named by its first
argument, and exits. In Azure it is the Container Apps Job `caj-tp-jobs-<env>` (tacticus-planner-infra) on a
Manual trigger, so nothing runs, and nothing is billed, until someone starts an execution:

```bash
az containerapp job start --name caj-tp-jobs-stg --resource-group rg-tp-stg
```

The job's arguments pick what runs (`smoke` as deployed). To run another job, change them first with
`az containerapp job update --name caj-tp-jobs-stg --resource-group rg-tp-stg --container-name jobs --args <job-name>`;
a later workload deploy resets them to `smoke`. Don't override the template on `job start` itself: an override
replaces the container definition, dropping the Key Vault-backed environment variables.

| Job | What it does |
| --- | --- |
| `smoke` | Connects to the database and logs the applied migration and profile counts. Proves the image, the connection string and the private network path work. |

The runner refuses to run while the database has pending migrations (the API applies them at startup, so a
job never runs against an older schema), and its exit code is the execution's result: `0` succeeded, `1` the
job threw, `2` unknown job name, `3` migrations pending. It uses its own connection string
(`planner-db-jobs-connection-string`, pool of 2) and has no column-encryption key, so a job that needs an
encrypted column fails fast instead of reading ciphertext.

To add a batch job, implement `IBatchJob` and register it under its name in `JobRegistration.AddBatchJobs`.
Run it locally against the Aspire database with
`dotnet run --project src/TacticusPlanner.Jobs -- <job-name>` and `ConnectionStrings__planner-db` set.

CD Stage and CD Prod publish the jobs image beside the API image and point the Container Apps Job at it once
the API revision is healthy. Until the infra has created the job and granted the deployment identity access
to it, that step is skipped with a warning.

### Feature flags

Unfinished work merged to `main` stays hidden behind a feature flag
Expand Down Expand Up @@ -416,6 +450,12 @@ docker build -f src/TacticusPlanner.Api/Dockerfile -t tacticus-planner-api:local
The image runs as a non-root user and listens on port `8080`. Aspire is only a
local development dependency and is not included in the runtime image.

The batch jobs image builds the same way and runs the `smoke` job by default:

```powershell
docker build -f src/TacticusPlanner.Jobs/Dockerfile -t tacticus-planner-jobs:local .
```

## Validation

```powershell
Expand All @@ -425,6 +465,7 @@ dotnet format TacticusPlanner.slnx --verify-no-changes --no-restore
dotnet build TacticusPlanner.slnx -c Release --no-restore
dotnet test TacticusPlanner.slnx -c Release --no-build
docker build -f src/TacticusPlanner.Api/Dockerfile -t tacticus-planner-api:local .
docker build -f src/TacticusPlanner.Jobs/Dockerfile -t tacticus-planner-jobs:local .
```

The test projects have separate responsibilities:
Expand Down
2 changes: 2 additions & 0 deletions TacticusPlanner.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Project Path="src/TacticusPlanner.Domain/TacticusPlanner.Domain.csproj" />
<Project Path="src/TacticusPlanner.GameCatalog/TacticusPlanner.GameCatalog.csproj" />
<Project Path="src/TacticusPlanner.GameDomain/TacticusPlanner.GameDomain.csproj" />
<Project Path="src/TacticusPlanner.Jobs/TacticusPlanner.Jobs.csproj" />
<Project Path="src/TacticusPlanner.Persistence/TacticusPlanner.Persistence.csproj" />
<Project Path="src/TacticusPlanner.TacticusApi/TacticusPlanner.TacticusApi.csproj" />
</Folder>
Expand All @@ -16,6 +17,7 @@
<Project Path="tests/TacticusPlanner.Api.IntegrationTests/TacticusPlanner.Api.IntegrationTests.csproj" />
<Project Path="tests/TacticusPlanner.Api.Tests/TacticusPlanner.Api.Tests.csproj" />
<Project Path="tests/TacticusPlanner.GameCatalog.Tests/TacticusPlanner.GameCatalog.Tests.csproj" />
<Project Path="tests/TacticusPlanner.Jobs.Tests/TacticusPlanner.Jobs.Tests.csproj" />
<Project Path="tests/TacticusPlanner.Persistence.IntegrationTests/TacticusPlanner.Persistence.IntegrationTests.csproj" />
</Folder>
</Solution>
2 changes: 2 additions & 0 deletions openspec/changes/add-background-jobs/proposal.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ No companion apps change: the response contract is unchanged.
- Hangfire runs inside the API process with PostgreSQL storage in a `hangfire` schema of its own database, `tacticus_planner_hangfire`, on the planner server (`ConnectionStrings:hangfire-db`, falling back to the planner database when unset, as in the test hosts). Hangfire creates and upgrades the schema itself. One server per replica runs the `default` queue with 2 workers and its own Npgsql pool of 2. No dashboard, no recurring jobs.
- `BackgroundJobs:ServerEnabled` (default `true`) turns the server off; the test hosts and OpenAPI generation run without it.
- When the inline Entra deletion fails for a token with a valid `oid` and a configured Graph credential, the purge enqueues `ExternalIdentityDeletionJob`, which Hangfire retries 8 times with a growing delay and then leaves failed. The response still reports `X-Sign-In-Account-Deleted: false`.
- A separate batch jobs image, `tacticus-planner-jobs` (`src/TacticusPlanner.Jobs`), runs one named job per execution of a Manual Container Apps Job and exits. The only job is `smoke`, which proves the image, connection string and network path. CD publishes it beside the API image and updates the job's image after the API is healthy. It changes no API behavior, so no spec delta.

## Capabilities

Expand All @@ -19,5 +20,6 @@ No companion apps change: the response contract is unchanged.
## Impact

- `src/TacticusPlanner.BackgroundJobs` (new project: Hangfire setup and options), `src/TacticusPlanner.Api` `Features/AccountManagement`, `Program.cs`, `Directory.Packages.props` (Hangfire.Core, Hangfire.NetCore, Hangfire.PostgreSql; Newtonsoft.Json pinned to 13.0.4).
- `src/TacticusPlanner.Jobs` and `tests/TacticusPlanner.Jobs.Tests` (new), `.github/workflows` (publish, deploy, build), Dependabot.
- Database: a new `tacticus_planner_hangfire` database (created by the infra and by Aspire) holding a `hangfire` schema managed by Hangfire, not by EF migrations.
- Infra: tacticus-planner-infra caps prod at 2 replicas so (11 API + 2 Hangfire) x 2 + 2 jobs = 28 of the 35 usable connections. It must be applied before this deploys.
Loading
Loading