Skip to content

Commit

Permalink
Merge pull request #35 from codeforjapan/feat/add-scheduled-task
Browse files Browse the repository at this point in the history
Feat/add scheduled task
  • Loading branch information
ayuki-joto authored Oct 25, 2023
2 parents 9c1464b + 88c3e74 commit 2ef4c45
Show file tree
Hide file tree
Showing 9 changed files with 1,423 additions and 772 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev_synth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
uses: youyo/aws-cdk-github-actions@v2
with:
cdk_subcommand: 'synth'
cdk_version: '2.54.0'
cdk_version: '2.102.0'
working_dir: 'cdk'
cdk_args: '--context stage=dev'
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
branches:
- main

push:
branches:
- main
Expand All @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '16' ]
node: [ '18' ]

steps:
- uses: actions/checkout@v2
Expand Down
15 changes: 9 additions & 6 deletions docs/build_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ AWS Systems Manager のパラメータストアで以下のようなパラメー
[Docker イメージをプッシュする](https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/docker-push-ecr-image.html)を参考に
buildしたdocker imageを用意したリポジトリにpushする。

# 4-3 [dev.json](../config/dev.json) を編集する
[dev.json](../config/dev.json)の’repository’部分に用意したECRリポジトリ名、’tag’部分にpushした際のtagに書き換える。
# 4-3 tagをexportする
4-2でpushしたdockerイメージのタグをexportする
```console
$ export IMAGE_TAG=先ほどpushしたimage tag
```

# 5. 証明書の準備

Expand All @@ -71,21 +74,21 @@ buildしたdocker imageを用意したリポジトリにpushする。
使用するリージョンごとに一回実行する必要がある。2回目以降は不要。

```console
$ npx cdk --context stage=dev --profile decidim bootstrap
$ npx cdk --context stage=dev tag=${IMAGE_TAG} --profile decidim bootstrap
```

## 6-2. デプロイ前の差分確認

どんなリソースが作成されるのかを確認できる。

```console
$ npx cdk --context stage=dev --profile decidim diff
$ npx cdk --context stage=dev tag=${IMAGE_TAG} --profile decidim diff
```

## 6-3. デプロイ実行

```console
$ npx cdk --context stage=dev --profile decidim deploy --all --require-approval never
$ npx cdk --context stage=dev tag=${IMAGE_TAG} --profile decidim deploy --all --require-approval never
```

上記コマンドが成功すれば、デプロイは成功です。
Expand Down Expand Up @@ -124,4 +127,4 @@ cloudfrontの管理画面に行き作成したcloudfrontの管理画面で代替

### seedの実行について
decidimではproduction環境のseedは以下のenvをつけて実行する必要があります
SEED=true bundle exec rake db:seed
SEED=true bundle exec rake db:seed
60 changes: 60 additions & 0 deletions lib/decidim-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { DockerImageAsset, Platform } from 'aws-cdk-lib/aws-ecr-assets';
import { DockerImageName, ECRDeployment } from 'cdk-ecr-deployment';
import { capacityProviderStrategy } from "../lib/config";
import path = require('path');
import { EcsTask } from "aws-cdk-lib/aws-events-targets";
import { Rule, Schedule } from 'aws-cdk-lib/aws-events';

export interface DecidimStackProps extends BaseStackProps {
vpc: aws_ec2.IVpc
Expand Down Expand Up @@ -363,5 +365,63 @@ export class DecidimStack extends cdk.Stack {
value: `${ props.stage }-${ props.serviceName }-alb-origin.${ props.domain }`,
exportName: `${ props.stage }${ props.serviceName }accessDomain`,
});

const eventTasks: {
id: string,
command: string[],
scheduleExpression: string
}[] = [
{
id: 'removeDownloadDataFiles',
command: ['bundle','exec', 'rake', 'decidim:delete_download_your_data_files'],
scheduleExpression: 'cron(0 0 * * ? *)'
},
{
id: 'ComputeMetrics',
command: ['bundle','exec', 'rake', 'decidim:metrics:all'],
scheduleExpression: 'cron(10 0 * * ? *)'
},
{
id: 'ComputeOpenData',
command: ['bundle','exec', 'rake', 'decidim:open_data:export'],
scheduleExpression: 'cron(20 0 * * ? *)'
},
{
id: 'DeleteOldRegistrationsForms',
command: ['bundle','exec', 'rake', 'decidim_meetings:clean_registration_forms'],
scheduleExpression: 'cron(30 0 * * ? *)'
},
{
id: 'GenerateReminders',
command: ['bundle','exec', 'rake', 'decidim:reminders:all'],
scheduleExpression: 'cron(40 0 * * ? *)'
},
{
id: 'MailDigestDaily',
command: ['bundle','exec', 'rake', 'decidim:mailers:notifications_digest_daily'],
scheduleExpression: 'cron(0 18 * * ? *)'
},
{
id: 'MailDigestWeekly',
command: ['bundle','exec', 'rake', 'decidim:mailers:notifications_digest_weekly'],
scheduleExpression: 'cron(0 19 ? * 6 *)'
}
]

eventTasks.map(task => {
new Rule(this, task.id, {
schedule: Schedule.expression(task.scheduleExpression),
targets: [new EcsTask({
cluster: cluster,
taskDefinition: taskDefinition,
containerOverrides: [
{
containerName: 'appContainer',
command: task.command
}
]
})]
})
})
}
}
Loading

0 comments on commit 2ef4c45

Please sign in to comment.