gpt4 book ai didi

django - ec2上的Django celery worker错误尝试使用systemd启动守护进程

转载 作者:行者123 更新时间:2023-12-03 10:00:17 28 4
gpt4 key购买 nike

我正在尝试将celery用作我的ec2服务器上的守护程序,并且到目前为止一直在关注this教程。每次我尝试启动celery worker守护程序时,日志都会返回以下错误:

systemd[1]: celery.service: Failed to load environment files: No such file or directory
systemd[1]: celery.service: Failed to run 'start' task: No such file or directory
systemd[1]: celery.service: Failed with result 'resources'.
systemd[1]: Failed to start Celery Service.
当我使用
celery -A project_name worker --loglevel=INFO
celery 按预期运行,但是当我注销服务器时会关闭。我在这里是一个完整的初学者,希望能帮助您解决此错误。下面提供了所有相关文件:
celery.conf
# celery.conf

CELERYD_NODES="w1"
CELERY_BIN="path/to/project/directory/env/bin/celery"
CELERY_APP="project_name"
CELERYD_MULTI="multi"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
celery 服务
[Unit]
Description=Celery Service
After=network.target

[Service]
Type=forking
User=username
Group=groupname
EnvironmentFile=/path/to/environmentfile/celery.conf
WorkingDirectory=path/to/project/directory/env/bin/
ExecStart=/bin/sh -c '${CELERY_BIN} -A ${CELERY_APP} multi start ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE}'
ExecReload=/bin/sh -c '${CELERY_BIN} -A ${CELERY_APP} multi restart ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'

[Install]
WantedBy=multi-user.target
django设置:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'profiles_api',
"push_notifications",

]

REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
]
}


MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'profiles_project.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'profiles_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name',
'USER': 'name',
'PASSWORD': '***',
'HOST': '', # Or an IP Address that your DB is hosted on
'PORT': '',
},
}
#

# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

AUTH_USER_MODEL = 'profiles_api.UserProfile'

STATIC_ROOT = 'static/'

# celery
BROKER_URL = 'redis://localhost:5555'
RESULT_BACKEND = 'redis://localhost:5555'
ACCEPT_CONTENT = ['application/json']
RESULT_SERIALIZER = 'json'
TASK_SERIALIZER = 'json'

最佳答案

我已通过编辑celery.service文件使其看起来像这样来解决此问题
celery 服务

[Unit]
Description=Celery Service
After=network.target

[Service]
#Type=forking
User=username
Group=groupname
KillMode=control-group
RemainAfterExit=yes
WorkingDirectory=path/to/project/directory
Restart=yes
#PassEnvironment=CELERYD_NODES CELERY_BIN CELERY_APP CELERYD_MULTI CELERYD_PID_FILE CELERYD_LOG_FILE CELERYD_LOG_LEVEL

ExecStart=path/to/project/directory/env/bin/celery -A project_name worker --loglevel=INFO

ExecStop=path/to/project/directory/env/bin/celery -A project_name worker --loglevel=INFO



ExecReload=path/to/project/directory/env/bin/celery -A project_name worker --loglevel=INFO


[Install]
WantedBy=multi-user.target

关于django - ec2上的Django celery worker错误尝试使用systemd启动守护进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64300287/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com