Feature Request: Add delay, wait or sleep snap

I think it will be very good to have a snap that will wait for a preconfigured period of time before continuing to execute next snaps.

For example, I am invoking some pipeline and it fails for some reason. I want to re-invoke it again but after 30 seconds(configurable with parameter).

So, I guess it would be good to have a snap that will wait or even better pause the pipeline execution for some period of time and then continue from the point where it is paused.

You can put a sleep in script snap using any script language - Javascript, python or Ruby

I already tried using timeout in JavaScript, but it was not working. So I implemented another script that is doing this:

var date = new Date();
do { curDate = new Date(); }
while(curDate-date < Interval);

which is not ideal solution.

Basically, a snap that will pause the pipeline for that period of time and continue where it is paused will be best option for this instead of doing a script that is in running state.

Try this. I used Ruby. It is simple

sleep 5

will sleep for 5 seconds.

wait_2017_09_13.slp (6.9 KB)

2 Likes

Tried using the same script more than once in single pipeline and through an error,

Script:class MyScript
    # Import the interface required by the Script snap.
    include com.snaplogic.scripting.language.ScriptHook
    attr_reader :log, :input, :output, :error
    def initialize(log, input, output, error)
        @log = log
        @input = input
        @output = output
        @error = error
    end

    # The "execute()" method is called once when the pipeline is started
    # and allowed to process its inputs or just send data to its outputs.
    def execute()
        while input.hasNext() do
            begin
                # Read the next document, wrap it in a map and write out the wrapper
                doc = input.next()
                wrapper = {
                 "original" => doc
                }
                sleep (5/100)
            
                log.info("Executed Ruby script")
                output.write(doc, wrapper)
            
            rescue => e
                log.error("Bad Rublet " + e.message)
                errWrapper = {
                    "errMsg" => e.message
                }
                error.write(errWrapper)
            end
        end
        
        
    end
end

# The Script Snap will look for a ScriptHook object in the "hook"
# variable.  The snap will then call the hook's "execute" method.
$hook = MyScript.new($log, $input, $output, $error) does not satisfy the contract

Resolution:
Add a global variable named 'hook' to the script that is instantiated to the class that implements ScriptHook interface

Reason:
Script does not have a variable named 'hook'

anyone has solved this?

Suddenly stopped working with the following error
Was working earlier but stopped after snaplex restart

Check if you have multiple instances of Ruby Script snaps in a single pipeline. The Ruby implementation has a limitation which prevents that from working. Python and JavaScript do not have this limitation

Python Script Sleep time

Import the interface required by the Script snap.

from com.snaplogic.scripting.language import ScriptHook

import time

class TransformScript(ScriptHook):

def __init__(self, input, output, error, log):

    self.input = input

    self.output = output

    self.error = error

    self.log = log


# The "execute()" method is called once when the pipeline is started

# and allowed to process its inputs or just send data to its outputs.

def execute(self):

    self.log.info("Executing Transform script")

    while self.input.hasNext():

        # Read the next document, wrap it in a map and write out the wrapper

        in_doc = self.input.next()

        time.sleep(30)

        self.output.write(in_doc)

    self.log.info("Finished executing the Transform script")

The Script Snap will look for a ScriptHook object in the “hook”

variable. The snap will then call the hook’s “execute” method.

hook = TransformScript(input, output, error, log)

I Restarted the snaplex and its working now. thanks @akidave

Hello, It would be so much easy if Snaplogic could provide a Snap for this.

Hi, there is new feature in Snaplogic called Resumable pipelines, which helps in re running the pipeline from point of failure in case of errors.Click here for more information on this feature.

Regards
Anubhav