تخطَّ إلى المحتوى
AggregaAggregaAggregaAggrega
اختر لغة: العربية
تُرجمت هذه الصفحة آليًا. اقرأ الأصل الإنجليزي.

مهمة تُشغَّل عند الطلب: Aggregate واحد وأربعة أهداف

مهمة تحمل core/manual-trigger، تُنشر دون أن تبدأ على Kubernetes و Docker Compose و Terraform و CloudFormation، إلى جانب مهمة لا تحمل شيئًا، مع الأمر الذي يبدأ تشغيلة على كل هدف.

بعض الأعمال لا تُشغَّل حتى الاكتمال إلا عندما يطلبها أحد: إصلاح بيانات لمرة واحدة، ونسخة احتياطية عند الطلب. و Feature المخصَّص لذلك هو core/manual-trigger. وعند إرفاقه بمكوِّن core/compute/task دون جدول زمني، ينشر المهمة دون أن يبدأها، وكل تشغيلة هي تشغيلة يبدؤها أحد يدويًا بأمر الهدف نفسه.

تبني مساحة العمل هذه Aggregate واحدًا لأربعة أهداف — Kubernetes عبر Helm و Docker Compose و Terraform و CloudFormation — وتُظهر على كل منها ما يغيِّره الإرفاق في الإخراج المُصيَّر والأمر الذي يبدأ تشغيلة. ولا تحمل المهمة الثانية إرفاقًا، فيظهر الفرق في البناء نفسه. ويُتوقع أن يتوقف اثنان آخران من Releases، وتقتبس الصفحة ما يطبعه كل منهما.

ما يبنيه

مثيلComponent الكتالوجالإرفاقما يبدأ تشغيلة
data-fixcore/compute/taskcore/manual-triggerشخص ما، يدويًا، على كل هدف
reportcore/compute/taskلا شيءجدولها الزمني، أو، على Docker Compose، بدء تشغيل المكدس

يوجد الإرفاق في Aggregate إلى جانب المهمة التي يغيِّرها: التشغيل عند الطلب جزء مما هو data-fix، لذا يكتبه المطوِّر، ولا يأخذ Feature قيمًا ولا infra. ولا يكتب Release شيئًا له.

الجدول الزمني للمهمة report مُعامِل اسمه report_schedule، يعلنه Aggregate بقيمة افتراضية فارغة ويقرؤه في قيمة schedule الخاصة بالمهمة:

aggregate.yaml
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 لا شيء يشغِّل مهمة Amazon ECS عند تطبيق Terraform أو CloudFormation، فتحتاج المهمة هناك إلى جدول زمني أو إلى الإرفاق، وتُرفض إن لم يكن لها أي منهما. لذا تفي Releases Kubernetes و Terraform و CloudFormation بالمُعامِل report_schedule، و Release Docker Compose يتركه فارغًا.

يُبنى data-fix بالطريقة نفسها على الأهداف الأربعة، دون جدول زمني ودون مُعامِل.

Releases الستة

ReleaseManifestreport_scheduleالبناء
release-helm.yamlmanifest-helm.yaml30 2 * * 1-5يكتمل
release-compose.yamlmanifest-compose.yamlفارغيكتمل
release-terraform.yamlmanifest-terraform.yaml30 2 * * 1-5يكتمل
release-cloudformation.yamlmanifest-cloudformation.yaml30 2 * * 1-5يكتمل
release-compose-scheduled.yamlmanifest-compose.yaml30 2 * * 1-5يتوقف
release-terraform-unscheduled.yamlmanifest-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. و Manifest الذي لا يربط أي FeatureRecipe للإرفاق يوقف البناء ويسمّيه.

يسمّي كل Release الصورة التي تشغِّلها كل مهمة تحت infra.image، لأن core/compute/task لا يحزم أي برمجيات. وتسمّي Releases AWS أيضًا، تحت infra.overrides، العنقود والشبكات الفرعية ومجموعات الأمان ودور التنفيذ الذي تعمل به كل مهمة، وحيث يكون للمهمة report جدول زمني، الدور الذي يبدأ الجدول الزمني كل تشغيلة تحته. وكل قيمة خاصة بالحساب هناك قيمة نائبة.

صُغ مسودة المثال

يمكنك بناء هذا المثال بالأوامر أدناه، أو دع مساعد الذكاء الاصطناعي يبنيه لك.

اكتب المثال في مجلد جديد يحمل اسمه:

Terminal window
aggrega draft --example task-run-on-demand

ثم يطبع الأوامر التي تشغّلها بعد ذلك. الأول ينتقل إلى المجلد الجديد:

Terminal window
cd task-run-on-demand

البناء

تقرأ مساحة العمل كتالوج المكتبة المعيارية من registry hub الذي يسميه .aggrega-config.yaml. ويُصيَّر كل Recipe هنا من الكتالوج وحده، فيعمل كل بناء دون اتصال بمجرد حل ملف القفل.

