From 79c0e3ddcbb9739157dea47f8a6b5d472b5ba412 Mon Sep 17 00:00:00 2001 From: PChol22 Date: Mon, 10 Jul 2023 23:21:10 +0200 Subject: [PATCH] queue articles publication to meet forem rate limiting --- package.json | 3 ++- src/dev-to-git.ts | 12 ++++++++++-- yarn.lock | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ae7075d..7febfac 100644 --- a/package.json +++ b/package.json @@ -129,6 +129,7 @@ "commander": "^2.20.0", "dotenv": "8.0.0", "front-matter": "3.0.2", - "got": "9.6.0" + "got": "9.6.0", + "throttled-queue": "^2.1.4" } } diff --git a/src/dev-to-git.ts b/src/dev-to-git.ts index e7b176e..3151854 100644 --- a/src/dev-to-git.ts +++ b/src/dev-to-git.ts @@ -2,6 +2,7 @@ import chalk from 'chalk'; import program from 'commander'; import dotenv from 'dotenv'; import fs from 'fs'; +import throttledQueue from 'throttled-queue'; import { Article } from './article'; import { ArticleConfig, @@ -16,9 +17,11 @@ import { Logger, formatArticlePublishedStatuses, logBuilder } from './helpers'; export const DEFAULT_CONFIG_PATH: string = './dev-to-git.json'; const repositoryRe: RegExp = /.*\/(.*)\/(.*)\.git/; +const ARTICLE_UPLOAD_INTERVAL = 3000; export class DevToGit { private configuration: ConfigurationOptions; + private queue: ReturnType; public logger: Logger; @@ -47,6 +50,8 @@ export class DevToGit { this.logger(chalk.red('DEV_TO_GIT_TOKEN environment variable, or --dev-to-token argument is required')); process.exit(1); } + + this.queue = throttledQueue(1, ARTICLE_UPLOAD_INTERVAL); } private parseRepository(repo: string | null): Repository | null { @@ -107,14 +112,17 @@ export class DevToGit { public async publishArticles(): Promise { const articles = this.readConfigFile(); - const articlePublishedStatuses = []; + const articlePublishedStatuses: ArticlePublishedStatus[] = []; // instead of using Promise.all we use a for with await // to run the updates one by one to avoid hammering dev.to API // and have more risks of being rate limited for (const articleConf of articles) { const article = new Article(articleConf, this.configuration.devToToken); - articlePublishedStatuses.push(await article.publishArticle()); + + await this.queue(async () => { + articlePublishedStatuses.push(await article.publishArticle()); + }); } return articlePublishedStatuses; diff --git a/yarn.lock b/yarn.lock index 0e5e9d8..2ac7e3c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7684,6 +7684,11 @@ throat@^4.0.0: resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a" integrity sha1-iQN8vJLFarGJJua6TLsgDhVnKmo= +throttled-queue@^2.1.4: + version "2.1.4" + resolved "https://registry.yarnpkg.com/throttled-queue/-/throttled-queue-2.1.4.tgz#4e2008c73ab3f72ba1bb09496c3cc9c5b745dbee" + integrity sha512-YGdk8sdmr4ge3g+doFj/7RLF5kLM+Mi7DEciu9PHxnMJZMeVuZeTj31g4VE7ekUffx/IdbvrtOCiz62afg0mkg== + through2@^2.0.0, through2@^2.0.2, through2@~2.0.0: version "2.0.5" resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"