コード例 #1
0
ファイル: journal_bulk_delete.py プロジェクト: DOAJ/doaj
    def prepare(cls, username, **kwargs):
        """
        Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
        or fail with a suitable exception

        :param kwargs: arbitrary keyword arguments pertaining to this task type
        :return: a BackgroundJob instance representing this task
        """

        super(JournalBulkDeleteBackgroundTask, cls).prepare(username, **kwargs)

        # first prepare a job record
        job = models.BackgroundJob()
        job.user = username
        job.action = cls.__action__
        job.reference = {'selection_query': json.dumps(kwargs['selection_query'])}

        params = {}
        cls.set_param(params, 'ids', kwargs['ids'])

        if not cls._job_parameter_check(params):
            raise BackgroundException(u"{}.prepare run without sufficient parameters".format(cls.__name__))

        job.params = params

        # now ensure that we have the locks for all the records, if they are lockable
        # will raise an exception if this fails
        lock.batch_lock('journal', kwargs['ids'], username, timeout=app.config.get("BACKGROUND_TASK_LOCK_TIMEOUT", 3600))

        return job
コード例 #2
0
ファイル: journal_in_out_doaj.py プロジェクト: DOAJ/doaj
    def prepare(cls, username, **kwargs):
        """
        Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
        or fail with a suitable exception

        :param kwargs: arbitrary keyword arguments pertaining to this task type
        :return: a BackgroundJob instance representing this task
        """

        # first prepare a job record
        job = models.BackgroundJob()
        job.user = username
        job.action = cls.__action__

        journal_ids = kwargs.get("journal_ids")

        params = {}
        cls.set_param(params, "journal_ids", journal_ids)
        cls.set_param(params, "in_doaj", kwargs.get("in_doaj"))

        if journal_ids is None or len(journal_ids) == 0 or kwargs.get("in_doaj") is None:
            raise RuntimeError(u"SetInDOAJBackgroundTask.prepare run without sufficient parameters")

        job.params = params

        if "selection_query" in kwargs:
            refs = {}
            cls.set_reference(refs, "selection_query", json.dumps(kwargs.get('selection_query')))
            job.reference = refs

        # now ensure that we have the locks for this journal
        # will raise an exception if this fails
        lock.batch_lock("journal", journal_ids, username, timeout=app.config.get("BACKGROUND_TASK_LOCK_TIMEOUT", 3600))

        return job
コード例 #3
0
    def prepare(cls, username, **kwargs):
        """
        Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
        or fail with a suitable exception

        :param kwargs: arbitrary keyword arguments pertaining to this task type
        :return: a BackgroundJob instance representing this task
        """

        super(JournalBulkDeleteBackgroundTask, cls).prepare(username, **kwargs)

        # first prepare a job record
        job = models.BackgroundJob()
        job.user = username
        job.action = cls.__action__
        job.reference = {'selection_query': json.dumps(kwargs['selection_query'])}

        params = {}
        cls.set_param(params, 'ids', kwargs['ids'])

        if not cls._job_parameter_check(params):
            raise BackgroundException("{}.prepare run without sufficient parameters".format(cls.__name__))

        job.params = params

        # now ensure that we have the locks for all the records, if they are lockable
        # will raise an exception if this fails
        lock.batch_lock('journal', kwargs['ids'], username, timeout=app.config.get("BACKGROUND_TASK_LOCK_TIMEOUT", 3600))

        return job
