добавил суперпольззователя, и запуск прям из окна создания задачи
All checks were successful
Deploy timelaps / deploy (push) Successful in 5s

This commit is contained in:
ack
2026-04-19 20:05:21 +03:00
parent a8be932210
commit 837876f9ed
8 changed files with 238 additions and 12 deletions

View File

@@ -64,7 +64,42 @@ def job_list(request):
if camera_id:
qs = qs.filter(camera_id=camera_id)
jobs = qs.order_by('-created_at')[:200]
return render(request, 'camlaps/job_list.html', {'jobs': jobs, 'queue_started': request.GET.get('started')})
return render(
request,
'camlaps/job_list.html',
{
'jobs': jobs,
'queue_started': request.GET.get('started'),
'retried': request.GET.get('retried'),
},
)
def worker_logs(request):
logger.info('logs:view:start')
log_file = settings.LOG_DIR / 'camlaps.log'
lines = []
if log_file.exists():
with log_file.open('r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()[-300:]
logger.info('logs:view:done lines=%s', len(lines))
return render(request, 'camlaps/worker_logs.html', {'log_lines': ''.join(lines)})
@require_POST
def clear_worker_logs(request):
logger.info('logs:clear:start')
try:
log_file = settings.LOG_DIR / 'camlaps.log'
log_file.parent.mkdir(parents=True, exist_ok=True)
log_file.write_text('', encoding='utf-8')
logger.info('logs:clear:done')
return redirect(f"{reverse('camlaps:worker_logs')}?cleared=1")
except Exception:
logger.exception('logs:clear:error')
return redirect(f"{reverse('camlaps:worker_logs')}?cleared=0")
@require_POST
@@ -96,6 +131,38 @@ def start_queue(request):
return redirect(f"{reverse('camlaps:job_list')}?started=error")
@require_POST
def retry_job(request, job_id: int):
logger.info('job:retry:start job_id=%s', job_id)
job = get_object_or_404(TimelapseJob, pk=job_id)
if job.status == TimelapseJob.Status.RUNNING:
logger.info('job:retry:done job_id=%s blocked=running', job_id)
return redirect(f"{reverse('camlaps:job_list')}?retried=running")
job.status = TimelapseJob.Status.PLANNED
job.progress_percent = 0
job.frames_total = None
job.frames_processed = 0
job.error_message = ''
job.started_at = None
job.finished_at = None
job.save(
update_fields=[
'status',
'progress_percent',
'frames_total',
'frames_processed',
'error_message',
'started_at',
'finished_at',
]
)
logger.info('job:retry:done job_id=%s', job_id)
return redirect(f"{reverse('camlaps:job_list')}?retried={job_id}")
def job_detail(request, job_id: int):
job = get_object_or_404(TimelapseJob.objects.select_related('camera'), pk=job_id)
video_url = None
@@ -106,7 +173,43 @@ def job_detail(request, job_id: int):
else:
video_url = '/timelapses/' + rel.split('/')[-1]
return render(request, 'camlaps/job_detail.html', {'job': job, 'video_url': video_url})
return render(
request,
'camlaps/job_detail.html',
{
'job': job,
'video_url': video_url,
'run_now': request.GET.get('run_now'),
},
)
@require_POST
def run_job_now(request, job_id: int):
logger.info('job:run_now:start job_id=%s', job_id)
job = get_object_or_404(TimelapseJob, pk=job_id)
if job.status == TimelapseJob.Status.RUNNING:
logger.info('job:run_now:done job_id=%s blocked=running', job_id)
return redirect(f"{reverse('camlaps:job_detail', kwargs={'job_id': job_id})}?run_now=running")
cmd = [sys.executable, 'manage.py', 'run_timelapse_worker', '--job-id', str(job_id)]
kwargs = {
'cwd': settings.BASE_DIR,
'stdout': subprocess.DEVNULL,
'stderr': subprocess.DEVNULL,
}
if sys.platform.startswith('win'):
kwargs['creationflags'] = subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP
try:
subprocess.Popen(cmd, **kwargs)
logger.info('job:run_now:done job_id=%s worker_started=true', job_id)
return redirect(f"{reverse('camlaps:job_detail', kwargs={'job_id': job_id})}?run_now=started")
except Exception:
logger.exception('job:run_now:error job_id=%s', job_id)
return redirect(f"{reverse('camlaps:job_detail', kwargs={'job_id': job_id})}?run_now=error")
def job_create(request, camera_id: int):