Spaces:
Sleeping
Sleeping
File size: 1,746 Bytes
33d4721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import sqlite3
class AutoTrainDB:
"""
A class to manage job records in a SQLite database.
Attributes:
-----------
db_path : str
The path to the SQLite database file.
conn : sqlite3.Connection
The SQLite database connection object.
c : sqlite3.Cursor
The SQLite database cursor object.
Methods:
--------
__init__(db_path):
Initializes the database connection and creates the jobs table if it does not exist.
create_jobs_table():
Creates the jobs table in the database if it does not exist.
add_job(pid):
Adds a new job with the given process ID (pid) to the jobs table.
get_running_jobs():
Retrieves a list of all running job process IDs (pids) from the jobs table.
delete_job(pid):
Deletes the job with the given process ID (pid) from the jobs table.
"""
def __init__(self, db_path):
self.db_path = db_path
self.conn = sqlite3.connect(db_path)
self.c = self.conn.cursor()
self.create_jobs_table()
def create_jobs_table(self):
self.c.execute(
"""CREATE TABLE IF NOT EXISTS jobs
(id INTEGER PRIMARY KEY, pid INTEGER)"""
)
self.conn.commit()
def add_job(self, pid):
sql = f"INSERT INTO jobs (pid) VALUES ({pid})"
self.c.execute(sql)
self.conn.commit()
def get_running_jobs(self):
self.c.execute("""SELECT pid FROM jobs""")
running_pids = self.c.fetchall()
running_pids = [pid[0] for pid in running_pids]
return running_pids
def delete_job(self, pid):
sql = f"DELETE FROM jobs WHERE pid={pid}"
self.c.execute(sql)
self.conn.commit()
|