mirror of
https://github.com/twisterarmy/twister-data-guard.git
synced 2025-03-13 05:51:30 +00:00
add multithread processes lock (for the crontab tasks compatibility) #1
This commit is contained in:
parent
7bc297de1d
commit
add9da3c94
54
follower.py
54
follower.py
@ -13,7 +13,11 @@ import sys, cPickle
|
|||||||
|
|
||||||
dbFileName = "twisterDataGuard.pickle"
|
dbFileName = "twisterDataGuard.pickle"
|
||||||
nodeUserName = "twisterdataguard"
|
nodeUserName = "twisterdataguard"
|
||||||
blocksInStep = 10000
|
blocksInStep = 10
|
||||||
|
|
||||||
|
class MyDb:
|
||||||
|
lastBlockHash = 0
|
||||||
|
dataLock = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from bitcoinrpc.authproxy import AuthServiceProxy
|
from bitcoinrpc.authproxy import AuthServiceProxy
|
||||||
@ -27,37 +31,45 @@ if len(sys.argv) > 1:
|
|||||||
|
|
||||||
twister = AuthServiceProxy(serverUrl)
|
twister = AuthServiceProxy(serverUrl)
|
||||||
|
|
||||||
class MyDb:
|
|
||||||
lastBlockHash = 0
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
db = cPickle.load(open(dbFileName))
|
db = cPickle.load(open(dbFileName))
|
||||||
nextHash = db.lastBlockHash
|
nextHash = db.lastBlockHash
|
||||||
|
dataLock = db.dataLock
|
||||||
except:
|
except:
|
||||||
db = MyDb()
|
db = MyDb()
|
||||||
nextHash = twister.getblockhash(0)
|
nextHash = twister.getblockhash(0)
|
||||||
|
dataLock = db.dataLock
|
||||||
|
|
||||||
print "blockchain reading..."
|
if not dataLock:
|
||||||
|
|
||||||
while True:
|
db.dataLock = True
|
||||||
|
cPickle.dump(db, open(dbFileName, "w"))
|
||||||
|
|
||||||
block = twister.getblock(nextHash)
|
print "blockchain reading..."
|
||||||
db.lastBlockHash = block["hash"]
|
|
||||||
|
|
||||||
blocksInStep = blocksInStep - 1
|
while True:
|
||||||
if blocksInStep < 0:
|
|
||||||
break
|
|
||||||
|
|
||||||
print "read block", str(block["height"])# + "\r",
|
block = twister.getblock(nextHash)
|
||||||
|
db.lastBlockHash = block["hash"]
|
||||||
|
|
||||||
for u in block["usernames"]:
|
blocksInStep = blocksInStep - 1
|
||||||
print "follow", u
|
if blocksInStep < 0:
|
||||||
twister.follow(nodeUserName, [u])
|
db.dataLock = False
|
||||||
if block.has_key("nextblockhash"):
|
break
|
||||||
nextHash = block["nextblockhash"]
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
cPickle.dump(db,open(dbFileName, "w"))
|
print "read block", str(block["height"])# + "\r",
|
||||||
|
|
||||||
print "task completed."
|
for u in block["usernames"]:
|
||||||
|
print "follow", u
|
||||||
|
twister.follow(nodeUserName, [u])
|
||||||
|
if block.has_key("nextblockhash"):
|
||||||
|
nextHash = block["nextblockhash"]
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
cPickle.dump(db, open(dbFileName, "w"))
|
||||||
|
|
||||||
|
print "task completed."
|
||||||
|
|
||||||
|
else:
|
||||||
|
print "operation locked by the running process."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user