Pause a Deluge code for 'X' seconds

12/11/2023 11:20 PM - Comment(s) - By Jesus Sosa

Sleep can be used in a Node JS function, such as below
  1. /**

    * author: vincent.balfe@cloud-connect.ie

    * @param seconds: literal - to be converted to ms

    * @return JSON IO - SUCCESS or FAIL

    */

    module.exports = async function( context, basicIO )

    {

     if (basicIO.getParameter("seconds"))

     {

     // Get 'seconds' param 

     var secs = (parseInt(basicIO.getParameter("seconds"))) * 1000;

     await sleep(secs)

     function sleep(ms) {

     return new Promise((resolve) => {

     setTimeout(resolve, ms);

     });

     }

     basicIO.write("SUCCESS"); 

     }

     else

     {

     basicIO.write("FAIL"); 

     }

    }

This can then be called multiple times ( no more than 5, and no longer than 1 minute sleep-time in total ) from a regular deluge function like so:
  1. info "Start: " + now;
  2. if( (thisapp.sleep_function("1")).contains("SUCCESS") )
    {
  3.         info "Step 1 ( 1 second ): " + now;
  4.         if( (thisapp.sleep_function("2")).contains("SUCCESS") )
  5.         {
  6.             info "Step 2 ( 2 seconds ): " + now;
  7.            if( (thisapp.sleep_function("3")).contains("SUCCESS"))
  8.            {
  9.                 info "Step 3 ( 3 seconds ): " + now;
  10.                 if( (thisapp.sleep_function("4")).contains("SUCCESS"))
  11.                 {
  12.                       info "Step 4 ( 4 seconds ): " + now;
  13.                       if( (thisapp.sleep_function("30")).contains("SUCCESS"))
  14.                       {
  15.                            info "Step 5 ( 30 seconds ): " + now;
  16.                       }
  17.                  }
  18.             }
  19.         }
  20. }
Result
  1. Start: 13-May-2022 09:42:50
  2. Step 1 ( 1 second ): 13-May-2022 09:42:52
  3. Step 2 ( 2 seconds ): 13-May-2022 09:42:55
  4. Step 3 ( 3 seconds ): 13-May-2022 09:42:59
  5. Step 4 ( 4 seconds ): 13-May-2022 09:43:03
  6. Step 5 ( 30 seconds ): 13-May-2022 09:43:33


Jesus Sosa

Share -