Celery configuration
app.config.update(
broker_url='redis://{}:{}/0'.format(redis_host,redis_port),
result_backend='redis://{}:{}/0'.format(redis_host,redis_port)
)
stol_distance = 1 # STOL distance 1 km
stol_speed = 50 # STOL speed 50 nautical miles
def make_celery(app):
celery = Celery(app.import_name, broker=app.config['broker_url'])
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
celery_app = make_celery(app)
Configure logging
log_folder = 'logs'
log_file = os.path.join(log_folder, f'pod_task_{datetime.now():%Y-%m-%d}.log')
log_level = logging.DEBUG
Create the log folder if it doesn't exist
if not os.path.exists(log_folder):
os.makedirs(log_folder)
Use RotatingFileHandler for log rotation with negative backupCount
handler = RotatingFileHandler(log_file, maxBytes=10 * 1024 * 1024, backupCount=-1)
handler.setLevel(log_level)
Create a log format with a timestamp
log_format = '%(asctime)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
app.logger.addHandler(handler)
@celery_app.task
def pod_task():
today = datetime.today()
previous_date = (today - timedelta(days=1)).strftime('%Y-%m-%d')
app.logger.info(f"Task Started For {previous_date}")
app.logger.error(f"Exception in task")
app.logger.info(f"Task Ended")
.....
.....
Schedule the task to run every day at 12:05 AM using Celery Beat
celery_app.conf.beat_schedule = {
'daily-task-scheduler': {
'task': 'celery_config.pod_task',
'schedule': crontab(hour=0, minute=5), # Schedule the task to run every day at 12:05 AM
},
}
Here is my docker compose file.
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- ${APP_PORT}:5000
volumes:
- .:/app
env_file: .env
networks:
- app-network
depends_on:
- db
db:
image: postgres:latest
env_file: .env
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- ./pgdata:/var/lib/postgresql/data
networks:
- app-network
pgadmin:
image: dpage/pgadmin4
env_file:
- .env
ports:
- ${PGADMIN_PORT}:80
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
volumes:
- ./pgadmin_data:/var/lib/
depends_on:
- db
networks:
- app-network
celery_worker:
build:
context: .
dockerfile: Dockerfile
command: 'celery -A celery_config.celery_app worker --loglevel=info -c 8'
depends_on:
- redis
- web
- db
networks:
- app-network
celery_beat:
build:
context: .
dockerfile: Dockerfile
command: 'celery -A celery_config.celery_app beat --loglevel=info'
depends_on:
- redis
- web
- db
networks:
- app-network
redis:
image: redis
env_file:
- .env
networks:
- app-network
ports:
- 6379:6379
volumes:
pgdata:
pgadmin_data:
networks:
app-network:
driver: bridge
Everything is working fine when I run the application without docker. But when I try to run the same thing with docker, folder and file create as expected, but the file is not modifying which supposed to get modified with some data.
当我在没有docker的情况下运行应用程序时,一切都很好。但是,当我尝试用docker运行相同的东西时,文件夹和文件创建如预期,但文件没有修改,应该用一些数据进行修改。
更多回答
我是一名优秀的程序员,十分优秀!