예제 #1
0
파일: worker.py 프로젝트: wibbit/pulpcore
    def perform_job(self, job, queue):
        """
        Set the :class:`pulpcore.app.models.Task` to running and install a kill monitor Thread

        This method is called by the worker's work horse thread (the forked child) just before the
        task begins executing. It creates a Thread which monitors a special Redis key which if
        created should kill the task with SIGKILL.

        Args:
            job (rq.job.Job): The job to perform
            queue (rq.queue.Queue): The Queue associated with the job
        """
        try:
            task = Task.objects.get(pk=job.get_id())
        except Task.DoesNotExist:
            pass
        else:
            task.set_running()
            user = get_users_with_perms(task).first()
            _set_current_user(user)

        def check_kill(conn, id, interval=1):
            while True:
                res = conn.srem(TASKING_CONSTANTS.KILL_KEY, id)
                if res > 0:
                    os.kill(os.getpid(), signal.SIGKILL)
                time.sleep(interval)

        t = threading.Thread(target=check_kill,
                             args=(self.connection, job.get_id()))
        t.start()

        return super().perform_job(job, queue)
예제 #2
0
    def test_on_update_enabled(self):
        _set_current_user(None)
        test_model = TestModelOnUpdate()
        test_model.save()

        self.assertIs(test_model.updated_by_id, None)
        test_model.refresh_from_db()
        self.assertIs(test_model.updated_by, None)

        self.login_and_go_to_homepage(username="******", password="******")
        test_model.save()

        self.assertEqual(self.user1.pk, test_model.updated_by_id)
        test_model.refresh_from_db()
        self.assertEqual(self.user1, test_model.updated_by)

        self.login_and_go_to_homepage(username="******", password="******")
        test_model.save()

        self.assertEqual(self.user2.pk, test_model.updated_by_id)
        test_model.refresh_from_db()
        self.assertEqual(self.user2, test_model.updated_by)

        _set_current_user(None)
        test_model.save()

        self.assertIs(test_model.updated_by_id, None)
        test_model.refresh_from_db()
        self.assertIs(test_model.updated_by, None)
예제 #3
0
파일: worker.py 프로젝트: gerrod3/pulpcore
    def perform_job(self, job, queue):
        """
        Set the :class:`pulpcore.app.models.Task` to running and init logging.

        This method is called by the worker's work horse thread (the forked child) just before the
        task begins executing.

        Args:
            job (rq.job.Job): The job to perform
            queue (rq.queue.Queue): The Queue associated with the job
        """
        try:
            task = Task.objects.get(pk=job.get_id())
        except Task.DoesNotExist:
            pass
        else:
            if task.state != TASK_STATES.WAITING:
                return
            task.set_running()
            user = get_users_with_perms(task).first()
            _set_current_user(user)
            set_guid(task.logging_cid)

        with TaskWorkingDirectory(job):
            return super().perform_job(job, queue)
예제 #4
0
 def basic_setup(cls):
     cls.case_worker, _ = User.objects.get_or_create(
         username="******")
     _set_current_user(cls.case_worker)
     cls.team, _ = Team.objects.get_or_create(name="FCK",
                                              leader=cls.case_worker)
     cls.case_worker.team = cls.team
     cls.case_worker.save()
     cls.municipality, _ = Municipality.objects.get_or_create(
         name="København")
     cls.district, _ = SchoolDistrict.objects.get_or_create(
         name="Baltorpskolen")
예제 #5
0
 def setUp(self):
     """Set up the ContainerDistributionSerializer tests."""
     self.mirror_repository, _ = ContainerRepository.objects.get_or_create(
         name="mirror repository", )
     self.mirror_repository_href = "/pulp/api/v3/repositories/container/container/{}/".format(
         self.mirror_repository.pk)
     self.push_repository, _ = ContainerPushRepository.objects.get_or_create(
         name="push repository", )
     self.push_repository_href = "/pulp/api/v3/repositories/container/container-push/{}/".format(
         self.push_repository.pk)
     self.user = get_user_model().objects.create(username="******",
                                                 is_staff=False)
     _set_current_user(self.user)
