浏览代码

disable per-thread working timeout

Mike L 3 年之前
父节点
当前提交
a714b62d65
共有 2 个文件被更改,包括 26 次插入30 次删除
  1. 13 15
      dist/twitter.js
  2. 13 15
      src/twitter.ts

+ 13 - 15
dist/twitter.js

@@ -418,31 +418,29 @@ class default_1 {
                 }
             });
             const queuedFeeds = lock.feed.slice(0, (lock.workon + 1) || undefined).reverse();
+            let logComplete = () => { };
             (0, utils_1.chainPromises)(utils_1.Arr.chunk(queuedFeeds, 5).map((arr, i) => () => Promise.all(arr.map((currentFeed, j) => {
                 const promiseDelay = this.workInterval * (Math.random() + j + 10 - arr.length) * 125 / lock.feed.length;
                 const wait = (ms) => isWaitingForLogin ? LoginAwaiter.awaiter : (0, util_1.promisify)(setTimeout)(ms);
                 const startTime = new Date().getTime();
                 const getTimerTime = () => new Date().getTime() - startTime;
                 const workon = (queuedFeeds.length - 1) - (i * 5 + j);
-                fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
+                if (j === arr.length - 1) {
+                    logComplete = () => logger.info(`batch job #${workon}-${workon + j} completed after ${getTimerTime()} ms`);
+                }
+                const next = () => {
+                    lock.workon = workon - 1;
+                    fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
+                };
                 if (Date.now() - new Date(lock.threads[currentFeed].updatedAt).getTime() < 3600000) {
                     logger.info(`skipped feed #${workon}: ${currentFeed}, last updated within an hour`);
-                    return wait(promiseDelay * 3);
+                    return wait(promiseDelay * 3).then(next);
                 }
-                return (0, util_1.promisify)(setTimeout)(promiseDelay * 3).then(() => {
+                return wait(promiseDelay * 3).then(() => {
                     logger.info(`about to pull from feed #${workon}: ${currentFeed}`);
-                    if (j === arr.length - 1)
-                        logger.info(`timeout for this batch job: ${Math.trunc(promiseDelay)} ms`);
-                    const promise = this.workOnFeed(currentFeed).then(() => {
-                        lock.workon = workon - 1;
-                        if (j === arr.length - 1) {
-                            logger.info(`batch job #${workon}-${workon + j} completed after ${getTimerTime()} ms`);
-                        }
-                        fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
-                    });
-                    return Promise.race([promise, wait(promiseDelay * 4)]);
-                });
-            })))).then(this.work);
+                    return this.workOnFeed(currentFeed);
+                }).then(next);
+            })).then(logComplete))).then(this.work);
         };
         this.client = new instagram_private_api_1.IgApiClient();
         if (opt.proxyUrl) {

+ 13 - 15
src/twitter.ts

@@ -690,32 +690,30 @@ export default class {
     });
 
     const queuedFeeds = lock.feed.slice(0, (lock.workon + 1) || undefined).reverse();
+    let logComplete = () => {};
     chainPromises(Arr.chunk(queuedFeeds, 5).map((arr, i) =>
       () => Promise.all(arr.map((currentFeed, j) => {
         const promiseDelay = this.workInterval * (Math.random() + j + 10 - arr.length) * 125 / lock.feed.length;
         const wait = (ms: number) => isWaitingForLogin ? LoginAwaiter.awaiter : promisify(setTimeout)(ms);
         const startTime = new Date().getTime();
         const getTimerTime = () => new Date().getTime() - startTime;
-
         const workon = (queuedFeeds.length - 1) - (i * 5 + j);
-        fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
+        if (j === arr.length - 1) {
+          logComplete = () => logger.info(`batch job #${workon}-${workon + j} completed after ${getTimerTime()} ms`);
+        }
+        const next = () => {
+          lock.workon = workon - 1;
+          fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
+        };
         if (Date.now() - new Date(lock.threads[currentFeed].updatedAt).getTime() < 3600000) {
           logger.info(`skipped feed #${workon}: ${currentFeed}, last updated within an hour`);
-          return wait(promiseDelay * 3);
+          return wait(promiseDelay * 3).then(next);
         }
-        return promisify(setTimeout)(promiseDelay * 3).then(() => {
+        return wait(promiseDelay * 3).then(() => {
           logger.info(`about to pull from feed #${workon}: ${currentFeed}`);
-          if (j === arr.length - 1) logger.info(`timeout for this batch job: ${Math.trunc(promiseDelay)} ms`);
-          const promise = this.workOnFeed(currentFeed).then(() => {
-            lock.workon = workon - 1;
-            if (j === arr.length - 1) {
-              logger.info(`batch job #${workon}-${workon + j} completed after ${getTimerTime()} ms`);
-            }
-            fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
-          });
-          return Promise.race([promise, wait(promiseDelay * 4)]);
-        });
-      }))
+          return this.workOnFeed(currentFeed);
+        }).then(next);
+      })).then(logComplete)
     )).then(this.work);
   };
 }