Преглед изворни кода

Merge pull request #12858 from overleaf/jpa-server-ce-script-change-compile-timeout

[web] server-ce-script: add script for changing compile timeout for user

GitOrigin-RevId: 3718b970661e1ede04402889c240ecdabdcc7eff
Jakob Ackermann пре 3 година
родитељ
комит
6ef9be0d0c

+ 41 - 0
services/web/modules/server-ce-scripts/scripts/change-compile-timeout.js

@@ -0,0 +1,41 @@
+const minimist = require('minimist')
+const { db, ObjectId, waitForDb } = require('../../../app/src/infrastructure/mongodb')
+
+async function main() {
+  await waitForDb()
+
+  const argv = minimist(process.argv.slice(2), {
+    string: ['user-id', 'compile-timeout'],
+  })
+
+  const { 'user-id': userId, 'compile-timeout': rawCompileTimeout } = argv
+  const compileTimeout = parseInt(rawCompileTimeout, 10)
+  if (!userId || !ObjectId.isValid(userId) || !rawCompileTimeout || Number.isNaN(compileTimeout)) {
+    console.error(
+      `Usage: node ${__filename} --user-id=5a9414f259776c7900b300e6 --timeout=90`
+    )
+    process.exit(101)
+  }
+
+  if (compileTimeout < 1 || compileTimeout > 600) {
+    console.error(
+      `The compile timeout must be positive number of seconds, below 10 minutes (600).`
+    )
+    process.exit(101)
+  }
+
+  await db.users.updateOne(
+    { _id: ObjectId(userId) },
+    { $set: { 'features.compileTimeout': compileTimeout } }
+  )
+}
+
+main()
+  .then(() => {
+    console.error('Done.')
+    process.exit(0)
+  })
+  .catch(err => {
+    console.error(err)
+    process.exit(1)
+  })

+ 71 - 1
services/web/modules/server-ce-scripts/test/acceptance/src/ServerCEScriptsTests.js

@@ -14,7 +14,7 @@ function run(cmd) {
   // https://nodejs.org/docs/latest-v12.x/api/child_process.html#child_process_options_stdio
   // Pipe stdin from /dev/null, store stdout, pipe stderr to /dev/null.
   return execSync(cmd, {
-    stdio: ['ignore', 'pipe', 'ignore'],
+    stdio: ['ignore', 'pipe', 'pipe'],
   }).toString()
 }
 
@@ -160,4 +160,74 @@ describe('ServerCEScripts', function () {
       expect(await getTagNames()).to.deep.equal([newName])
     })
   })
+
+  describe('change-compile-timeout', function () {
+    let userA, userB
+    beforeEach('login', async function () {
+      userA = new User()
+      await userA.login()
+
+      userB = new User()
+      await userB.login()
+    })
+
+    async function getCompileTimeout(user) {
+      const { compileTimeout } = await user.getFeatures()
+      return compileTimeout
+    }
+
+    let userATimeout, userBTimeout
+    beforeEach('fetch current state', async function () {
+      userATimeout = await getCompileTimeout(userA)
+      userBTimeout = await getCompileTimeout(userB)
+    })
+
+    describe('happy path', function () {
+      let newUserATimeout
+      beforeEach('run script on user a', function () {
+        newUserATimeout = userATimeout - 1
+        run(
+          `node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=${newUserATimeout}`
+        )
+      })
+
+      it('should change the timeout for user a', async function () {
+        const actual = await getCompileTimeout(userA)
+        expect(actual).to.not.equal(userATimeout)
+        expect(actual).to.equal(newUserATimeout)
+      })
+
+      it('should leave the timeout for user b as is', async function () {
+        expect(await getCompileTimeout(userB)).to.equal(userBTimeout)
+      })
+    })
+
+    describe('bad options', function () {
+      it('should reject zero timeout', async function () {
+        try {
+          run(
+            `node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=0`
+          )
+          expect.fail('should error out')
+        } catch (err) {
+          expect(err.stderr.toString()).to.include('positive number of seconds')
+        }
+        expect(await getCompileTimeout(userA)).to.equal(userATimeout)
+        expect(await getCompileTimeout(userB)).to.equal(userBTimeout)
+      })
+
+      it('should reject a 20min timeout', async function () {
+        try {
+          run(
+            `node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=1200`
+          )
+          expect.fail('should error out')
+        } catch (err) {
+          expect(err.stderr.toString()).to.include('below 10 minutes')
+        }
+        expect(await getCompileTimeout(userA)).to.equal(userATimeout)
+        expect(await getCompileTimeout(userB)).to.equal(userBTimeout)
+      })
+    })
+  })
 })