cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Delaying a process

Abhishek_117
New Contributor II

How can i delay a process according to our needs, is there any snap to perform wait operation?
I tried with script snap, mentioned there to delay for 15 seconds,but its taking more time than 15 seconds.

CODE

 

# Import the interface required by the Script snap.
from com.snaplogic.scripting.language import ScriptHook
from random import randint
from time import sleep
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():
data = self.input.next()
time.sleep(15)
map = {"out": data}
self.output.write(map)

self.log.info("Script executed")

# The "cleanup()" method is called after the snap has exited the execute() method
def cleanup(self๐Ÿ˜ž
self.log.info("Cleaning up")

# 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)
 
 
 
This code should perform 15 seconds of wait operation,but taking long time.

 

1 ACCEPTED SOLUTION

j_angelevski
Contributor III

Hi @Abhishek_117 

The code you shared does wait 15 seconds, but it waits on each input document. This means that if you have 10 input documents, you will have a wait time of 150 seconds.

I am not sure where you set up your script in your pipeline, but you have to make sure you are either waiting 15 seconds before the input documents start processing, or waiting 15 seconds after each input document has been processed. You can do this if you amend your code, specifically the time.sleep() function.

If you want your script to wait for 15 seconds before it starts processing the input documents, the you can use the following code in your execute() function:

def execute(self):
        self.log.info("Executing Transform script")
        time.sleep(15)
        while self.input.hasNext():
            try:
                # Read the next input document, store it in a new dictionary, and write this as an output document.
                inDoc = self.input.next()
                outDoc = {
                    'original' : inDoc
                }
                self.output.write(inDoc, outDoc)
            except Exception as e:
                errDoc = {
                    'error' : str(e)
                }
                self.log.error("Error in python script")
                self.error.write(errDoc)

        self.log.info("Script executed")

 Or, if you want to wait for 15 seconds after all documents have been 

def execute(self):
        self.log.info("Executing Transform script")
        while self.input.hasNext():
            try:
                # Read the next input document, store it in a new dictionary, and write this as an output document.
                inDoc = self.input.next()
                outDoc = {
                    'original' : inDoc
                }
                self.output.write(inDoc, outDoc)
            except Exception as e:
                errDoc = {
                    'error' : str(e)
                }
                self.log.error("Error in python script")
                self.error.write(errDoc)

        time.sleep(15)
        self.log.info("Script executed")

The only difference is that you need to put time.sleep(15) before the while loop or after the while loop, this of course depends on your case.

View solution in original post

5 REPLIES 5

virender_prajap
New Contributor III

I am using Python Script with 15 seconds of sleep. Snap is taking slightly more than 15 seconds because of script load, execution, input & output time.

virender_prajap_0-1692273409854.png

Try this one, may be it can work for you.
Scripting Language: Python

# Import the interface required by the Script snap.
from com.snaplogic.scripting.language import ScriptHook
import java.util.HashMap
import time
import os
import platform

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 input document, wrap it in a map and write out the wrapper.
            inDoc = self.input.next()
            outDoc = inDoc    
            time.sleep(15)
            self.output.write(inDoc, outDoc)
        self.log.info("Script executed")

# 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)



j_angelevski
Contributor III

Hi @Abhishek_117 

The code you shared does wait 15 seconds, but it waits on each input document. This means that if you have 10 input documents, you will have a wait time of 150 seconds.

I am not sure where you set up your script in your pipeline, but you have to make sure you are either waiting 15 seconds before the input documents start processing, or waiting 15 seconds after each input document has been processed. You can do this if you amend your code, specifically the time.sleep() function.

If you want your script to wait for 15 seconds before it starts processing the input documents, the you can use the following code in your execute() function:

def execute(self):
        self.log.info("Executing Transform script")
        time.sleep(15)
        while self.input.hasNext():
            try:
                # Read the next input document, store it in a new dictionary, and write this as an output document.
                inDoc = self.input.next()
                outDoc = {
                    'original' : inDoc
                }
                self.output.write(inDoc, outDoc)
            except Exception as e:
                errDoc = {
                    'error' : str(e)
                }
                self.log.error("Error in python script")
                self.error.write(errDoc)

        self.log.info("Script executed")

 Or, if you want to wait for 15 seconds after all documents have been 

def execute(self):
        self.log.info("Executing Transform script")
        while self.input.hasNext():
            try:
                # Read the next input document, store it in a new dictionary, and write this as an output document.
                inDoc = self.input.next()
                outDoc = {
                    'original' : inDoc
                }
                self.output.write(inDoc, outDoc)
            except Exception as e:
                errDoc = {
                    'error' : str(e)
                }
                self.log.error("Error in python script")
                self.error.write(errDoc)

        time.sleep(15)
        self.log.info("Script executed")

The only difference is that you need to put time.sleep(15) before the while loop or after the while loop, this of course depends on your case.

Abhishek_117
New Contributor II

Thanks  @j_angelevski 
Its working now,Please tell me how to learn more about various snaps like you know,I want to be more confident in snaps in snaplogic

To facilitate the enhancement of your proficiency with SnapLogic and the comprehensive utilization of its diverse features, several strategic approaches can be considered:

Firstly, an exploration of the official SnapLogic documentation is recommended. This resource provides extensive insights into each feature, as well as individual snaps, accompanied by practical use cases. Specifically, for a more in-depth understanding of the expression language, which parallels JavaScript in syntax, additional information can be accessed via the following link: Expression Language Guide. Should you possess prior knowledge of JavaScript, the transition to the expression language should be relatively seamless since the expression language has a similar syntax to that of JavaScript ( at least for the usage of functions and representation of objects/arrays ). Furthermore, detailed documentation concerning the Script snap is available here: Script Snap Guide.

Subsequently, active engagement through hands-on practice is highly recommended. Commencing with elementary projects and progressively advancing to more intricate integrations provides a foundational understanding of distinct snaps and their respective functionalities.

Moreover, the examination of pre-existing integrations and pipelines developed by others can be immensely instructive. Having a reverse engineering approach illuminates the usage of diverse snaps to accomplish specific tasks.

Last but certainly not least, engaging with the SnapLogic community can significantly enrich your learning journey. The collective wisdom of the community is a valuable resource that can provide you with practical tips, solutions to challenges, and alternative perspectives that enhance your command of SnapLogic. This collaborative engagement further complements your exploration of the platform's multifaceted features.