Forráskód Böngészése

try fix working more than once per job

Mike L 3 éve
szülő
commit
97bb2d96b6
2 módosított fájl, 28 hozzáadás és 25 törlés
  1. 14 12
      dist/twitter.js
  2. 14 13
      src/twitter.ts

+ 14 - 12
dist/twitter.js

@@ -361,27 +361,29 @@ class default_1 {
             });
             const queuedFeeds = lock.feed.slice(0, (lock.workon + 1) || undefined).reverse();
             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) * 250 / lock.feed.length;
+                const promiseDelay = this.workInterval * (Math.random() + j + 10 - arr.length) * 125 / lock.feed.length;
                 const wait = (ms) => isWaitingForLogin ? utils_1.neverResolves() : util_1.promisify(setTimeout)(ms);
                 const startTime = new Date().getTime();
                 const getTimerTime = () => new Date().getTime() - startTime;
-                if (Date.now() - new Date(lock.threads[currentFeed].updatedAt).getTime() < 3600000)
-                    return wait(promiseDelay);
                 const workon = (queuedFeeds.length - 1) - (i * 5 + j);
                 fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
-                const promise = util_1.promisify(setTimeout)(promiseDelay * 3).then(() => {
+                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 util_1.promisify(setTimeout)(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`);
-                    return 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));
+                    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 Promise.race([promise, wait(promiseDelay * 4)]);
             })))).then(this.work);
         };
         this.client = new instagram_private_api_1.IgApiClient();

+ 14 - 13
src/twitter.ts

@@ -582,28 +582,29 @@ export default class {
     const queuedFeeds = lock.feed.slice(0, (lock.workon + 1) || undefined).reverse();
     chainPromises(Arr.chunk(queuedFeeds, 5).map((arr, i) =>
       () => Promise.all(arr.map((currentFeed, j) => {
-        const promiseDelay = this.workInterval * (Math.random() + j) * 250 / lock.feed.length;
+        const promiseDelay = this.workInterval * (Math.random() + j + 10 - arr.length) * 125 / lock.feed.length;
         const wait = (ms: number) => isWaitingForLogin ? neverResolves() : promisify(setTimeout)(ms);
         const startTime = new Date().getTime();
         const getTimerTime = () => new Date().getTime() - startTime;
 
-        if (Date.now() - new Date(lock.threads[currentFeed].updatedAt).getTime() < 3600000) return wait(promiseDelay);
-
         const workon = (queuedFeeds.length - 1) - (i * 5 + j);
         fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
-        const promise = promisify(setTimeout)(promiseDelay * 3).then(() => {
+        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 promisify(setTimeout)(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`);
-          return 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));
+          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 Promise.race([promise, wait(promiseDelay * 4)]);
       }))
     )).then(this.work);
   };