Package: lsbatch
About lsbatchlsbatchis aLeadSquaredprovided Batch Jobs software development kit. This allows developers to code and test Batch Jobs offline. It can also be used to prepare final deliverable that can uploaded to configure Batch Jobs in user's LeadSquared account.Getting StartedAssuming that you have Python 3.7 and virtualenv installed, installlsbatchpipinstalllsbatchUsageInitiate a new Batch Job projectTo create a new Batch Job in your local machine, execute the following command in cmd or shell:lsbatchinitYou will be asked to provide name of this Batch Job>YourBatchJobName:A new folder with the provided name will be created in the current directory.
This folder will be having all the libraries required for Batch Jobs local development.
You will find custom virtualenv in folder namebatch-virtualenvwith following default packages already installedboto3==1.14.33mysql-connector-python==8.0.21pysftp==0.2.9sendgrid==6.4.4pandas==1.1.0pyyaml==5.3.1openpyxl==3.0.4requests==2.24.0tabulate==0.8.7numpy==1.19.1After initiating batch withlsbatch init,  make sure all the following commands are executed at the project folder levelcd{batch_job_folder_name}General rules for writing a Batch JobYour Batch Job folder should have a folder by namesrcwhich will be used to write your actual code. This folder is automatically created during theinitprocessUser should keep their Batch Job files in 'src' folder onlysrcfolder should havemain.pyfile that is basically the starting point of your application. By default this file is created with sample code during theinitprocessYour src folder may contain a requirements.txt file which can be used to provide any custom packages which you may requireUsing inbuilt Batch Jobs functionBatch Job provides various functions for comman tasks like logging, sending notification emails or making DB queries.lsbatchpacks and mocks the same functions and are listed below:LoggingLS.log.info('Hello')
LS.log.info('Hello', {"Support Email": "support@leadsquared.com"})
LS.log.info(LS.settings.mykey)
LS.log.debug('Debug Log Example')
LS.log.error(type(err).__name__, traceback.format_exc())
LS.log.fatal(type(err).__name__, traceback.format_exc())Using VariableUsed to store key/value pair with sensitive/private data. For example API key for some third party service or your SFTP password. It can be retrieved using following keyLS.settings.<key_name>Send Batch Job status notification emailLS.email.send_email_notification(<EmailType>, <Subject>, <EmailBody>, isHTML =<True/False>)WhereEmailType: one of "Success" or "Failure" which corresponds to success or failure of execution statusSubject: can be maximum 255 character, above which it will be trimmedEmailBody: plain text or HTML contentisHTML: is an optional parameter with default value as TrueMaking DB CallsYou can connect to your LeadSquared account database and ETL database as well. Use the following function to query the sameLS.db.execute_DB_query(<query>, multi=False)
LS.db.execute_ETL_query(<query>, multi=False)by default the function considers the query are single statement. For multiline query, passmultiparameter asTrueMocking DB query resultTo mock the DB result use the filequery.jsonwhich is provided in the root project folder. It contains a json wherekey is the DB query(exact - case sensitive) andvalue is the csv file name that contains the result.You will also find asample_query_result.csvin project's root folder which is mapped as sample response inquery.json.You can create csv file by any name. Please note that this is purely for mocking purpose and does not evaluate the query, syntax or result in any way. Use the admin panel in your LeadSquared account to validate the same.Executing a Batch JobTo run Batch Job, run commandlsbatch runlsbatchrunThis will execute the code in context of virtual environment that has already been setupInstall LeadSquared Batch Job dependencies in current folderThe root folder already contains a.gitignorefile that will commit only necessary files to your source code repository. Feel free to update the file as per your need. If you clone the repository, run thelsbatch installcommand inside the project directory to setup the Batch Job environment again.lsbatchinstallThis command can also be used in existing projects to reset the Batch Jobs environment at any time.To run this command succesfully, current directory should havesrcthat contains your codeInstall your custom packageslsbatchinstall{package_name}This installs your custom package tobatch-virtualenvfolder. Also it creates/updates arequirements.txtfile insidesrcfolder.requirements.txtfile will be used to identify dependencies at the time of actual execution of the Batch JobPackaging your project for deploymentBatch Job accepts the deliverables as a zip file. To zip the project in a correct way, execute the following commandlsbatchpackThis will create acode.zipfile in the directory nameddeliverables. Remember to add this directory to the.gitignorefile in order to reduce the size of repositoryUninstall packageTo uninstall a custom package, execute the following commandlsbatchuninstall{package_name}This will also remove the package name fromrequirements.txtfileConfiguring Tenant ProfileYou can create multiple tenant profiles with various access levels using the configure command$lsbatchconfigureYou will be prompted to enterProfile Name : Define the profile name. We suggest you to enter a profile name in the following convention, so that it is easy to remember and re-use - + (e.g., 1234Admin)Orgcode : LeadSquared organization code (e.g., 1234). To find your orgcode, see How to Find My LeadSquared Orgcode.Region : You may pass 1, 11, 31 or 21. Alternatively, you can pass SGP, US, IR or MUM.
1 denotes Singapore | 11 denotes U.S.A | 21 denotes India | 31 denotes Ireland.Token : Pass the token generated from the Batch Jobs UI.You can also simply run lsbatch, it will prompt all available commands$lsbatchCreate and Upload a Batch Job Code for DeploymentTo learn how to upload the batch job code directly from the LeadSquared UI, seeCreate a Batch JobPass the command line parameters --orgcode --region --token. You may also pass these parameters as -o, -r and -t.E.g.$lsbatchsave--orgcode1234--region1--tokenasdfghijkl1234Alternatively, if you've configured a profile use --profile or -p.E.g.$lsbatchsave--profilemytenant1234Note:If the Batch Job Id doesn't exist in the batch_details.json file, the command creates a Batch Job and uploads the code.If the Batch Job Id exists in batch_detals.json, it packs and uploads the code to the respective Batch Job.To save the settings, pass command line parameter --settings, or -s.E.g.$lsbatchsave--profilemytenant1234--settingsNote: Only Admins and Developer-Configurator can save the settings.Deploy or Publish a Batch JobThe following command publishes a Batch Job.Pass the command line parameters --orgcode --region --token. You may also pass these parameters as -o, -r and -t.E.g.$lsbatchpublish--orgcode1234--region1--tokenasdfghijkl1234Alternatively, if you've configured a profile use --profile or -p.E.g.$lsbatchpublish--profilemytenant1234Note: Only Admins can publish a Batch Job.