コード例 #4
0
    def prepare(cls, username, **kwargs):
        """
        Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
        or fail with a suitable exception

        :param kwargs: arbitrary keyword arguments pertaining to this task type
        :return: a BackgroundJob instance representing this task
        """

        super(JournalBulkEditBackgroundTask, cls).prepare(username, **kwargs)

        # first prepare a job record
        job = models.BackgroundJob()
        job.user = username
        job.action = cls.__action__

        refs = {}
        cls.set_reference(refs, "selection_query",
                          json.dumps(kwargs['selection_query']))
        job.reference = refs

        params = {}

        # get the named parameters we know may be there
        cls.set_param(params, 'ids', kwargs['ids'])
        if "note" in kwargs and kwargs[
                "note"] is not None and kwargs["note"] != "":
            cls.set_param(params, 'note', kwargs.get('note', ''))

        # get the metadata overwrites
        if "replacement_metadata" in kwargs:
            metadata = {}
            for k, v in kwargs["replacement_metadata"].items():
                if v is not None and v != "":
                    metadata[k] = v
            if len(metadata.keys()) > 0:
                cls.set_param(params, 'replacement_metadata',
                              json.dumps(metadata))

        if not cls._job_parameter_check(params):
            raise BackgroundException(
                "{}.prepare run without sufficient parameters".format(
                    cls.__name__))

        job.params = params

        # now ensure that we have the locks for all the journals
        # will raise an exception if this fails
        lock.batch_lock("journal",
                        kwargs['ids'],
                        username,
                        timeout=app.config.get("BACKGROUND_TASK_LOCK_TIMEOUT",
                                               3600))

        return job
コード例 #5
0
    def test_03_batch_lock_unlock(self):
        source = JournalFixtureFactory.make_journal_source()
        ids = []

        # create a bunch of journals that we can play with
        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        time.sleep(2)

        ls = lock.batch_lock("journal", ids, "testuser")
        assert len(ls) == 5

        time.sleep(2)

        report = lock.batch_unlock("journal", ids, "testuser")
        assert len(report["success"]) == 5
        assert len(report["fail"]) == 0

        time.sleep(2)

        # now lock an individual record by a different user and check that no locks are set
        # in batch
        l = lock.lock("journal", ids[3], "otheruser")

        time.sleep(2)

        with self.assertRaises(lock.Locked):
            ls = lock.batch_lock("journal", ids, "testuser")

        for id in ids:
            assert lock.has_lock("journal", id, "testuser") is False
コード例 #6
0
ファイル: test_lock.py プロジェクト: DOAJ/doaj
    def test_03_batch_lock_unlock(self):
        source = JournalFixtureFactory.make_journal_source()
        ids = []

        # create a bunch of journals that we can play with
        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        j = models.Journal(**deepcopy(source))
        j.save()
        ids.append(j.id)

        time.sleep(2)

        ls = lock.batch_lock("journal", ids, "testuser")
        assert len(ls) == 5

        time.sleep(2)

        report = lock.batch_unlock("journal", ids, "testuser")
        assert len(report["success"]) == 5
        assert len(report["fail"]) == 0

        time.sleep(2)

        # now lock an individual record by a different user and check that no locks are set
        # in batch
        l = lock.lock("journal", ids[3], "otheruser")

        time.sleep(2)

        with self.assertRaises(lock.Locked):
            ls = lock.batch_lock("journal", ids, "testuser")

        for id in ids:
            assert lock.has_lock("journal", id, "testuser") is False
コード例 #7
0
    def prepare(cls, username, **kwargs):
        """
        Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
        or fail with a suitable exception

        :param kwargs: arbitrary keyword arguments pertaining to this task type
        :return: a BackgroundJob instance representing this task
        """

        # first prepare a job record
        job = models.BackgroundJob()
        job.user = username
        job.action = cls.__action__

        journal_ids = kwargs.get("journal_ids")

        params = {}
        cls.set_param(params, "journal_ids", journal_ids)
        cls.set_param(params, "in_doaj", kwargs.get("in_doaj"))

        if journal_ids is None or len(
                journal_ids) == 0 or kwargs.get("in_doaj") is None:
            raise RuntimeError(
                u"SetInDOAJBackgroundTask.prepare run without sufficient parameters"
            )

        job.params = params

        if "selection_query" in kwargs:
            refs = {}
            cls.set_reference(refs, "selection_query",
                              json.dumps(kwargs.get('selection_query')))
            job.reference = refs

        # now ensure that we have the locks for this journal
        # will raise an exception if this fails
        lock.batch_lock("journal",
                        journal_ids,
                        username,
                        timeout=app.config.get("BACKGROUND_TASK_LOCK_TIMEOUT",
                                               3600))

        return job