1.0.19 • Published 2 years ago

nv-remote-retry v1.0.19

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

nv-remote-retry

  • retry task using redis

install

  • npm install nv-remote-retry

usage

  const RRetry = require("nv-remote-retry")
  var cfg = {
      password:'redis',
      db :15,
      port:6379,
      host:'127.0.0.1',
      family:4
  }
  var retry = new RRetry(cfg)
  await retry.init()

example

    var handle = async (lefted_times,a,b,c)=> {
           console.log(lefted_times, new Date())
           console.log(a,b,c)
           let rand = Math.random();
           if(rand>0.8) {
                 console.log("success: ",rand)
                 return([true,rand])
           } else {
                console.log("fail: ",rand)
                return([false,rand])
           }
    }        
    
    
    var arg_ary = [
         new Map([[100,200]]),
         new Set([300,400]),
        {a:500,b:[600,700]}
    ]


    var [id,rid] = await retry.add_retry_task(handle,arg_ary,5,6000);                 
    //retry max 5 times  for every  6 second
    //  will RETURN a id-pair       

succ

    > retry.list_all5 2022-11-05T07:57:12.126Z
    Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
    fail:  0.4219821578177563
    > 4 2022-11-05T07:57:18.161Z
    Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
    fail:  0.10755268101657967

    > 3 2022-11-05T07:57:24.201Z
    Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
    success:  0.9279835941851622

fail

        > var [id,rid]  = await retry.add_retry_task(handle,arg_ary,5,6000);
        > 5 2022-11-05T07:58:19.844Z
        Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
        fail:  0.19502970946958698

        > 4 2022-11-05T07:58:25.885Z
        Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
        fail:  0.6033240882938884

        > 3 2022-11-05T07:58:31.925Z
        Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
        fail:  0.238829682926065

        > 2 2022-11-05T07:58:37.965Z
        Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
        fail:  0.6222257875394948

        > 1 2022-11-05T07:58:44.003Z
        Map(1) { 100 => 200 } Set(2) { 300, 400 } { a: 500, b: [ 600, 700 ] }
        fail:  0.5639765495604092

        >

list all pending task, this is slow, only for TEST

                var arg_ary = [
                     new Map([[100,200]]),
                     new Set([300,400]),
                    {a:500,b:[600,700]}
                ]

                var [id,rid]  = await retry.add_retry_task(handle,arg_ary,5,20000);

                var arg_ary = [
                     1, new Date(), new Error("eeeee")
                ]
               var [id,rid]  = await retry.add_retry_task(handle,arg_ary,5,20000);


        > await retry.list_all()
        [
          [
            Map(1) { 100 => 200 },
            Set(2) { 300, 400 },
            { a: 500, b: [Array] }
          ],
          [
            1,
            2022-11-05T08:03:34.354Z,
            Error: eeeee
                at REPL37:2:29
                at Script.runInThisContext (node:vm:129:12)
                at REPLServer.defaultEval (node:repl:571:29)
                at bound (node:domain:421:15)
                at REPLServer.runBound [as eval] (node:domain:432:12)
                at REPLServer.onLine (node:repl:898:10)
                at REPLServer.emit (node:events:539:35)
                at REPLServer.emit (node:domain:475:12)
                at [_onLine] [as _onLine] (node:internal/readline/interface:424:12)
                at [_line] [as _line] (node:internal/readline/interface:885:18)
          ]
        ]

METHODS

      async .add_retry_task(
             handle            : Function | Lambda,    
             args_ary          : Array<any@StructureClonable> ,
                                  // handle(lefted_retry_times,...args_ary)                           
             max_retry_times    : Int32 ,
             retry_interval     : Int32@('unit IS "ms")
      ) -> [
          id#0  :  Int@(#0    <   2**48), 
          rid#1 :  Int@(#1+#0 === 2**48)
      ]
      
      
      async .cancel_retry_task(id:Number)

LICENSE

  • ISC
1.0.19

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago