Skip to content

Commit

Permalink
feat: add updateAllDistribution function with chunk size
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidetaka Okamoto committed Jun 29, 2021
1 parent 93809a5 commit d5c1920
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ After configure the client, just run the method.

```typescript
await client.updateAllDistribution()

// If you want to update distribution per 10 items.
await client.updateAllDistribution(10)
```

### Upadte Specific Distribution
Expand Down
14 changes: 9 additions & 5 deletions libs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'tslib'
import { CloudFront } from 'aws-sdk'
import sequentialPromise from '@hideokamoto/sequential-promise'
import { sequentialPromiseWithChunk } from '@hideokamoto/sequential-promise'
import { detailedDiff } from 'deep-object-diff'

import DistributionSummaryList = CloudFront.DistributionSummaryList
Expand All @@ -13,6 +13,7 @@ const sleep = async (time = 1000) => {
}

export namespace interfaces {
export type LoggerFunction = (...str: string[]) => void;
// To Write any method to the new CloudFront distribution config
export type UpdatorFunction = (dist: {id: string; arn: string}, distributionConfig: DistributionConfig) => DistributionConfig | null | Promise<DistributionConfig | null>

Expand All @@ -31,7 +32,7 @@ export namespace interfaces {
// for constructor, to replace worker clients
export interface Clients {
cfClient: CloudFront;
logger: Function;
logger: LoggerFunction
}

// for constructor, to update task config
Expand All @@ -45,6 +46,7 @@ export namespace interfaces {

import UpdatorFunction = interfaces.UpdatorFunction
import FilterCondition = interfaces.FilterCondition
import LoggerFunction = interfaces.LoggerFunction
import TaskType = interfaces.TaskType
import Workers = interfaces.Workers
import Clients = interfaces.Clients
Expand All @@ -61,7 +63,7 @@ const defaultConfig: Config = {
}
export class CloudFrontUpdator {
// Logger
protected log: Function
protected log: LoggerFunction

// CloudFront API Client (AWS-SDK)
protected cfClient: CloudFront
Expand Down Expand Up @@ -225,17 +227,19 @@ export class CloudFrontUpdator {
/**
* Update all distribution
*/
public async updateAllDistribution (): Promise<void> {
public async updateAllDistribution (chunkSize = 1): Promise<void> {
const distributions = await this.listAllDistributions()
this.log(`Distirbution Count: ${distributions.length}`)
let targetNumber = 0
const filteredFlag = true
if (this.taskType === 'sequential') {
await sequentialPromise(distributions, async (distribution) => {
await sequentialPromiseWithChunk(distributions, async (distribution) => {
if (!await this.isTargetDistribution(distribution)) return
targetNumber += 1
this.log(`No. ${targetNumber}: ${distribution.Id}`)
await this.updateDistribution(distribution, filteredFlag)
}, {
chunkSize: chunkSize
})
} else if (this.taskType === 'parallel') {
await Promise.all(distributions.map(async (distribution) => {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@
"lint-staged": "10.5.1",
"rollup": "2.33.2",
"rollup-plugin-typescript2": "0.29.0",
"semantic-release": "17.4.3",
"ts-jest": "26.4.4",
"tslib": "2.0.3",
"typedoc": "0.19.2",
"semantic-release": "17.4.3",
"typescript": "3.9.7"
},
"dependencies": {
"@hideokamoto/sequential-promise": "^1.0.1",
"aws-sdk": "^2.925.0",
"@hideokamoto/sequential-promise": "^1.1.0",
"aws-sdk": "^2.936.0",
"deep-object-diff": "^1.1.0"
}
}
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,10 @@
minimatch "^3.0.4"
strip-json-comments "^3.1.1"

"@hideokamoto/sequential-promise@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@hideokamoto/sequential-promise/-/sequential-promise-1.0.1.tgz#a1793bfcfbcd8a08ecdbfee874b64dcb7c2342fa"
integrity sha512-ovFAaRTI7/nURQcBaoyTE6x6Wo+I1cegWN9mW0LHnDAghzeSTZGI0lJKgK71fB1E3Cs9ltYUUY8VVMdCYVMR5Q==
"@hideokamoto/sequential-promise@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@hideokamoto/sequential-promise/-/sequential-promise-1.1.0.tgz#b4959d8ea077e0d56096c0d8feec7ead5c9604fa"
integrity sha512-Xo3jadBltQR+/IT06nQh5WxqeCuQyCFprS7yOVfKhz6iSfJSHzDCjv+cLOQTXNIalD3DnenRN5apr39jGUU+Jg==

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
Expand Down Expand Up @@ -1520,10 +1520,10 @@ atob@^2.1.2:
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==

aws-sdk@^2.925.0:
version "2.925.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.925.0.tgz#a352f3adc8274120fb31fa4fc04527d44b6107cf"
integrity sha512-dHXngzuSTZvIFWizWbU+ceZTAKmgodzEIi/AWbB+z0THKfxD3Iz/CJ7kZ7a9QyZv7X082L68vv3siMhXBMoTLA==
aws-sdk@^2.936.0:
version "2.936.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.936.0.tgz#b69f5db7c0c745f260b014415a8fbfb38c5e615d"
integrity sha512-X0kuyycck0fEPN5V0Vw1PmPIQ4BO0qupsL1El5jnXzXxNkf1cOmn5PMSxPXPsdcqua4w4h3sf143/yME0V9w8g==
dependencies:
buffer "4.9.2"
events "1.1.1"
Expand Down

0 comments on commit d5c1920

Please sign in to comment.