1
0
mirror of https://github.com/azlux/botamusique synced 2024-11-23 13:56:17 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Terry Geng
3200c83fd0
Merge pull request #280 from TerryGeng/racing
fix: Racing condition happens when editing playlist.
2021-05-24 09:35:56 +08:00
Terry Geng
b65e7b2bcc
fix: Racing condition happens when editing playlist. Fixes #279. 2021-05-24 09:26:23 +08:00

View File

@ -46,6 +46,7 @@ class BasePlaylist(list):
self.pending_items = [] self.pending_items = []
self.log = logging.getLogger("bot") self.log = logging.getLogger("bot")
self.validating_thread_lock = threading.Lock() self.validating_thread_lock = threading.Lock()
self.playlist_lock = threading.RLock()
def is_empty(self): def is_empty(self):
return True if len(self) == 0 else False return True if len(self) == 0 else False
@ -59,69 +60,82 @@ class BasePlaylist(list):
return self return self
def append(self, item: CachedItemWrapper): def append(self, item: CachedItemWrapper):
self.version += 1 with self.playlist_lock:
super().append(item) self.version += 1
self.pending_items.append(item) super().append(item)
self.pending_items.append(item)
self.async_validate() self.async_validate()
return item return item
def insert(self, index, item): def insert(self, index, item):
self.version += 1 with self.playlist_lock:
self.version += 1
if index == -1:
index = self.current_index
if index == -1: super().insert(index, item)
index = self.current_index
super().insert(index, item) if index <= self.current_index:
self.current_index += 1
if index <= self.current_index: self.pending_items.append(item)
self.current_index += 1
self.pending_items.append(item)
self.async_validate() self.async_validate()
return item return item
def extend(self, items): def extend(self, items):
self.version += 1 print("extend to-enter")
super().extend(items) with self.playlist_lock:
self.pending_items.extend(items) print("extend enter")
self.version += 1
super().extend(items)
self.pending_items.extend(items)
print("extend leave")
self.async_validate() self.async_validate()
return items return items
def next(self): def next(self):
if len(self) == 0: with self.playlist_lock:
return False if len(self) == 0:
return False
if self.current_index < len(self) - 1: if self.current_index < len(self) - 1:
self.current_index += 1 self.current_index += 1
return self[self.current_index] return self[self.current_index]
else: else:
return False return False
def point_to(self, index): def point_to(self, index):
if -1 <= index < len(self): with self.playlist_lock:
self.current_index = index if -1 <= index < len(self):
self.current_index = index
def find(self, id): def find(self, id):
for index, wrapper in enumerate(self): with self.playlist_lock:
if wrapper.item.id == id: for index, wrapper in enumerate(self):
return index if wrapper.item.id == id:
return index
return None return None
def __delitem__(self, key): def __delitem__(self, key):
return self.remove(key) return self.remove(key)
def remove(self, index): def remove(self, index):
self.version += 1 with self.playlist_lock:
if index > len(self) - 1: self.version += 1
return False if index > len(self) - 1:
return False
removed = self[index] removed = self[index]
super().__delitem__(index) super().__delitem__(index)
if self.current_index > index: if self.current_index > index:
self.current_index -= 1 self.current_index -= 1
# reference counter # reference counter
counter = 0 counter = 0
@ -146,47 +160,54 @@ class BasePlaylist(list):
self.remove(index) self.remove(index)
def current_item(self): def current_item(self):
if len(self) == 0: with self.playlist_lock:
return False if len(self) == 0:
return False
return self[self.current_index] return self[self.current_index]
def next_index(self): def next_index(self):
if self.current_index < len(self) - 1: with self.playlist_lock:
return self.current_index + 1 if self.current_index < len(self) - 1:
else: return self.current_index + 1
return False
return False
def next_item(self): def next_item(self):
if self.current_index < len(self) - 1: with self.playlist_lock:
return self[self.current_index + 1] if self.current_index < len(self) - 1:
else: return self[self.current_index + 1]
return False
return False
def randomize(self): def randomize(self):
# current_index will lose track after shuffling, thus we take current music out before shuffling with self.playlist_lock:
# current = self.current_item() # current_index will lose track after shuffling, thus we take current music out before shuffling
# del self[self.current_index] # current = self.current_item()
# del self[self.current_index]
random.shuffle(self) random.shuffle(self)
# self.insert(0, current) # self.insert(0, current)
self.current_index = -1 self.current_index = -1
self.version += 1 self.version += 1
def clear(self): def clear(self):
self.version += 1 with self.playlist_lock:
self.current_index = -1 self.version += 1
self.current_index = -1
super().clear()
var.cache.free_all() var.cache.free_all()
super().clear()
def save(self): def save(self):
var.db.remove_section("playlist_item") with self.playlist_lock:
assert self.current_index is not None var.db.remove_section("playlist_item")
var.db.set("playlist", "current_index", self.current_index) assert self.current_index is not None
var.db.set("playlist", "current_index", self.current_index)
for index, music in enumerate(self): for index, music in enumerate(self):
var.db.set("playlist_item", str(index), json.dumps({'id': music.id, 'user': music.user})) var.db.set("playlist_item", str(index), json.dumps({'id': music.id, 'user': music.user}))
def load(self): def load(self):
current_index = var.db.getint("playlist", "current_index", fallback=-1) current_index = var.db.getint("playlist", "current_index", fallback=-1)
@ -243,8 +264,8 @@ class BasePlaylist(list):
self.log.debug("playlist: validating failed.") self.log.debug("playlist: validating failed.")
if var.bot: if var.bot:
var.bot.send_channel_msg(e.msg) var.bot.send_channel_msg(e.msg)
var.cache.free_and_delete(item.id)
self.remove_by_id(item.id) self.remove_by_id(item.id)
var.cache.free_and_delete(item.id)
continue continue
if item.version > ver: if item.version > ver:
@ -261,35 +282,42 @@ class OneshotPlaylist(BasePlaylist):
self.current_index = -1 self.current_index = -1
def current_item(self): def current_item(self):
if self.current_index == -1: with self.playlist_lock:
self.current_index = 0 if len(self) == 0:
self.current_index = -1
return False
return self[self.current_index] if self.current_index == -1:
def from_list(self, _list, current_index):
if len(_list) > 0:
if current_index > -1:
for i in range(current_index):
_list.pop(0)
return super().from_list(_list, 0)
return super().from_list(_list, -1)
return self
def next(self):
if len(self) > 0:
self.version += 1
if self.current_index != -1:
super().__delitem__(self.current_index)
if len(self) == 0:
return False
else:
self.current_index = 0 self.current_index = 0
return self[0] return self[self.current_index]
else:
self.current_index = -1 def from_list(self, _list, current_index):
return False with self.playlist_lock:
if len(_list) > 0:
if current_index > -1:
for i in range(current_index):
_list.pop(0)
return super().from_list(_list, 0)
return super().from_list(_list, -1)
return self
def next(self):
with self.playlist_lock:
if len(self) > 0:
self.version += 1
if self.current_index != -1:
super().__delitem__(self.current_index)
if len(self) == 0:
return False
else:
self.current_index = 0
return self[0]
else:
self.current_index = -1
return False
def next_index(self): def next_index(self):
if len(self) > 1: if len(self) > 1:
@ -304,10 +332,11 @@ class OneshotPlaylist(BasePlaylist):
return False return False
def point_to(self, index): def point_to(self, index):
self.version += 1 with self.playlist_lock:
self.current_index = -1 self.version += 1
for i in range(index): self.current_index = -1
super().__delitem__(0) for i in range(index):
super().__delitem__(0)
class RepeatPlaylist(BasePlaylist): class RepeatPlaylist(BasePlaylist):
@ -316,23 +345,27 @@ class RepeatPlaylist(BasePlaylist):
self.mode = "repeat" self.mode = "repeat"
def next(self): def next(self):
if len(self) == 0: with self.playlist_lock:
return False if len(self) == 0:
return False
if self.current_index < len(self) - 1: if self.current_index < len(self) - 1:
self.current_index += 1 self.current_index += 1
return self[self.current_index] return self[self.current_index]
else: else:
self.current_index = 0 self.current_index = 0
return self[0] return self[0]
def next_index(self): def next_index(self):
if self.current_index < len(self) - 1: with self.playlist_lock:
return self.current_index + 1 if self.current_index < len(self) - 1:
else: return self.current_index + 1
return 0 else:
return 0
def next_item(self): def next_item(self):
if len(self) == 0:
return False
return self[self.next_index()] return self[self.next_index()]
@ -347,17 +380,18 @@ class RandomPlaylist(BasePlaylist):
return super().from_list(_list, -1) return super().from_list(_list, -1)
def next(self): def next(self):
if len(self) == 0: with self.playlist_lock:
return False if len(self) == 0:
return False
if self.current_index < len(self) - 1: if self.current_index < len(self) - 1:
self.current_index += 1 self.current_index += 1
return self[self.current_index] return self[self.current_index]
else: else:
self.version += 1 self.version += 1
self.randomize() self.randomize()
self.current_index = 0 self.current_index = 0
return self[0] return self[0]
class AutoPlaylist(OneshotPlaylist): class AutoPlaylist(OneshotPlaylist):