for chat_id in db["chats"]:
chat = db["chats"][chat_id]
for user_id in chat["users"]:
u_id = int(user_id)
user = chat["users"][user_id]
if "prefix" in user:
prefix = user["prefix"].copy()
duration = prefix.get("duration")
if duration is not None and duration > 0:
prefix["duration"] -= 1
if prefix["duration"] == 0:
prefix["prefix_name"] = ""
if u_id in chat["users"]:
chat["users"][u_id]["prefix"] = prefix
saver.save()
print ("its okay")
написал для него apscheduler:
async def stscheduler():
scheduler = AsyncIOScheduler()
scheduler.add_job(upd_pref, 'interval', minutes=1)
scheduler.start()
задачу запускает:
apscheduler.executors.default:Running job "upd_pref (trigger: in
terval[0:01:00], next run at: 2023-05-10 12:33:38 UTC)" (scheduled at
2023-05-10 12:32:38.968974+00:00)
its okay
INFO:apscheduler.executors.default:Job "upd_pref (trigger: interval[0
:01:00], next run at: 2023-05-10 12:33:38 UTC)" executed successfully
но изменения в бд не происходят.
saver.save()
class NotifyingFuture(asyncio.Future):
def init(self, *args, **kwargs):
self.__to_notify_on_await = kwargs.pop("on_await", None)
super().init(*args, **kwargs)
async def _wait_then_do(time, task, *args, **kwargs):
await asyncio.sleep(time)
return await task(*args, **kwargs)
class DbSaver:
def init(self):
self._pending = None
self._waiter = asyncio.Event()
self._sync_future = None
@staticmethod
async def _set():
with open(db_path, "w") as f:
f.write(json.dumps(db, indent=2))
def _cancel_then_set(self):
if self._pending is not None and not self._pending.cancelled():
self._pending.cancel()
self._pending = asyncio.ensure_future(self._set())
def save(self):
if self._pending is not None and not self._pending.cancelled():
self._pending.cancel()
if self._sync_future is None or self._sync_future.done():
self._sync_future = NotifyingFuture(on_await=self._cancel_then_set)
self._pending = asyncio.ensure_future(
_wait_then_do(5, self._set)
) # Delay database ops by 10s
return self._sync_future
saver = DbSaver()
куда копать и на что смотреть?
Живи
Обсуждают сегодня