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
44 changes: 44 additions & 0 deletions lib/constructs/dify-services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,50 @@ export class ApiService extends Construct {
},
});

taskDefinition.addContainer('WorkerBeat', {
image: customRepository
? ecs.ContainerImage.fromEcrRepository(customRepository, `dify-api_${props.imageTag}`)
: ecs.ContainerImage.fromRegistry(`langgenius/dify-api:${props.imageTag}`),
environment: {
MODE: 'beat',
LOG_LEVEL: debug ? 'DEBUG' : 'ERROR',
DEBUG: debug ? 'true' : 'false',

// Enable pessimistic disconnect handling for recover from Aurora automatic pause
SQLALCHEMY_POOL_PRE_PING: 'True',

// The configurations of redis connection.
REDIS_HOST: redis.endpoint,
REDIS_PORT: redis.port.toString(),
REDIS_USE_SSL: 'true',
REDIS_DB: '0',

// The type of storage to use for storing user files.
STORAGE_TYPE: 's3',
S3_BUCKET_NAME: storageBucket.bucketName,
S3_REGION: Stack.of(storageBucket).region,
S3_USE_AWS_MANAGED_IAM: 'true',

DB_DATABASE: postgres.databaseName,

...getAdditionalEnvironmentVariables(this, 'worker_beat', props.additionalEnvironmentVariables),
},
logging: ecs.LogDriver.awsLogs({
streamPrefix: 'log',
}),
secrets: {
DB_USERNAME: ecs.Secret.fromSecretsManager(postgres.secret, 'username'),
DB_HOST: ecs.Secret.fromSecretsManager(postgres.secret, 'host'),
DB_PORT: ecs.Secret.fromSecretsManager(postgres.secret, 'port'),
DB_PASSWORD: ecs.Secret.fromSecretsManager(postgres.secret, 'password'),
REDIS_PASSWORD: ecs.Secret.fromSecretsManager(redis.secret),
CELERY_BROKER_URL: ecs.Secret.fromSsmParameter(redis.brokerUrl),
SECRET_KEY: ecs.Secret.fromSecretsManager(encryptionSecret),

...getAdditionalSecretVariables(this, 'worker_beat', props.additionalEnvironmentVariables),
},
});

const sandboxFileContainer = taskDefinition.addContainer('SandboxFileMount', {
image: ecs.ContainerImage.fromAsset(join(__dirname, 'docker', 'sandbox'), {
platform: Platform.LINUX_AMD64,
Expand Down
2 changes: 1 addition & 1 deletion lib/environment-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,4 @@ export interface EnvironmentProps {
setupEmail?: boolean;
}

export type DifyContainerTypes = 'web' | 'api' | 'worker' | 'sandbox';
export type DifyContainerTypes = 'web' | 'api' | 'worker' | 'worker_beat' | 'sandbox';
182 changes: 182 additions & 0 deletions test/__snapshots__/dify-on-aws-cf.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,170 @@ exports[`Snapshot test (with CloudFront) 2`] = `
},
],
},
{
"Environment": [
{
"Name": "MODE",
"Value": "beat",
},
{
"Name": "LOG_LEVEL",
"Value": "ERROR",
},
{
"Name": "DEBUG",
"Value": "false",
},
{
"Name": "SQLALCHEMY_POOL_PRE_PING",
"Value": "True",
},
{
"Name": "REDIS_HOST",
"Value": {
"Fn::GetAtt": [
"RedisFF642DF2",
"PrimaryEndPoint.Address",
],
},
},
{
"Name": "REDIS_PORT",
"Value": "6379",
},
{
"Name": "REDIS_USE_SSL",
"Value": "true",
},
{
"Name": "REDIS_DB",
"Value": "0",
},
{
"Name": "STORAGE_TYPE",
"Value": "s3",
},
{
"Name": "S3_BUCKET_NAME",
"Value": {
"Ref": "StorageBucket19DB2FF8",
},
},
{
"Name": "S3_REGION",
"Value": "us-west-2",
},
{
"Name": "S3_USE_AWS_MANAGED_IAM",
"Value": "true",
},
{
"Name": "DB_DATABASE",
"Value": "main",
},
],
"Essential": true,
"Image": "langgenius/dify-api:latest",
"LogConfiguration": {
"LogDriver": "awslogs",
"Options": {
"awslogs-group": {
"Ref": "ApiServiceTaskWorkerBeatLogGroup5578F6B1",
},
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "log",
},
},
"Name": "WorkerBeat",
"Secrets": [
{
"Name": "DB_USERNAME",
"ValueFrom": {
"Fn::Join": [
"",
[
{
"Ref": "PostgresClusterSecretAttachment8DDCF2A8",
},
":username::",
],
],
},
},
{
"Name": "DB_HOST",
"ValueFrom": {
"Fn::Join": [
"",
[
{
"Ref": "PostgresClusterSecretAttachment8DDCF2A8",
},
":host::",
],
],
},
},
{
"Name": "DB_PORT",
"ValueFrom": {
"Fn::Join": [
"",
[
{
"Ref": "PostgresClusterSecretAttachment8DDCF2A8",
},
":port::",
],
],
},
},
{
"Name": "DB_PASSWORD",
"ValueFrom": {
"Fn::Join": [
"",
[
{
"Ref": "PostgresClusterSecretAttachment8DDCF2A8",
},
":password::",
],
],
},
},
{
"Name": "REDIS_PASSWORD",
"ValueFrom": {
"Ref": "RedisAuthToken9E34F6A5",
},
},
{
"Name": "CELERY_BROKER_URL",
"ValueFrom": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition",
},
":ssm:us-west-2:123456789012:parameter/",
{
"Ref": "RedisBrokerUrlA8582E06",
},
],
],
},
},
{
"Name": "SECRET_KEY",
"ValueFrom": {
"Ref": "ApiServiceEncryptionSecretF73F9ECD",
},
},
],
},
{
"Essential": false,
"Image": {
Expand Down Expand Up @@ -2583,6 +2747,19 @@ exports[`Snapshot test (with CloudFront) 2`] = `
],
},
},
{
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents",
],
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"ApiServiceTaskWorkerBeatLogGroup5578F6B1",
"Arn",
],
},
},
{
"Action": [
"ecr:BatchCheckLayerAvailability",
Expand Down Expand Up @@ -2803,6 +2980,11 @@ exports[`Snapshot test (with CloudFront) 2`] = `
},
"Type": "AWS::IAM::Policy",
},
"ApiServiceTaskWorkerBeatLogGroup5578F6B1": {
"DeletionPolicy": "Retain",
"Type": "AWS::Logs::LogGroup",
"UpdateReplacePolicy": "Retain",
},
"ApiServiceTaskWorkerLogGroup48EED12F": {
"DeletionPolicy": "Retain",
"Type": "AWS::Logs::LogGroup",
Expand Down
Loading