<commit_before>import json
from abc import abstractmethod, ABCMeta
from six import add_metaclass
from datetime import datetime

from sqjobs import Job
from sqjobs.contrib.django.djsqjobs.models import JobStatus


class DuplicatedTaskException(Exception):

    def __init__(self, message, errors=None):
        super(DuplicatedTaskException, self).__init__(message)
        self.errors = errors


@add_metaclass(ABCMeta)
class ResultJob(Job):

    def __init__(self):
        self.properly_setup = False

    def set_up(self, *args, **kwargs):
        super(ResultJob, self).set_up(*args, **kwargs)
        # Take as id the left part from the hash symbol
        task_id = self.id.split('#')[0]
        try:
            self.job_status = JobStatus(
                job_id=task_id,
                job_name=self.__repr__()
            )
            self.job_status.save(force_insert=True)
            self.properly_setup = True
        except:
            self.job_status = JobStatus.objects.get(job_id=task_id)
            if self.job_status.status == JobStatus.FAILURE:
                self.job_status.status = JobStatus.PENDING
                self.job_status.save()
                self.properly_setup = True
            else:
                raise DuplicatedTaskException('Task duplicated: %s' % task_id)
<commit_msg>Improved exception handling of repeated tasks<commit_after>import json
from abc import abstractmethod, ABCMeta
from six import add_metaclass
from datetime import datetime

from django.db import IntegrityError

from sqjobs import Job
from sqjobs.contrib.django.djsqjobs.models import JobStatus


class DuplicatedTaskException(Exception):

    def __init__(self, message, errors=None):
        super(DuplicatedTaskException, self).__init__(message)
        self.errors = errors


@add_metaclass(ABCMeta)
class ResultJob(Job):

    def __init__(self):
        self.properly_setup = False

    def set_up(self, *args, **kwargs):
        super(ResultJob, self).set_up(*args, **kwargs)
        # Take as id the left part from the hash symbol
        task_id = self.id.split('#')[0]
        try:
            self.job_status = JobStatus(
                job_id=task_id,
                job_name=self.__repr__()
            )
            self.job_status.save(force_insert=True)
            self.properly_setup = True
        except IntegrityError:
            self.job_status = JobStatus.objects.get(job_id=task_id)
            if self.job_status.status == JobStatus.FAILURE:
                self.job_status.status = JobStatus.PENDING
                self.job_status.save()
                self.properly_setup = True
            else:
                raise DuplicatedTaskException('Task duplicated: %s' % task_id)

{'fraction_non_alphanumeric': 0.08477064220183486, 'fraction_numerical': 0.0007339449541284404, 'mean_word_length': 2.463786531130877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2484467', 'n_tokens_mistral': 803, 'n_tokens_neox': 771, 'n_words': 178}