-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscheduler.py
More file actions
323 lines (262 loc) · 10.9 KB
/
scheduler.py
File metadata and controls
323 lines (262 loc) · 10.9 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
Background scheduler for automated report generation.
Runs as a daemon thread, checking every hour (by default) for any reports
that are due to be generated. Keeps things ticking along without user intervention.
"""
from __future__ import annotations
import os
import threading
import time
import logging
from datetime import datetime, timezone
from typing import Optional
import yaml
try:
from zoneinfo import ZoneInfo
except ImportError:
from backports.zoneinfo import ZoneInfo
import database as db
from pdf_generator import generate_report
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('scheduler')
# Global scheduler instance
_scheduler_instance: Optional['ReportScheduler'] = None
_scheduler_lock = threading.Lock()
class ReportScheduler:
"""Background scheduler that checks and runs scheduled reports."""
def __init__(self, check_interval_minutes: int = 60, api_token: str = None):
"""
Initialize the scheduler.
Args:
check_interval_minutes: How often to check for due schedules (default 60)
api_token: Default API token to use for report generation
"""
self.check_interval = check_interval_minutes * 60 # Convert to seconds
self.api_token = api_token
self._stop_event = threading.Event()
self._thread: Optional[threading.Thread] = None
self._running = False
def start(self):
"""Start the scheduler in a background thread."""
if self._running:
logger.warning("Scheduler is already running")
return
self._stop_event.clear()
self._thread = threading.Thread(target=self._run_loop, daemon=True)
self._thread.start()
self._running = True
logger.info(f"Scheduler started (checking every {self.check_interval // 60} minutes)")
def stop(self):
"""Stop the scheduler."""
if not self._running:
return
self._stop_event.set()
if self._thread:
self._thread.join(timeout=5)
self._running = False
logger.info("Scheduler stopped")
def is_running(self) -> bool:
"""Check if scheduler is running."""
return self._running
def _run_loop(self):
"""Main scheduler loop."""
logger.info("Scheduler loop started")
while not self._stop_event.is_set():
try:
self._check_and_run_schedules()
except Exception as e:
logger.error(f"Error in scheduler loop: {e}")
# Wait for next check interval (but check stop event frequently)
for _ in range(self.check_interval):
if self._stop_event.is_set():
break
time.sleep(1)
logger.info("Scheduler loop exited")
def _check_and_run_schedules(self):
"""Check all scheduled templates and run any that are due."""
logger.debug("Checking schedules...")
templates = db.get_scheduled_templates()
for template in templates:
try:
if self._is_due(template):
logger.info(f"Template '{template['name']}' is due, running...")
self._run_template(template)
except Exception as e:
logger.error(f"Error processing template {template['id']}: {e}")
def _is_due(self, template: dict) -> bool:
"""Check if a template's schedule is due to run."""
try:
tz = ZoneInfo(template.get('schedule_timezone') or 'UTC')
except Exception:
tz = ZoneInfo('UTC')
logger.warning(f"Invalid timezone '{template.get('schedule_timezone')}', using UTC")
now = datetime.now(tz)
frequency = template.get('schedule_frequency')
target_hour = template.get('schedule_hour')
if not frequency or target_hour is None:
return False
# Check if we're in the right hour
if now.hour != target_hour:
return False
# Check if we already ran this hour
last_run = template.get('schedule_last_run')
if last_run:
try:
last_run_dt = datetime.fromisoformat(last_run.replace('Z', '+00:00'))
if last_run_dt.tzinfo is None:
last_run_dt = last_run_dt.replace(tzinfo=ZoneInfo('UTC'))
last_run_local = last_run_dt.astimezone(tz)
# If we ran within the last hour, skip
if (now - last_run_local).total_seconds() < 3600:
return False
except Exception as e:
logger.warning(f"Could not parse last_run '{last_run}': {e}")
# Check frequency-specific conditions
if frequency == 'daily':
return True
elif frequency == 'weekly':
target_dow = template.get('schedule_day_of_week', 0) # 0 = Monday
return now.weekday() == target_dow
elif frequency == 'monthly':
target_dom = template.get('schedule_day_of_month', 1)
return now.day == target_dom
return False
def _run_template(self, template: dict):
"""Execute report generation for a template."""
template_id = template['id']
try:
# Parse config
config = yaml.safe_load(template['config_yaml'])
region = config.get('global', {}).get('region', 'au1')
# Use provided API token
api_token = self.api_token
if not api_token:
logger.error(f"No API token available for template {template_id}")
self._record_failure(template_id, "No API token configured")
return
# Generate output path
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_name = template['name'].replace(' ', '_').replace('/', '_')
filename = f"{safe_name}_{timestamp}.pdf"
filepath = os.path.join(db.get_reports_directory(), filename)
# Ensure reports directory exists
os.makedirs(db.get_reports_directory(), exist_ok=True)
# Generate report
logger.info(f"Generating report: {filename}")
orientation = config.get('global', {}).get('orientation', 'portrait')
success, error = generate_report(
config=config,
output_path=filepath,
api_token=api_token,
region=region,
orientation=orientation
)
if success:
file_size = os.path.getsize(filepath)
db.create_generated_report(
template_id=template_id,
filename=filename,
file_path=filepath,
status="success",
file_size=file_size
)
logger.info(f"Report generated successfully: {filename} ({file_size} bytes)")
else:
self._record_failure(template_id, error, filename, filepath)
# Update last_run
db.update_template(template_id, schedule_last_run=datetime.now(timezone.utc).isoformat())
except Exception as e:
logger.error(f"Failed to run template {template_id}: {e}")
self._record_failure(template_id, str(e))
def _record_failure(self, template_id: int, error: str, filename: str = None, filepath: str = None):
"""Record a failed report generation."""
if filename is None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"failed_{template_id}_{timestamp}.pdf"
if filepath is None:
filepath = os.path.join(db.get_reports_directory(), filename)
db.create_generated_report(
template_id=template_id,
filename=filename,
file_path=filepath,
status="failed",
error_message=error[:500] # Truncate long errors
)
logger.error(f"Report generation failed for template {template_id}: {error}")
def get_scheduler() -> Optional[ReportScheduler]:
"""Get the global scheduler instance."""
return _scheduler_instance
def start_scheduler(api_token: str, check_interval_minutes: int = 60) -> ReportScheduler:
"""Start the global scheduler instance."""
global _scheduler_instance
with _scheduler_lock:
if _scheduler_instance is not None and _scheduler_instance.is_running():
logger.info("Scheduler already running, updating API token")
_scheduler_instance.api_token = api_token
return _scheduler_instance
_scheduler_instance = ReportScheduler(
check_interval_minutes=check_interval_minutes,
api_token=api_token
)
_scheduler_instance.start()
return _scheduler_instance
def stop_scheduler():
"""Stop the global scheduler instance."""
global _scheduler_instance
with _scheduler_lock:
if _scheduler_instance is not None:
_scheduler_instance.stop()
_scheduler_instance = None
def run_template_now(template_id: int, api_token: str) -> tuple[bool, Optional[str]]:
"""
Run a specific template immediately (ad-hoc execution).
Args:
template_id: ID of the template to run
api_token: API token for Sysdig API
Returns:
Tuple of (success, error_message)
"""
template = db.get_template(template_id)
if not template:
return False, "Template not found"
try:
config = yaml.safe_load(template['config_yaml'])
region = config.get('global', {}).get('region', 'au1')
orientation = config.get('global', {}).get('orientation', 'portrait')
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_name = template['name'].replace(' ', '_').replace('/', '_')
filename = f"{safe_name}_{timestamp}.pdf"
filepath = os.path.join(db.get_reports_directory(), filename)
os.makedirs(db.get_reports_directory(), exist_ok=True)
success, error = generate_report(
config=config,
output_path=filepath,
api_token=api_token,
region=region,
orientation=orientation
)
if success:
file_size = os.path.getsize(filepath)
db.create_generated_report(
template_id=template_id,
filename=filename,
file_path=filepath,
status="success",
file_size=file_size
)
return True, None
else:
db.create_generated_report(
template_id=template_id,
filename=filename,
file_path=filepath,
status="failed",
error_message=error
)
return False, error
except Exception as e:
return False, str(e)