Skip to content

Commit

Permalink
Tweak timeouts. Add dot.env.example .env file.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlinvill committed Jul 25, 2023
1 parent a8eec3f commit 07a236a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
5 changes: 3 additions & 2 deletions demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ HOSTC="${MYIP}:8102"
HOSTD="${MYIP}:8103"


echo "Starting HOSTURI=${HOSTA} PEERA_URI=${HOSTB} PEERB_URI=${HOSTC} PEERC_URI=${HOSTD} ./main.py"
( HOSTURI="${HOSTA}" PEERA_URI="${HOSTB}" PEERB_URI="${HOSTC}" PEERC_URI="${HOSTD}" ./main.py ) & hosta_pid=$!
# This should be the default .env case. Let's test that.
echo "Starting ./main.py with default .env"
( ./main.py ) & hosta_pid=$!

echo "Starting HOSTURI=${HOSTB} PEERA_URI=${HOSTA} PEERB_URI=${HOSTC} PEERC_URI=${HOSTD} ./main.py"
( HOSTURI="${HOSTB}" PEERA_URI="${HOSTA}" PEERB_URI="${HOSTC}" PEERC_URI="${HOSTD}" ./main.py ) & hostb_pid=$!
Expand Down
8 changes: 4 additions & 4 deletions distributed/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DistributedLock:
"""
def __init__(self, mynode: str, peerlist: List, leader: Value):
self.autounlocktime = 35
self.autounlocktime = 25
self.peers = peerlist or []
self.lockmanager = None
self.syncobj = None
Expand All @@ -74,14 +74,14 @@ def run(self):
while True:
try:
if self.lockmanager.tryAcquire(
"coincidenceLock", sync=True, timeout=randint(40, 60)
"coincidenceLock", sync=True, timeout=randint(30, 60)
):
# we have the write lock
self.setleaderstate(True)
sleep(14)
sleep(10)
else:
self.setleaderstate(False)
sleep(randint(1, 5))
sleep(randint(1, 15))

except Exception as errmsg:
print(f"Exception! {errmsg}")
Expand Down
5 changes: 5 additions & 0 deletions dot.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HOSTURI="127.0.0.1:8100"

PEERA_URI="127.0.0.1:8101"
PEERB_URI="127.0.0.1:8102"
PEERC_URI="127.0.0.1:8103"
16 changes: 13 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import sys
from dotenv import load_dotenv
import click

from typing import List
from time import sleep
Expand All @@ -30,14 +31,19 @@ def runlock(mynode: str, peerlist: List, leader_state: Value):
distributedlock.run()


if __name__ == "__main__":
@click.group(invoke_without_command=True)
@click.option('--env', type=str, default='.env',
show_default='.env', help='environment file containing the host/peer configuration')
@click.pass_context
def main(ctx, env):
mp.set_start_method("spawn")
MYHOSTURI = False
peers = []

console = Console()

load_dotenv()
envf = env or '.env'
load_dotenv(envf)

if "HOSTURI" in os.environ:
MYHOSTURI = os.environ["HOSTURI"]
Expand All @@ -50,7 +56,7 @@ def runlock(mynode: str, peerlist: List, leader_state: Value):


assert MYHOSTURI is not False
assert len(peers) != 0
assert len(peers) > 0

console.log(f"I am {MYHOSTURI}")
console.log(f"peers are {peers}")
Expand All @@ -75,3 +81,7 @@ def runlock(mynode: str, peerlist: List, leader_state: Value):

finally:
p.join()


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
click
python-dotenv
rich
pysyncobj
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
license='MIT',
packages=['distributedlock'],
install_requires=[
'click',
'python-dotenv',
'pysyncobj',
'rich',
Expand Down

0 comments on commit 07a236a

Please sign in to comment.