Terminal window
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 shows
release-compose.yaml 0 complete OK: completed, the files the README shows
release-terraform.yaml 0 complete OK: completed, the files the README shows
release-cloudformation.yaml 0 complete OK: completed, the files the README shows
release-compose-scheduled.yaml 1 halt OK: halted with the expected refusal
release-terraform-unscheduled.yaml 1 halt OK: halted with the expected refusal
check: every build ended as expected.

مع مساعد الذكاء الاصطناعي

يمكن لمساعد ذكاء اصطناعي مُعَد بأدوات Aggrega أن يفعل الشيء نفسه: الصق هذا الموجِّه فيه.

استخدم أدوات 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` و `write` مضبوطين على true. أخبرني بالملفات التي كتبها البناء؛ وهي تحت `task-run-on-demand/dist/`.

Kubernetes

يكتب release-helm.yaml ملفين، CronJob لكل مهمة، ولا شيء غير ذلك. و data-fix هو CronJob مُعلَّق، على جدول زمني لم يكتبه المطوِّر قط:

dist/helm/data-fix-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
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: Never

يحتاج CronJob إلى جدول زمني، و 0 0 30 2 * يسمّي 30 فبراير، وهو تاريخ لا تحتويه أي سنة: عندما يكون معلَّقًا لا يبدأ CronJob أي تشغيلة، وعندما يُستأنف لا يملك وقتًا يبدأ فيه واحدة. ومن دون الإرفاق، تكون المهمة بلا جدول زمني Job، وهو يعمل فور نشره.

report هو CronJob على الجدول الزمني الذي أوفى به Release، وهو غير معلَّق:

dist/helm/report-cronjob.yaml
[… 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 هي Job يُنشأ من CronJob الخاص بها، باسم من اختيارك:

Terminal window
kubectl create job data-fix-run-1 --from=cronjob/data-fix

الأمر نفسه، إذا أُعطي --from=cronjob/report، يبدأ تشغيلة إضافية للمهمة report بين تشغيلاتها المجدولة.

Docker Compose

يكتب release-compose.yaml ملف Compose واحدًا. وكل مهمة خدمة لا يبدأها Compose من جديد حين تنتهي؛ وتحمل data-fix أيضًا profile باسم manual، ولا تحمل report أي profile:

dist/compose/docker-compose.yaml
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 خدمة تحمل profile فقط عندما يكون ذلك profile مفعَّلًا أو يُذكر اسم الخدمة في سطر الأوامر، لذا يشغِّل docker compose up المهمة report مرة واحدة ويترك data-fix متوقفة. وتبدأ تشغيلة data-fix بذكر اسم خدمتها، و --rm يزيل حاويتها بمجرد انتهاء التشغيلة:

Terminal window
docker compose -f dist/compose/docker-compose.yaml run --rm data-fix

Terraform

يكتب release-terraform.yaml تكوينًا جذريًا فيه كتلة module واحدة لكل مهمة، ومعها وحدة كل مهمة بجانبها. وتختلف الكتلتان في الجدول الزمني:

dist/terraform/aggrega.modules.tf
# 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 قائمًا بذاته:

dist/terraform/modules/data-fix/main.tf
[… 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 الجدول الزمني للمهمة report بصيغة EventBridge Scheduler الخاصة: يتحول 30 2 * * 1-5 إلى cron(30 2 ? * 2,3,4,5,6 *)، وتُرقَّم أيام الأسبوع من الأحد بوصفه 1، بتوقيت UTC لأن Release لا يسمّي منطقة زمنية.

CloudFormation

يكتب release-cloudformation.yaml قالب مكدس واحدًا. ويحتوي تعريف مهمة لكل مهمة وجدولًا زمنيًا للمهمة report وحدها:

dist/cloudformation/cloudformation-stack.json
{
"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 بعملية RunTask في Amazon ECS، مع إعطائها family تعريف المهمة كما صُيِّر وتكوين العنقود والشبكة اللذين يسميهما Release تحت infra.overrides:

Terminal window
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}'

عندما يُحدَّد تعريف المهمة باسم family الخاصة به وحدها، يُحل إلى أحدث مراجعة نشطة له.

ما لا يفعله البناء

يكتب البناء الملفات ثم يتوقف. ولا يبدأ أي تشغيلة على أي هدف: فلا يصل إلى أي عنقود، ولا يبدأ أي مكدس Compose، ولا يستدعي أي API في AWS، ولا يغيِّر الإرفاق إلا ما تقوله الملفات. ولا يكتب نصًا برمجيًا أيضًا: كل أمر في هذه الصفحة هو أمر الهدف نفسه، يشغِّله من يشغِّل الهدف، ولا شيء منها في dist/. ولا تضيف تحقيقات الإرفاق أي ملف خاص بها على أي هدف؛ والتغيير الذي يُحدثه كل منها يظهر في إخراج المهمة نفسه، المعروض أعلاه.

اثنان من Releases يتوقفان

يفي release-compose-scheduled.yaml بالمُعامِل report_schedule على Docker Compose، ويرفق 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 --rm

يترك release-terraform-unscheduled.yaml المُعامِل report_schedule فارغًا على Terraform، فلا يكون للمهمة 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.