Skip to content

Commit

Permalink
Picking lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlinvill committed Jul 17, 2023
1 parent 323d822 commit 44e6812
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions distributed/lock.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Created on 5/17/2023
# @author Mark Linvill
#
# PySyncObj distributed lock adapted from pysyncobj examples/docs.
"""
Created on 5/17/2023
@author Mark Linvill
PySyncObj distributed lock adapted from pysyncobj examples/docs.
"""

from typing import List

Expand All @@ -20,42 +21,44 @@
from pysyncobj.batteries import ReplLockManager

statedesc = ["follower", "leader"]

STARTPORT = 8100

def getmyip() -> str:
"""
Set up a socket to determine our ip address.
:return: myip: My ip as a string
"""
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(0)
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.settimeout(0)
try:
s.connect(("10.254.254.254", 1))
myip = s.getsockname()[0]
sock.connect(("10.254.254.254", 1))
myip = sock.getsockname()[0]
except Exception:
myip = "127.0.0.1"

return myip


class DistributedLock:
def __init__(self, me: str, peers: List, leader: Value):
"""
Encapsulate distributed lock logic
"""
def __init__(self, mynode: str, peerlist: List, leader: Value):
self.autounlocktime = 35
self.peers = peers or []
self.peers = peerlist or []
self.lockmanager = None
self.syncobj = None
self.console = Console()
self.myip = me
self.port = 8100
self.myip = mynode
self.leader = leader # 1 for leader, 0 for follower
# self.pysyncobj_version = SyncObj.getCodeVersion()

if self.myip is None:
self.myip = f"{getmyip()}:{self.port}"
self.myip = f"{getmyip()}:{STARTPORT}"

assert self.myip is not False
assert len(self.peers)
assert len(self.peers) != 0

def run(self):
"""
Expand All @@ -79,15 +82,24 @@ def run(self):
self.setleaderstate(False)
sleep(randint(1, 5))

except Exception as e:
print(f"Exception! {e}")
except Exception as error:
print(f"Exception! {error}")

self.setleaderstate(False)
self.lockmanager.release("coincidenceLock", sync=True)


def setleaderstate(self, state: int):
"""
attribute setter
:param state:
:return: None
"""
self.leader.value = state

def getleaderstate(self) -> int:
"""
attribute getter
:return: int: leader value
"""
return self.leader.value

0 comments on commit 44e6812

Please sign in to comment.