Scheduling - skip days

This commit is contained in:
Johnny Gear 2025-11-13 22:03:20 -06:00
parent 8e8a85e822
commit cef4a1b731
4 changed files with 38 additions and 11 deletions

36
db.py
View file

@ -1,11 +1,9 @@
import os
import sqlite3 import sqlite3
import datetime
from settings import SQLITE_DB from settings import SQLITE_DB
TABLE_REPEATS = ''' TABLE_REPEAT = '''
CREATE TABLE IF NOT EXISTS repeats ( CREATE TABLE IF NOT EXISTS repeat (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
probability FLOAT NOT NULL, probability FLOAT NOT NULL,
orders TEXT NOT NULL, orders TEXT NOT NULL,
@ -13,6 +11,13 @@ TABLE_REPEATS = '''
); );
''' '''
TABLE_SKIP_DAY = '''
CREATE TABLE IF NOT EXISTS skip_day (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TIMESTAMP UNIQUE NOT NULL
);
'''
TABLE_ORDER_STATUS = ''' TABLE_ORDER_STATUS = '''
CREATE TABLE IF NOT EXISTS order_status ( CREATE TABLE IF NOT EXISTS order_status (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -39,7 +44,8 @@ class Database:
def __init__(self): def __init__(self):
self.conn = sqlite3.connect(SQLITE_DB) self.conn = sqlite3.connect(SQLITE_DB)
self.conn.row_factory = sqlite3.Row self.conn.row_factory = sqlite3.Row
self.table_init(TABLE_REPEATS) self.table_init(TABLE_REPEAT)
self.table_init(TABLE_SKIP_DAY)
self.table_init(TABLE_PUNISHMENT_STATUS) self.table_init(TABLE_PUNISHMENT_STATUS)
self.table_init(TABLE_ORDER_STATUS) self.table_init(TABLE_ORDER_STATUS)
@ -56,21 +62,33 @@ class Database:
def repeat_get(self): def repeat_get(self):
c = self.conn.cursor() c = self.conn.cursor()
sql = 'SELECT id, probability, orders, count FROM repeats LIMIT 1' sql = 'SELECT id, probability, orders, count FROM repeat LIMIT 1'
c.execute(sql) c.execute(sql)
return c.fetchone() return c.fetchone()
def repeat_increment(self): def repeat_increment(self):
self.update('UPDATE repeats SET count = count + 1') self.update('UPDATE repeat SET count = count + 1')
def repeat_put(self, probability, orders): def repeat_put(self, probability, orders):
self.update( self.update(
'INSERT INTO repeats (probability, orders) VALUES (?, ?);', 'INSERT INTO repeat (probability, orders) VALUES (?, ?);',
[probability, orders] [probability, orders]
) )
def repeat_clear(self): def repeat_clear(self):
self.update('DELETE FROM repeats') self.update('DELETE FROM repeat')
def skip_day_put(self, date):
self.update(
'INSERT INTO skip_day (date) VALUES (?);',
[date]
)
def skip_day_contains(self, date):
c = self.conn.cursor()
sql = 'SELECT * FROM skip_day WHERE date=?;'
c.execute(sql, [date])
return c.fetchone() is not None
def order_status_put(self, mastodon_id, created_at, text): def order_status_put(self, mastodon_id, created_at, text):
self.update( self.update(

View file

@ -1,4 +1,3 @@
import os
import sys import sys
import logging import logging
import argparse import argparse

View file

@ -1,4 +1,3 @@
import os
import logging import logging
from settings import MASTODON_INSTANCE, MASTODON_ACCESS_TOKEN from settings import MASTODON_INSTANCE, MASTODON_ACCESS_TOKEN

View file

@ -6,6 +6,7 @@ from scheduler.asyncio import Scheduler
from settings import TIMEZONE, ORDER_TIME, ORDER_TIMEOUT from settings import TIMEZONE, ORDER_TIME, ORDER_TIMEOUT
from orders import order_issue, order_check from orders import order_issue, order_check
from db import Database
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,8 +26,18 @@ class OrderScheduler():
logger.info(self.scheduler) logger.info(self.scheduler)
async def scheduled_order(self): async def scheduled_order(self):
# Skip weekends
day_of_week = datetime.datetime.now(tz=self.tz).weekday() day_of_week = datetime.datetime.now(tz=self.tz).weekday()
if (day_of_week in [SATURDAY, SUNDAY]): if (day_of_week in [SATURDAY, SUNDAY]):
logger.info('Today is a weekend')
return
# Skip stored dates
d = Database()
today = datetime.datetime.now(tz=self.tz).strftime("%Y-%d-%m")
logger.info('Today %s' % today)
if (d.skip_day_contains(today)):
logger.info('Today is a skip day')
return return
await order_issue() await order_issue()