예제 #6
0
    def test__local_thread_var_is_set_to_logged_in_user(self):
        _set_current_user(None)
        self.assertIsNone(get_current_user())

        self.login_and_go_to_homepage(username="******", password="******")
        self.assertEqual(self.user1, get_current_user())
        self.client.logout()

        self.login_and_go_to_homepage(username="******", password="******")
        self.assertEqual(self.user2, get_current_user())
        self.client.logout()

        self.client.get("/")
        current_user = get_current_user()
        assert_that(current_user, instance_of(AnonymousUser))
예제 #7
0
def _perform_task(task_pk, task_working_dir_rel_path):
    """Setup the environment to handle a task and execute it.
    This must be called as a subprocess, while the parent holds the advisory lock."""
    signal.signal(signal.SIGINT, child_signal_handler)
    signal.signal(signal.SIGTERM, child_signal_handler)
    signal.signal(signal.SIGUSR1, child_signal_handler)
    # All processes need to create their own postgres connection
    connection.connection = None
    task = Task.objects.get(pk=task_pk)
    task.set_running()
    # Store the task id in the environment for `Task.current()`.
    os.environ["PULP_TASK_ID"] = str(task.pk)
    user = get_users_with_perms(task).first()
    _set_current_user(user)
    set_guid(task.logging_cid)
    try:
        _logger.info("Starting task {}".format(task.pk))

        # Execute task
        module_name, function_name = task.name.rsplit(".", 1)
        module = importlib.import_module(module_name)
        func = getattr(module, function_name)
        args = json.loads(task.args) or ()
        kwargs = json.loads(task.kwargs) or {}
        os.chdir(task_working_dir_rel_path)
        result = func(*args, **kwargs)
        if asyncio.iscoroutine(result):
            _logger.debug("Task is coroutine {}".format(task.pk))
            loop = asyncio.get_event_loop()
            loop.run_until_complete(result)

    except Exception:
        exc_type, exc, tb = sys.exc_info()
        task.set_failed(exc, tb)
        _logger.info("Task {} failed ({})".format(task.pk, exc))
        _logger.info("\n".join(traceback.format_list(
            traceback.extract_tb(tb))))
    else:
        task.set_completed()
        _logger.info("Task completed {}".format(task.pk))
    os.environ.pop("PULP_TASK_ID")
예제 #8
0
파일: worker.py 프로젝트: lboclboc/pulpcore
    def perform_job(self, job, queue):
        """
        Set the :class:`pulpcore.app.models.Task` to running and install a kill monitor Thread

        This method is called by the worker's work horse thread (the forked child) just before the
        task begins executing. It creates a Thread which monitors a special Redis key which if
        created should kill the task with SIGKILL.

        Args:
            job (rq.job.Job): The job to perform
            queue (rq.queue.Queue): The Queue associated with the job
        """
        try:
            task = Task.objects.get(pk=job.get_id())
        except Task.DoesNotExist:
            pass
        else:
            task.set_running()
            user = get_users_with_perms(task).first()
            _set_current_user(user)

        return super().perform_job(job, queue)
예제 #9
0
    def test_on_update_disabled(self):
        self.login_and_go_to_homepage(username="******", password="******")
        test_model = TestModelDefaultBehavior()
        test_model.save()

        self.assertEqual(self.user1.pk, test_model.created_by_id)
        test_model.refresh_from_db()
        self.assertEqual(self.user1, test_model.created_by)

        self.login_and_go_to_homepage(username="******", password="******")
        test_model.save()

        self.assertEqual(self.user1.pk, test_model.created_by_id)
        test_model.refresh_from_db()
        self.assertEqual(self.user1, test_model.created_by)

        _set_current_user(None)
        test_model.save()

        self.assertEqual(self.user1.pk, test_model.created_by_id)
        test_model.refresh_from_db()
        self.assertEqual(self.user1, test_model.created_by)
예제 #10
0
 def tearDown(self):
     """Delete the user."""
     super().tearDown()
     self.user.delete()
     _set_current_user(None)
예제 #11
0
 def assert_becomes(self, current_user, expected_thread_user):
     _set_current_user(current_user)
     assert_that(get_current_authenticated_user(),
                 equal_to(expected_thread_user))
예제 #12
0
 def tearDown(self):
     super(TestUserBase, self).tearDown()
     _set_current_user(None)
예제 #13
0
 def tearDown(self):
     super(TestSetUserToThread, self).tearDown()
     _set_current_user(None)