Script occasionally runs often than configured and returns bogus data

Hi dear Ignited,

I am troubleshooting problem with script. We have set of machines that have ever-growing counter (it resets only on very rare occasions). On several occassions, the script has apparently ran twice at the same time. One run was legit but the other (given the bogus data in database) seems to return values tens of hours old. Let me demonstrate:

dbconn = system.tag.read('Globals/IgnitionRuntimeConnection').value
import datetime
t_sql = """
INSERT INTO [dbo].[x56_PROCESS_MAC_MONITORING](machineid, timestamp, alive, totalcounter)
VALueS (?,?,?,?)
"""
for i in range(13,29):
	tc = system.tag.read('x56_Process/Machine'+str(i)+'/Statistics/TotalCounter').value
	alive = True if tc is not None else False
	system.db.runPrepUpdate(t_sql, [i, datetime.datetime.now(), alive, tc], dbconn)

Script above runs every second and writes current value of one tag to database table for troubleshooting purposes for each machine. How come it is able to write 2 different values for one and the same second for one individual machine? The first value is correct (number is OK in accordance to ever-growing TotalCounter), but the other value written at the very same second must be at least 12 hours old. I can’t get my head around why this can happen. In general, there shouldn’t be more than 1 row per one machine per 1 second, yet there is.

Thanks for your feedback, SZ

Assuming v7.9?

First off, you are doing too many tag reads which is probably slowing the script down quite a bit.
generate a list of tag paths to read, read them all, then loop over the results.

dbconn = system.tag.read('Globals/IgnitionRuntimeConnection').value
t_sql = """
INSERT INTO [dbo].[x56_PROCESS_MAC_MONITORING](machineid, timestamp, alive, totalcounter)
VALueS (?,?,?,?)
"""
machNumbers = range (13,29)
tagPaths = ['x56_Process/Machine%s/Statistics/TotalCounter' % i for i in machNumbers]
tags = system.tag.readAll(tagPaths)

for i, tag in zip(machNumbers,tags):
	tc = tag.value
	alive = True if tc is not None else False
	system.db.runPrepUpdate(t_sql, [i, system.date.now(), alive, tc], dbconn)

Hi @dkhayes117 ,

thank you for useful hint. Yes, the gateway version is 7.9.18.