08-11-2023 11:59 PM
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
Solved! Go to Solution.
08-17-2023 07:50 AM
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.
08-17-2023 05:05 AM
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.
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)
08-17-2023 07:50 AM
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.
08-17-2023 08:51 PM
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
08-18-2023 02:57 AM
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.