-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_jobs.py
59 lines (49 loc) · 1.47 KB
/
sync_jobs.py
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
from app import create_app, db
from app.db import CronJob
from app.cron_parser import CronParser
def sync_cron_jobs():
"""Sync cron jobs from system to database."""
parser = CronParser()
jobs = parser.get_all_jobs()
print(f"Found {len(jobs)} cron jobs in system")
# Get existing jobs from database
existing_jobs = CronJob.query.all()
existing_job_keys = {
(job.schedule, job.command, job.owner, job.source)
for job in existing_jobs
}
# Track changes
jobs_added = 0
jobs_unchanged = 0
for job in jobs:
job_key = (
job['schedule'],
job['command'],
job['owner'],
job['source']
)
# Skip if job already exists
if job_key in existing_job_keys:
jobs_unchanged += 1
continue
# Add new job to database
new_job = CronJob(
schedule=job['schedule'],
command=job['command'],
owner=job['owner'],
source=job['source']
)
db.session.add(new_job)
jobs_added += 1
# Commit changes
db.session.commit()
print(f"\nSync complete:")
print(f"- {jobs_added} new jobs added")
print(f"- {jobs_unchanged} jobs unchanged")
if __name__ == "__main__":
app = create_app()
with app.app_context():
# Ensure database tables exist
db.create_all()
# Sync jobs
sync_cron_jobs()