Beispiel #1
0
                 '0_common'))  # Change this later to a package import
import scai

COD_INTEGRATION = 11000  # CRM_BASE to Operational
COD_COUNTRY = -1  # Replaced by code in conf_file

print(datetime.now().time())

conf_file = sys.argv[1]  # File with names for the tables to copy and S3 path
source_conf_file = sys.argv[2]  # File with source database
target_conf_file = sys.argv[3]  # File with target database
data = json.load(open(conf_file))

COD_COUNTRY = int(data['cod_country'])  # Global variable

country_execution_status = scai.getCountryIntegrationStatus(
    target_conf_file, COD_COUNTRY)  # SCAI
scai_last_execution_status = scai.getLastExecutionStatus(
    target_conf_file, COD_INTEGRATION, COD_COUNTRY)  # SCAI

if (country_execution_status != 1 and scai_last_execution_status == 1):
    print(
        'The integration executed successfuly on last execution. The problem is further ahead.'
    )
    sys.exit(0)

if (scai_last_execution_status == 2):
    sys.exit("The integration is already running...")

#scai.integrationStart(target_conf_file, COD_INTEGRATION, COD_COUNTRY)	# SCAI

# Copy tables from crm_base schema to Operational Model
DB_CONF_FILE = sys.argv[1]
CONF_FILE = sys.argv[
    2]  #When it is necessary to automate for more sites, add argument and below variables attribution, and add all cod_source_systems to variable so that it goes something like: COD_SOURCE_SYSTEM= (12,13,14)

data = json.load(open(CONF_FILE))
COD_COUNTRY = data['COD_COUNTRY']
COD_INTEGRATION = data['COD_INTEGRATION']
DSC_PROCESS = data['DSC_PROCESS']
COD_SOURCE_SYSTEM = data['COD_SOURCE_SYSTEM']
PATH_TO_SQL = data['PATH_TO_SQL']

conn = getDatabaseConnection(DB_CONF_FILE)
cur = conn.cursor()

#SCAI Last run validation
country_execution_status = scai.getCountryIntegrationStatus(
    DB_CONF_FILE, COD_COUNTRY)  # SCAI

scai_last_execution_status = scai.getLastExecutionStatus(
    DB_CONF_FILE, COD_INTEGRATION, COD_COUNTRY)  # SCAI

if (country_execution_status != 1 and scai_last_execution_status == 1):
    print(
        'The integration executed successfuly on last execution. The problem is further ahead.'
    )
    sys.exit(0)

if (scai_last_execution_status == 2):
    sys.exit("The integration is already running...")

if (scai_last_execution_status == 3):
    scai_process_status = scai.processCheck(DB_CONF_FILE, DSC_PROCESS,
def main(conf_file, dml_file, country):
	print(datetime.now().time())
	print('Connecting to Database...')
	
	conn = getDatabaseConnection(conf_file)
	cur = conn.cursor()
	
	country_execution_status = scai.getCountryIntegrationStatus(conf_file, country)	# SCAI

	scai_last_execution_status = scai.getLastExecutionStatus(conf_file, COD_INTEGRATION, country)	# SCAI


	if (country_execution_status != 1 and scai_last_execution_status == 1):
		print ('The integration executed successfuly on last execution. The problem is further ahead.')
		sys.exit(0)

	if (scai_last_execution_status == 2):
		sys.exit("The integration is already running...")

    #If last execution ended in error, then check in which block it ended
	cur.execute("select "\
				" nvl(block_nbr,1) as block_nbr "\
				" from crm_integration_anlt.t_rel_scai_country_integration country_integration "\
				" where "\
				"	country_integration.cod_integration = %(COD_INTEGRATION)d "\
				"	and country_integration.cod_country = %(cod_country)d "\
				"   and country_integration.cod_status = 3 "\
				"	and ind_active = 1 "\
				% {
					'cod_country':country ,
					'COD_INTEGRATION':COD_INTEGRATION
				}
			)
	conn.commit()
	
	results = cur.fetchone()
	
	#If above query does not return a value (For example on a normal execution, without previous errors)
	if (not results):
		block_nbr = 1
	else:
		block_nbr = results[0]
	
	scai.integrationStart(conf_file, COD_INTEGRATION, country) 	# SCAI
	

	dml_scripts = open(dml_file).read().split('$$$')
	
	#print('Scripts: ' + dml_scripts)
	print('Executing DML scripts...') 
	i = 1
	for dml in dml_scripts:
		if i < block_nbr:  # Make this run starting from a certain block
			i = i + 1
			continue
		print('Running block #' + str(i))
		try:
			cur.execute("lock crm_integration_anlt.t_rel_scai_integration_process  in exclusive mode")
			cur.execute(dml)
		except Exception as e:
			conn.rollback() 
			scai.integrationEnd(conf_file, COD_INTEGRATION, country, 3, i)		# SCAI
			print (e)
			print (e.pgerror)
			sys.exit("The process aborted with error.")
		else:
			conn.commit() 

		i = i + 1

		
	print('Closing Database connection...')
	cur.close()
	conn.close()
	scai.integrationEnd(conf_file, COD_INTEGRATION, country, 1)		# SCAI
	print(datetime.now().time())
	print('All done!')