fina autoamtiona adn billing and credits
This commit is contained in:
@@ -474,3 +474,198 @@ class AutomationViewSet(viewsets.ViewSet):
|
||||
]
|
||||
})
|
||||
|
||||
@action(detail=False, methods=['get'], url_path='current_processing')
|
||||
def current_processing(self, request):
|
||||
"""
|
||||
GET /api/v1/automation/current_processing/?site_id=123&run_id=abc
|
||||
Get current processing state for active automation run
|
||||
"""
|
||||
site_id = request.query_params.get('site_id')
|
||||
run_id = request.query_params.get('run_id')
|
||||
|
||||
if not site_id or not run_id:
|
||||
return Response(
|
||||
{'error': 'site_id and run_id required'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
try:
|
||||
# Get the site
|
||||
site = get_object_or_404(Site, id=site_id, account=request.user.account)
|
||||
|
||||
# Get the run
|
||||
run = AutomationRun.objects.get(run_id=run_id, site=site)
|
||||
|
||||
# If not running, return None
|
||||
if run.status != 'running':
|
||||
return Response({'data': None})
|
||||
|
||||
# Get current processing state
|
||||
service = AutomationService.from_run_id(run_id)
|
||||
state = service.get_current_processing_state()
|
||||
|
||||
return Response({'data': state})
|
||||
|
||||
except AutomationRun.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Run not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{'error': str(e)},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
@action(detail=False, methods=['post'], url_path='pause')
|
||||
def pause_automation(self, request):
|
||||
"""
|
||||
POST /api/v1/automation/pause/?site_id=123&run_id=abc
|
||||
Pause current automation run
|
||||
|
||||
Will complete current queue item then pause before next item
|
||||
"""
|
||||
site_id = request.query_params.get('site_id')
|
||||
run_id = request.query_params.get('run_id')
|
||||
|
||||
if not site_id or not run_id:
|
||||
return Response(
|
||||
{'error': 'site_id and run_id required'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
try:
|
||||
site = get_object_or_404(Site, id=site_id, account=request.user.account)
|
||||
run = AutomationRun.objects.get(run_id=run_id, site=site)
|
||||
|
||||
if run.status != 'running':
|
||||
return Response(
|
||||
{'error': f'Cannot pause automation with status: {run.status}'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# Update status to paused
|
||||
run.status = 'paused'
|
||||
run.paused_at = timezone.now()
|
||||
run.save(update_fields=['status', 'paused_at'])
|
||||
|
||||
return Response({
|
||||
'message': 'Automation paused',
|
||||
'status': run.status,
|
||||
'paused_at': run.paused_at
|
||||
})
|
||||
|
||||
except AutomationRun.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Run not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{'error': str(e)},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
@action(detail=False, methods=['post'], url_path='resume')
|
||||
def resume_automation(self, request):
|
||||
"""
|
||||
POST /api/v1/automation/resume/?site_id=123&run_id=abc
|
||||
Resume paused automation run
|
||||
|
||||
Will continue from next queue item in current stage
|
||||
"""
|
||||
site_id = request.query_params.get('site_id')
|
||||
run_id = request.query_params.get('run_id')
|
||||
|
||||
if not site_id or not run_id:
|
||||
return Response(
|
||||
{'error': 'site_id and run_id required'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
try:
|
||||
site = get_object_or_404(Site, id=site_id, account=request.user.account)
|
||||
run = AutomationRun.objects.get(run_id=run_id, site=site)
|
||||
|
||||
if run.status != 'paused':
|
||||
return Response(
|
||||
{'error': f'Cannot resume automation with status: {run.status}'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# Update status to running
|
||||
run.status = 'running'
|
||||
run.resumed_at = timezone.now()
|
||||
run.save(update_fields=['status', 'resumed_at'])
|
||||
|
||||
# Queue continuation task
|
||||
from igny8_core.business.automation.tasks import continue_automation_task
|
||||
continue_automation_task.delay(run_id)
|
||||
|
||||
return Response({
|
||||
'message': 'Automation resumed',
|
||||
'status': run.status,
|
||||
'resumed_at': run.resumed_at
|
||||
})
|
||||
|
||||
except AutomationRun.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Run not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{'error': str(e)},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
@action(detail=False, methods=['post'], url_path='cancel')
|
||||
def cancel_automation(self, request):
|
||||
"""
|
||||
POST /api/v1/automation/cancel/?site_id=123&run_id=abc
|
||||
Cancel current automation run
|
||||
|
||||
Will complete current queue item then stop permanently
|
||||
"""
|
||||
site_id = request.query_params.get('site_id')
|
||||
run_id = request.query_params.get('run_id')
|
||||
|
||||
if not site_id or not run_id:
|
||||
return Response(
|
||||
{'error': 'site_id and run_id required'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
try:
|
||||
site = get_object_or_404(Site, id=site_id, account=request.user.account)
|
||||
run = AutomationRun.objects.get(run_id=run_id, site=site)
|
||||
|
||||
if run.status not in ['running', 'paused']:
|
||||
return Response(
|
||||
{'error': f'Cannot cancel automation with status: {run.status}'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# Update status to cancelled
|
||||
run.status = 'cancelled'
|
||||
run.cancelled_at = timezone.now()
|
||||
run.completed_at = timezone.now()
|
||||
run.save(update_fields=['status', 'cancelled_at', 'completed_at'])
|
||||
|
||||
return Response({
|
||||
'message': 'Automation cancelled',
|
||||
'status': run.status,
|
||||
'cancelled_at': run.cancelled_at
|
||||
})
|
||||
|
||||
except AutomationRun.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Run not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{'error': str(e)},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user