变更内容: - 在 `Dockerfile` 和 `docker-compose.yml` 中添加 Worker 模式支持,包含运行模式 `RUN_MODE` 的配置。 - 更新 API 路由,改为将任务入队处理,并由 Worker 执行。 - 在 JobManager 中新增任务队列及分布式锁功能,支持任务的入队、出队、执行控制以及重试机制。 - 添加全局并发控制逻辑,避免任务超额运行。 - 扩展单元测试,覆盖任务队列、锁机制和并发控制的各类场景。 - 在 Serverless 配置中分别为 API 和 Worker 添加独立服务定义。 提升任务调度灵活性,增强系统可靠性与扩展性。
73 lines
2.2 KiB
YAML
73 lines
2.2 KiB
YAML
# 阿里云函数计算配置
|
||
ROSTemplateFormatVersion: '2015-09-01'
|
||
Transform: 'Aliyun::Serverless-2018-04-03'
|
||
Resources:
|
||
functional-scaffold:
|
||
Type: 'Aliyun::Serverless::Service'
|
||
Properties:
|
||
Description: '算法工程化 Serverless 脚手架'
|
||
LogConfig:
|
||
Project: functional-scaffold-logs
|
||
Logstore: function-logs
|
||
VpcConfig:
|
||
VpcId: 'vpc-xxxxx'
|
||
VSwitchIds:
|
||
- 'vsw-xxxxx'
|
||
SecurityGroupId: 'sg-xxxxx'
|
||
prime-checker:
|
||
Type: 'Aliyun::Serverless::Function'
|
||
Properties:
|
||
Description: '质数判断算法服务(API)'
|
||
Runtime: custom-container
|
||
MemorySize: 512
|
||
Timeout: 60
|
||
InstanceConcurrency: 10
|
||
CAPort: 8000
|
||
CustomContainerConfig:
|
||
Image: 'registry.cn-hangzhou.aliyuncs.com/your-namespace/functional-scaffold:latest'
|
||
Command: '["/app/entrypoint.sh"]'
|
||
EnvironmentVariables:
|
||
APP_ENV: production
|
||
LOG_LEVEL: INFO
|
||
METRICS_ENABLED: 'true'
|
||
RUN_MODE: api
|
||
REDIS_HOST: 'r-xxxxx.redis.rds.aliyuncs.com'
|
||
REDIS_PORT: '6379'
|
||
Events:
|
||
httpTrigger:
|
||
Type: HTTP
|
||
Properties:
|
||
AuthType: ANONYMOUS
|
||
Methods:
|
||
- GET
|
||
- POST
|
||
job-worker:
|
||
Type: 'Aliyun::Serverless::Function'
|
||
Properties:
|
||
Description: '异步任务 Worker'
|
||
Runtime: custom-container
|
||
MemorySize: 512
|
||
Timeout: 900
|
||
InstanceConcurrency: 1
|
||
CustomContainerConfig:
|
||
Image: 'registry.cn-hangzhou.aliyuncs.com/your-namespace/functional-scaffold:latest'
|
||
Command: '["/app/entrypoint.sh"]'
|
||
EnvironmentVariables:
|
||
APP_ENV: production
|
||
LOG_LEVEL: INFO
|
||
METRICS_ENABLED: 'true'
|
||
RUN_MODE: worker
|
||
REDIS_HOST: 'r-xxxxx.redis.rds.aliyuncs.com'
|
||
REDIS_PORT: '6379'
|
||
WORKER_POLL_INTERVAL: '1.0'
|
||
MAX_CONCURRENT_JOBS: '5'
|
||
JOB_MAX_RETRIES: '3'
|
||
JOB_EXECUTION_TIMEOUT: '300'
|
||
Events:
|
||
timerTrigger:
|
||
Type: Timer
|
||
Properties:
|
||
CronExpression: '0 */1 * * * *'
|
||
Enable: true
|
||
Payload: '{}'
|