def check_project_tasks(): """ Checks to see if any incomplete projects do not currently have any running celery tasks associated with them. This can happen if e.g. the server is reset in the middle of a check. If one is found, the check is restarted. This is intended to be a regular check (via celerybeat). """ projects = UserProject.objects.all() tl = get_task_list() for project in projects: if not project.state in [ u'measuring', u'completed', u'error', u'paused' ] and not is_project_task_active(project, tl): async_result = check_project_domains.delay(project.id) task_id = async_result.id print task_id project_task = ProjectTask() project_task.project_id = project.id project_task.celery_id = task_id project_task.type = u'checker' project_task.save() print u'Restarted task for project %d (task id: %s)' % (project.id, task_id)
def check_project_tasks(): """ Checks to see if any incomplete projects do not currently have any running celery tasks associated with them. This can happen if e.g. the server is reset in the middle of a check. If one is found, the check is restarted. This is intended to be a regular check (via celerybeat). """ projects = UserProject.objects.all() tl = get_task_list() for project in projects: if not project.state in [u'measuring', u'completed', u'error', u'paused'] and not is_project_task_active(project, tl): async_result = check_project_domains.delay(project.id) task_id = async_result.id print task_id project_task = ProjectTask() project_task.project_id = project.id project_task.celery_id = task_id project_task.type = u'checker' project_task.save() print u'Restarted task for project %d (task id: %s)' % (project.id, task_id)
def upload_project(request): """ View: Processes new project file upload of an authenticated user. Redirects to index page if unauthenticated. Otherwise once the file is parsed for domains to process, the user is redirected back to their project listing with an appropriate success or failure message. """ if not request.user.is_authenticated(): logger.debug('Unauthenticated user.') return redirect('index') project = None if request.method == 'POST': uploadform = URLFileForm(request.POST, request.FILES) logger.debug('Attempting to upload project...') if uploadform.is_valid(): logger.debug('Form is valid.') file_contents = unicode(request.FILES['file'].read(), errors='ignore') (domain_list, failed_domains, failed_lines) = extract_domains(file_contents, request.user.email, request.FILES['file'].name) projectdomains = [] with transaction.atomic(): project = UserProject(state='checking', last_updated=timezone.now(), user_id=request.user.id) parse_error_str = '' # If any lines failed, track them if len(failed_lines) > 0: for fd in failed_lines: parse_error_str += '%d: %s (%s)\n' % (fd[0], fd[1], fd[2]) project.parse_errors = parse_error_str project.save() # Store uploaded file in database projectfile = UploadedFile(filename=request.FILES['file'].name, filedata=file_contents, project_id=project.id) # Create and associate project domain records for domain in domain_list: projectdomains.append( ProjectDomain(domain=domain, subdomains_preserved=False, is_checked=False, state='unchecked', last_checked=timezone.now(), project_id=project.id)) for (domain, state, error) in failed_domains: projectdomains.append( ProjectDomain(domain=domain, subdomains_preserved=False, is_checked=True, state=state, last_checked=timezone.now(), project_id=project.id, error=error)) projectfile.save() [pd.save() for pd in projectdomains] # Initial celery task to check availability of parsed domains if project.projectdomain_set.exclude(state='error').count() == 0: project.state = 'completed' project.save() request.session[ 'profile_message'] = 'Project "%s" successfully uploaded but no valid domains were found for checking.' % request.FILES[ 'file'].name request.session['profile_messagetype'] = 'warning' else: task_id = check_project_domains.delay(project.id) project_task = ProjectTask() project_task.project_id = project.id project_task.celery_id = task_id project_task.type = 'checker' project_task.save() request.session[ 'profile_message'] = 'Project "%s" successfully uploaded. You will be emailed when domain checking is complete.' % request.FILES[ 'file'].name request.session['profile_messagetype'] = 'success' return redirect('project_list') else: # If an error occurred, inform the user logger.debug(uploadform.errors) request.session[ 'profile_message'] = '<b>Project not uploaded.</b> The following errors occurred: %s' % uploadform.errors request.session['profile_messagetype'] = 'danger' return redirect('project_list')
def upload_project(request): """ View: Processes new project file upload of an authenticated user. Redirects to index page if unauthenticated. Otherwise once the file is parsed for domains to process, the user is redirected back to their project listing with an appropriate success or failure message. """ if not request.user.is_authenticated(): logger.debug('Unauthenticated user.') return redirect('index') project = None if request.method == 'POST': uploadform = URLFileForm(request.POST, request.FILES) logger.debug('Attempting to upload project...') if uploadform.is_valid(): logger.debug('Form is valid.') file_contents = unicode(request.FILES['file'].read(), errors='ignore') (domain_list, failed_domains, failed_lines) = extract_domains(file_contents, request.user.email, request.FILES['file'].name) projectdomains = [] with transaction.atomic(): project = UserProject(state='checking', last_updated=timezone.now(), user_id=request.user.id) parse_error_str = '' # If any lines failed, track them if len(failed_lines) > 0: for fd in failed_lines: parse_error_str += '%d: %s (%s)\n' % (fd[0], fd[1], fd[2]) project.parse_errors = parse_error_str project.save() # Store uploaded file in database projectfile = UploadedFile(filename=request.FILES['file'].name, filedata=file_contents, project_id=project.id) # Create and associate project domain records for domain in domain_list: projectdomains.append(ProjectDomain(domain=domain, subdomains_preserved=False, is_checked=False, state='unchecked', last_checked=timezone.now(), project_id=project.id)) for (domain, state, error) in failed_domains: projectdomains.append(ProjectDomain(domain=domain, subdomains_preserved=False, is_checked=True, state=state, last_checked=timezone.now(), project_id=project.id, error=error)) projectfile.save() [pd.save() for pd in projectdomains] # Initial celery task to check availability of parsed domains if project.projectdomain_set.exclude(state='error').count() == 0: project.state = 'completed' project.save() request.session['profile_message'] = 'Project "%s" successfully uploaded but no valid domains were found for checking.' % request.FILES['file'].name request.session['profile_messagetype'] = 'warning' else: task_id = check_project_domains.delay(project.id) project_task = ProjectTask() project_task.project_id = project.id project_task.celery_id = task_id project_task.type = 'checker' project_task.save() request.session['profile_message'] = 'Project "%s" successfully uploaded. You will be emailed when domain checking is complete.' % request.FILES['file'].name request.session['profile_messagetype'] = 'success' return redirect('project_list') else: # If an error occurred, inform the user logger.debug(uploadform.errors) request.session['profile_message'] = '<b>Project not uploaded.</b> The following errors occurred: %s' % uploadform.errors request.session['profile_messagetype'] = 'danger' return redirect('project_list')