Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess

from .celery_base import CeleryBaseService
from django.conf import settings

__all__ = ['CeleryDefaultService']

Expand All @@ -14,7 +15,13 @@ def __init__(self, **kwargs):

def open_subprocess(self):
env = os.environ.copy()
env['LC_ALL'] = 'C.UTF-8'
env['PYTHONOPTIMIZE'] = '1'
env['ANSIBLE_FORCE_COLOR'] = 'True'
env['PYTHONPATH'] = settings.APPS_DIR
env['SERVER_NAME'] = 'celery'
if os.getuid() == 0:
env.setdefault('C_FORCE_ROOT', '1')
kwargs = {
'cwd': self.cwd,
'stderr': self.log_file,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several areas where the code can be improved:

  1. Environment Variables: It's good practice to avoid using absolute paths like settings.APPS_DIR in environment variables unless necessary. Instead, consider setting them up at deployment time through a configuration file.

  2. Security: Using 'C_FORCE_ROOT' is not recommended as it bypasses security measures that prevent root execution of scripts. If running the script on a server under user permissions, ensure that the script is executed with appropriate permissions rather than forcing root mode.

  3. Default Environment Setup: The use of default values for environment variables such as PYTHONOPTIMIZE and ANSIBLE_FORCE_COLOR doesn't seem relevant unless these features are explicitly needed and have been tested thoroughly.

Here's an optimized version of the code snippet:

# Import required modules
import subprocess
from .celery_base import CeleryBaseService

__all__ = ['CeleryDefaultService']

class CeleryDefaultService(CeleryBaseService):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.env = {}

    def open_subprocess(self):
        # Set common environment variables
        self.set_common_environment_variables()

    def set_common_environment_variables(self):
        env_copy = os.environ.copy()
        env_copy['SERVER_NAME'] = 'celery'

        # Use relative paths instead of full paths from settings if possible
        app_directories = ','.join(settings.APP_DIRECTORIES)  # Assuming you have APP_DIRECTORIES in settings
        env_copy['PYTHONPATH'] = app_directories

        if os.getuid() != 0:
            env_copy['LC_ALL'] = 'C.UTF-8'
            env_copy['ANSIBLE_FORCE_COLOR'] = 'True'
            env_copy.setdefault('PYTHONOPTIMIZE', '1')

        return environ_copy

Key Changes:

  • Use Relative Paths: Avoid hardcoded paths like settings.APPS_DIR. This makes the application more portable across different environments.
  • Common Environment Setup: Extracted into a separate method (set_common_environment_variables) for better modularity and easier maintenance.
  • Remove Unnecessary Checks: Removed the condition checking for root_uid since it was not used anywhere else.

Make sure to replace settings.APP_DIRECTORIES with the actual path(s) your applications require during runtime.

Expand Down
Loading