隨需執行的任務:一個 Aggregate,四個目標
一個帶有 core/manual-trigger 的任務,在 Kubernetes、Docker Compose、Terraform 與 CloudFormation 上部署但不啟動,旁邊是一個什麼都不帶的任務,並附上在每個目標上啟動一次執行的命令。
有些工作只在有人要求時才執行到完成:一次性的資料修正、依需求進行的備份。core/manual-trigger 就是為此而設的 Feature。附加到沒有排程的 core/compute/task 時,它會部署任務但不啟動它,而每一次執行都是由某人以目標自己的命令手動啟動。
這個工作區為四個目標建置一個 Aggregate——透過 Helm 的 Kubernetes、Docker Compose、Terraform 與 CloudFormation——並在每個目標上展示附加項目對渲染輸出造成的改變,以及啟動一次執行的命令。第二個任務沒有附加項目,因此差異在同一次建置中清楚可見。另有兩個 Releases 預期會中止,頁面會引用它們各自印出的內容。
它建置什麼
| 實例 | 目錄 Component | 附加項目 | 什麼啟動一次執行 |
|---|---|---|---|
data-fix | core/compute/task | core/manual-trigger | 在每個目標上,都由某人手動啟動 |
report | core/compute/task | 無 | 它的排程,或在 Docker Compose 上,堆疊啟動時 |
附加項目位在 Aggregate 中,緊鄰它所改變的任務:依需求執行是 data-fix 本身的一部分,因此由開發者撰寫,而且這個 Feature 不接受任何值,也不接受 infra。Release 不為它寫任何東西。
report 的排程是一個參數 report_schedule,Aggregate 以空的預設值宣告它,並把它讀入任務的 schedule 值:
spec: parameters: report_schedule: type: string default: "" [… omitted: the parameter's description]
components: [… omitted: data-fix with its attachment, and the comment above each task] report: type: core/compute/task values: schedule: "{{ .parameters.report_schedule }}"它是參數,是因為四個目標對於沒有附加項目的任務看法不一。Kubernetes 以 CronJob 依排程執行它,沒有排程時則在部署時執行一次。Docker Compose 沒有排程器,因此每次堆疊啟動時執行任務一次,並拒絕排程。在 AWS 上,Terraform 或 CloudFormation 套用時沒有任何東西會執行 Amazon ECS 任務,因此那裡的任務需要排程或附加項目,兩者皆無就會被拒絕。所以 Kubernetes、Terraform 與 CloudFormation 的 Releases 會填入 report_schedule,而 Docker Compose 的 Release 則讓它保持為空。
data-fix 在四個目標上以相同方式建置,沒有排程,也沒有參數。
六個 Releases
| Release | Manifest | report_schedule | 建置 |
|---|---|---|---|
release-helm.yaml | manifest-helm.yaml | 30 2 * * 1-5 | 完成 |
release-compose.yaml | manifest-compose.yaml | 空 | 完成 |
release-terraform.yaml | manifest-terraform.yaml | 30 2 * * 1-5 | 完成 |
release-cloudformation.yaml | manifest-cloudformation.yaml | 30 2 * * 1-5 | 完成 |
release-compose-scheduled.yaml | manifest-compose.yaml | 30 2 * * 1-5 | 中止 |
release-terraform-unscheduled.yaml | manifest-terraform.yaml | 空 | 中止 |
每個 Manifest 都把 core/compute/task 對應到其目標的 Recipe,並把 core/manual-trigger 繫結到其目標的 FeatureRecipe:helm/task 與 helm/manual-trigger、compose/task 與 compose/manual-trigger、terraform/task-ecs 與 terraform/manual-trigger,以及 cloudformation/task-ecs 與 cloudformation/manual-trigger。沒有為附加項目繫結 FeatureRecipe 的 Manifest,會讓建置中止並指名它。
每個 Release 都在 infra.image 下指名每個任務執行的映像檔,因為 core/compute/task 不封裝任何軟體。AWS 的 Releases 也在 infra.overrides 下指名每個任務執行所用的叢集、子網路、安全群組與執行角色,並且在 report 有排程的地方,指名排程啟動每次執行所用的角色。那裡每個帳戶專屬的值都是預留位置。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example task-run-on-demand它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd task-run-on-demand建置
這個工作區從 .aggrega-config.yaml 所指名的 hub 登錄庫讀取標準程式庫目錄。這裡的每個 Recipe 都只從目錄渲染,因此鎖定檔解析完成之後,每次建置都可離線執行。
aggrega build --release release-helm.yaml --auto-pull # one Release在文件存放庫的檢出內容中,它持有由 aggrega draft --example 寫出的副本所不帶的共用建置輔助程式,./check.sh 會建置全部六個 Releases 並檢查每個結果。它每個 Release 印出一行,且只有在每個都如此頁所述地結束時,才以 0 結束:
RELEASE EXIT EXPECTED VERDICT----------------------------------------------------------------------------------------------------release-helm.yaml 0 complete OK: completed, the files the README showsrelease-compose.yaml 0 complete OK: completed, the files the README showsrelease-terraform.yaml 0 complete OK: completed, the files the README showsrelease-cloudformation.yaml 0 complete OK: completed, the files the README showsrelease-compose-scheduled.yaml 1 halt OK: halted with the expected refusalrelease-terraform-unscheduled.yaml 1 halt OK: halted with the expected refusal
check: every build ended as expected.搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,且不要自行建立任何資料夾。如果工作區資料夾中還沒有 `task-run-on-demand` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `task-run-on-demand`,且不指定 `dir`:它會把範例寫入新的 `task-run-on-demand` 資料夾。接著呼叫 `build` 工具,將 `workspace_root` 設為 `task-run-on-demand`,將 `release` 設為 `release-helm.yaml`,將 `auto_pull` 設為 true,並將 `write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `task-run-on-demand/dist/` 底下。Kubernetes
release-helm.yaml 寫出兩個檔案,每個任務一個 CronJob,沒有其他東西。data-fix 是一個保持暫停的 CronJob,使用的排程是開發者從未寫過的:
apiVersion: batch/v1kind: CronJobmetadata: name: data-fix [… omitted: the labels and annotations the build writes on every resource]spec: schedule: 0 0 30 2 * suspend: true jobTemplate: spec: template: [… omitted: the pod template's metadata, the same labels again] spec: containers: - name: main image: registry.example.com/orders-data-fix:2.0.1 [… omitted: the container's CPU and memory requests and limits] restartPolicy: NeverCronJob 需要排程,而 0 0 30 2 * 指的是 2 月 30 日,這是任何一年都沒有的日期:暫停時,CronJob 不會啟動任何執行,恢復之後,它仍然沒有可以啟動一次執行的時間。沒有附加項目時,沒有排程的任務會是 Job,部署後就立即執行。
report 是依 Release 所填入的排程執行的 CronJob,並未暫停:
[… omitted: apiVersion, kind and metadata, as in data-fix-cronjob.yaml under report's name]spec: schedule: 30 2 * * 1-5 jobTemplate: [… omitted: the job template, as in data-fix-cronjob.yaml with report's image]data-fix 的一次執行是從它的 CronJob 建立的 Job,名稱由你自行選擇:
kubectl create job data-fix-run-1 --from=cronjob/data-fix同一個命令,若給予 --from=cronjob/report,就會在 report 的排程執行之間額外啟動一次執行。
Docker Compose
release-compose.yaml 寫出一個 Compose 檔案。每個任務都是一個服務,在結束時 Compose 不會再次啟動它;data-fix 還帶有設定檔 manual,而 report 則沒有:
services: data-fix: image: registry.example.com/orders-data-fix:2.0.1 [… omitted: the labels the build writes on every service, and the service's network] restart: "no" [… omitted: the CPU and memory limits and reservations] profiles: - manual report: image: registry.example.com/orders-report:1.4.2 [… omitted: the labels and the network, as on data-fix] restart: "no" [… omitted: the CPU and memory limits and reservations, as on data-fix][… omitted: the network the services join]Compose 只有在啟用該設定檔,或服務在命令列上被指名時,才會啟動帶有設定檔的服務,因此 docker compose up 會執行 report 一次,並讓 data-fix 保持停止。data-fix 的一次執行是藉由指名它的服務來啟動,而 --rm 會在執行結束後移除它的容器:
docker compose -f dist/compose/docker-compose.yaml run --rm data-fixTerraform
release-terraform.yaml 寫出一個根設定,每個任務有一個模組區塊,每個任務的模組就在它旁邊。兩個區塊的差別在排程:
# Root configuration for this boundary. Each component rendered here is one# module block below, and the directory that block points at holds that# component's own files. One configuration means one dependency graph, which is# what lets a reference or a depends_on reach from one component to another.
module "data-fix" { source = "./modules/data-fix"
assign_public_ip = false cluster = "arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster" cpu = "256" environment = {} execution_role_arn = "arn:aws:iam::123456789012:role/example-ecs-task-execution" image = "registry.example.com/orders-data-fix:2.0.1" memory = "512" name = "data-fix" security_groups = ["sg-0fedcba9876543210"] subnet_ids = ["subnet-0123456789abcdef0"]}
module "report" { source = "./modules/report"
assign_public_ip = false cluster = "arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster" cpu = "256" environment = {} execution_role_arn = "arn:aws:iam::123456789012:role/example-ecs-task-execution" image = "registry.example.com/orders-report:1.4.2" memory = "512" name = "report" schedule_expression = "cron(30 2 ? * 2,3,4,5,6 *)" schedule_expression_timezone = "UTC" scheduler_role_arn = "arn:aws:iam::123456789012:role/example-scheduler" security_groups = ["sg-0fedcba9876543210"] subnet_ids = ["subnet-0123456789abcdef0"]}模組只有在被給予運算式時,才會宣告 EventBridge Scheduler 排程,因此 data-fix 的任務定義是獨立存在的:
[… omitted: the provider requirement, the Region lookup, the task definition and the comments on each]resource "aws_scheduler_schedule" "this" { count = var.schedule_expression == null ? 0 : 1[… omitted: the rest of the schedule]Recipe 以 EventBridge Scheduler 自己的形式寫出 report 的排程:30 2 * * 1-5 變成 cron(30 2 ? * 2,3,4,5,6 *),星期幾從星期日起算為 1,並使用 UTC,因為 Release 沒有指名時區。
CloudFormation
release-cloudformation.yaml 寫出一個堆疊範本。它包含每個任務的任務定義,以及只給 report 的排程:
{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "DataFixTaskDefinition": { [… omitted: the provenance metadata the build writes on every resource] "Properties": { "ContainerDefinitions": [ { "Essential": true, "Image": "registry.example.com/orders-data-fix:2.0.1", "Name": "main" } ], "Cpu": "256", "ExecutionRoleArn": "arn:aws:iam::123456789012:role/example-ecs-task-execution", "Family": "data-fix", "Memory": "512", "NetworkMode": "awsvpc", "RequiresCompatibilities": [ "FARGATE" ] }, "Type": "AWS::ECS::TaskDefinition" }, "ReportSchedule": { [… omitted: the provenance metadata] "Properties": { "FlexibleTimeWindow": { "Mode": "OFF" }, "ScheduleExpression": "cron(30 2 ? * 2,3,4,5,6 *)", "ScheduleExpressionTimezone": "UTC", "Target": { "Arn": "arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster", "EcsParameters": { [… omitted: the launch type, the network configuration and the task count each run starts with] "TaskDefinitionArn": { "Ref": "ReportTaskDefinition" } }, "RoleArn": "arn:aws:iam::123456789012:role/example-scheduler" } }, "Type": "AWS::Scheduler::Schedule" }, "ReportTaskDefinition": { [… omitted: the same shape as DataFixTaskDefinition, with report's image and family] } }}堆疊中沒有任何東西參照 DataFixTaskDefinition,因此沒有任何東西會啟動它。
在 Amazon ECS 上啟動一次執行
在兩個 AWS 目標上,data-fix 的一次執行都是以 Amazon ECS RunTask 操作啟動,給予渲染出的任務定義系列,以及 Release 在 infra.overrides 下所指名的叢集與網路設定:
aws ecs run-task \ --cluster arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster \ --task-definition data-fix \ --launch-type FARGATE \ --network-configuration 'awsvpcConfiguration={subnets=[subnet-0123456789abcdef0],securityGroups=[sg-0fedcba9876543210],assignPublicIp=DISABLED}'僅以系列名稱指名時,任務定義會解析為它最新的作用中修訂版本。
建置不做的事
建置寫出檔案後就停止。它在任何目標上都不啟動任何執行:它不連到任何叢集、不啟動任何 Compose 堆疊,也不呼叫任何 AWS API,而附加項目只改變檔案所寫的內容。它也不寫任何指令碼:本頁的每個命令都是目標自己的命令,由運作它的人執行,而且沒有任何一個在 dist/ 中。附加項目的實現在任何目標上都不新增自己的檔案;各自造成的改變就在任務自己的輸出中,如上所示。
會中止的兩個 Releases
release-compose-scheduled.yaml 在 Docker Compose 上填入 report_schedule,並且也把 core/manual-trigger 附加到 report。在 Kubernetes 與 AWS 上,有排程的任務無論有沒有附加項目,都會依排程執行;在沒有排程器的 Docker Compose 上,有排程的任務無論附加與否,都沒有實現。aggrega build --release release-compose-scheduled.yaml --auto-pull 會中止:
[… omitted: what resolving the lockfile prints, the same halt among it][CATALOG_ERROR E0033] Recipe/FeatureRecipe assertions caused by: Recipe "task" instance "report" assertion "schedule_refused": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: the component "report" is scheduled "30 2 * * 1-5", and Docker Compose has no scheduler: nothing in a compose file starts a service at a time, so the run would happen at none of the times written. Map core/compute/task to helm/task in the Manifest, which runs it on its schedule as a Kubernetes CronJob; or remove the schedule from the component in the Aggregate to run it once at each docker compose up, or attach core/manual-trigger to it there to start each run with docker compose run --rmrelease-terraform-unscheduled.yaml 在 Terraform 上讓 report_schedule 保持為空,因此 report 既沒有排程也沒有附加項目,而 AWS 上沒有任何東西會啟動它。這正是 core/manual-trigger 存在的情況,而 aggrega build --release release-terraform-unscheduled.yaml --auto-pull 會中止並指名它:
[… omitted: what resolving the lockfile prints, the same halt among it][CATALOG_ERROR E0033] Recipe/FeatureRecipe assertions caused by: Recipe "task-ecs" instance "report" assertion "schedule_translates": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: the component "report" has no schedule and carries no core/manual-trigger attachment, so it runs once, and nothing on AWS starts a run that happens once: neither Terraform nor CloudFormation runs an Amazon ECS task when it applies, so the run would never happen. Give the component a schedule in the Aggregate; attach core/manual-trigger to it there to deploy its task definition alone, for the operator to start each run with aws ecs run-task; or map core/compute/task to helm/task in the Manifest, which runs it once on Kubernetes在這裡,排程是透過 Release 填入的參數抵達任務,因此要給元件排程,就是要填入 report_schedule,就像 release-terraform.yaml 所做的那樣。