From f443d991091eac7579370b7a3cfee9709e04009e Mon Sep 17 00:00:00 2001 From: re-fort Date: Fri, 12 Jan 2018 01:50:30 +0900 Subject: [PATCH] :bulb: Use job queue (#14) * :up: Add dependencies * :bulb: Use queue --- index.js | 44 ++++++++++++++++++++++++++++++-------------- package.json | 1 + yarn.lock | 8 +++++++- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 7534d29..5d3fd20 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const RssFeedEmitter = require('rss-feed-emitter') +const Queue = require('queue') const Github = require('./lib/github') const Repo = require('./lib/repository') // const Slack = require('./lib/slack') @@ -7,6 +8,7 @@ const Utility = require('./lib/utility') let upstreamFeeder = new RssFeedEmitter() let headFeeder = new RssFeedEmitter() let github = new Github() +let q = Queue({ autostart: true, concurrency: 1 }) // let slack = new Slack({ token: process.env.SLACK_TOKEN }) let startUpTime = new Date().toISOString() @@ -64,7 +66,7 @@ const setupHeadFeeder = () => { const shortHash = hash.substr(0, 8) if (repo.existsRemoteBranch(shortHash)) { - Utility.log('W', `Remote branch already exists: ${shortHash}`) + Utility.log('W', `${item.title}: Remote branch already exists`) return } @@ -74,23 +76,37 @@ const setupHeadFeeder = () => { let body = `本家のドキュメントに更新がありました:page_facing_up:\r\nOriginal:${item.link}` const { data: newIssue } = await github.createIssue(remote, { title: `[Doc]: ${Utility.removeHash(item.title)}`, body, labels: ['documentation'] }) issueNo = newIssue.number - Utility.log('S', `Issue created: ${newIssue.html_url}`) + Utility.log('S', `${item.title}: Issue created: ${newIssue.html_url}`) } else { issueNo = result.items[0].number } - repo.fetchAllRemotes() - repo.updateDefaultBranch() - repo.createNewBranch(shortHash) - - if (repo.hasConflicts('cherry-pick', hash)) { - Utility.log('W', 'Conflicts occurred. Please make a pull request by yourself') - repo.resetChanges() - } else { - Utility.log('S', `Fully merged: ${shortHash}`) - repo.updateRemote(shortHash) - await after(item, shortHash, issueNo) - } + q.push(() => { + return new Promise(async (resolve, reject) => { + try { + q.stop() + + repo.fetchAllRemotes() + repo.updateDefaultBranch() + repo.createNewBranch(shortHash) + + if (repo.hasConflicts('cherry-pick', hash)) { + Utility.log('W', `${item.title}: Conflicts occurred. Please make a pull request by yourself`) + repo.resetChanges() + } else { + Utility.log('S', `${item.title}: Fully merged`) + repo.updateRemote(shortHash) + await after(item, shortHash, issueNo) + } + + resolve() + } catch(e) { + reject() + } finally { + q.start() + } + }) + }) }) } diff --git a/package.json b/package.json index 9d10a09..ea26e69 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "colors": "^1.1.2", "env-cmd": "^6.0.0", "github": "^11.0.0", + "queue": "^4.4.2", "request-promise": "^4.2.1", "rss-feed-emitter": "^1.0.7", "shelljs": "^0.7.8" diff --git a/yarn.lock b/yarn.lock index 5aa1ed1..5e3daa3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -635,7 +635,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@2, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3: +inherits@2, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1, inherits@~2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" @@ -995,6 +995,12 @@ qs@~6.4.0: version "6.4.0" resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233" +queue@^4.4.2: + version "4.4.2" + resolved "https://registry.yarnpkg.com/queue/-/queue-4.4.2.tgz#5a9733d9a8b8bd1b36e934bc9c55ab89b28e29c7" + dependencies: + inherits "~2.0.0" + readable-stream@^2.2.2: version "2.3.3" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"