Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update Heroku deployment for Celery and Redis integration #2600

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

areebahmeddd
Copy link
Contributor

@areebahmeddd areebahmeddd commented Nov 15, 2024

Proposed Changes

  • Added support to run the backend along with Celery on Heroku.
  • Added app specification for database and Redis in app.json.
  • Updated Procfile to include Celery worker and beat.
  • Updated documentation to reflect the new setup for Heroku deployment.

Associated Issue

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Any other necessary step

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • New Features

    • Introduced background task management with Celery, enabling asynchronous processing and scheduled tasks.
    • Added a dedicated "production" environment configuration for improved deployment setup.
  • Documentation

    • Updated setup documentation to include a new section on deploying the backend with Celery on Heroku, providing detailed steps and commands for developers.

Copy link

coderabbitai bot commented Nov 15, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes include updates to the Procfile, app.json, and documentation for deploying the backend with Celery on Heroku. The Procfile now specifies commands for a Celery worker and beat scheduler, enhancing background task management. The app.json introduces a new "production" environment with a post-deploy script and relevant environment variables. Lastly, the documentation has been expanded to include a section on deploying with Celery on Heroku, providing developers with necessary steps and configurations.

Changes

File Change Summary
Procfile Added entries for worker and beat to manage Celery processes with config.celery_app.
app.json Introduced a new "production" environment with a postdeploy script and several environment variables.
docs/local-setup/configuration.rst Added a new section on deploying the backend with Celery on Heroku, detailing installation and configuration steps.

Poem

In the land of code, where tasks do flow,
Celery workers now put on a show.
Production's ready, with scripts in tow,
Deploy on Heroku, let the data grow!
With every change, our app will glow! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

🧹 Outside diff range and nitpick comments (4)
Procfile (1)

3-3: Consider adding worker performance configurations

The worker configuration could benefit from explicit concurrency and memory settings. You know, just to avoid those... unfortunate production surprises.

Consider updating to:

-worker: celery -A config.celery_app worker --loglevel=info
+worker: celery -A config.celery_app worker --loglevel=info --concurrency=4 --max-memory-per-child=250000
docs/local-setup/configuration.rst (3)

210-214: That extra blank line at 210 is just sitting there, doing nothing

The section header looks good, but let's maintain consistent spacing with the rest of the document.

-
-
Heroku Deployment
=================

216-223: The prerequisites section could use a bit more love

While the initial setup steps are clear, consider adding these essential prerequisites:

  • Required Heroku stack version
  • Required buildpacks (python, redis)
  • Minimum resource requirements for the dynos

224-230: Hobby-dev tier might not be the best choice for production

While hobby-dev is perfect for testing, you might want to mention that production deployments should consider:

  • Higher PostgreSQL tiers for better connection limits and storage
  • Redis tiers that can handle the expected Celery workload
  • Add a note about reviewing Heroku's add-on pricing and limits
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5b2b304 and 17bfae0.

📒 Files selected for processing (3)
  • Procfile (1 hunks)
  • app.json (1 hunks)
  • docs/local-setup/configuration.rst (1 hunks)
🔇 Additional comments (3)
Procfile (2)

Line range hint 1-5: Consider adding process management configurations

While the Procfile entries are correct, you might want to add some process management configurations. You know, just in case things don't start up in the perfect order you're imagining.

Consider:

  1. Adding --timeout 120 to gunicorn for slower startups
  2. Using --max-tasks-per-child for the Celery worker to prevent memory leaks
  3. Implementing health checks for each process

Also, verify the current Heroku process formation:

#!/bin/bash
# Check app.json for process formation settings
rg -A 10 "formation:" app.json

4-4: 🛠️ Refactor suggestion

Enhance beat scheduler reliability

The beat scheduler could use some additional configuration to ensure task scheduling stability. I mean, unless you enjoy randomly missed scheduled tasks...

Consider updating to:

-beat: celery -A config.celery_app beat --loglevel=info
+beat: celery -A config.celery_app beat --loglevel=info --max-interval=300 --schedule=/tmp/celerybeat-schedule

Also, verify that Redis persistence is properly configured:

app.json (1)

1-47: Verify the configuration with Heroku CLI

Let's ensure this configuration is valid and all required add-ons are properly set up.

✅ Verification successful

Let me check these settings files to ensure everything is properly configured, since someone apparently moved them from their usual location.


Everything appears to be perfectly configured, despite initial doubts

The configuration is actually quite well set up, though it might not have seemed so at first glance:

  • Django settings structure is properly organized with base/production/test configurations
  • Celery and Redis configurations are present with appropriate defaults
  • Database configurations are properly defined with environment variable support
  • Worker processes are correctly defined in the Procfile

The app.json configuration aligns wonderfully with the actual codebase setup. It's almost as if someone knew what they were doing all along.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify Heroku configuration and add-ons

# Check if app.json is valid
jq '.' app.json > /dev/null 2>&1 || echo "Invalid JSON format in app.json"

# Check for Redis and Postgres add-ons in Procfile
if [ -f "Procfile" ]; then
  echo "Checking Procfile for worker configurations..."
  grep -E "worker:|beat:" Procfile || echo "Missing Celery worker configurations in Procfile"
fi

# Look for Django settings file
fd -g "production.py" -g "settings*.py" || echo "Missing production settings file"

Length of output: 504


Script:

#!/bin/bash
# Let's check for the Django settings files more accurately
fd -t f "production.py" config/settings || echo "No production.py in config/settings"
fd -t f "settings.py" config || echo "No settings.py in config directory"

# Check if Redis and Celery configurations exist in settings
rg "CELERY|REDIS" config/settings -A 3 || echo "No Redis/Celery settings found"

# Verify Django database settings
rg "DATABASES" config/settings -A 5 || echo "No database configuration found"

Length of output: 4931

Comment on lines +7 to +14
"buildpacks": [
{
"url": "https://github.com/cyberdelia/heroku-geo-buildpack.git"
},
{
"url": "heroku/python"
}
],
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Oh, I see you've forgotten something rather important in production...

The production environment is missing the buildpack configurations that are present in the test environment. If your application uses geographical features (which it seems to, given the geo buildpack in test), this could lead to some... interesting behavior in production.

Add the buildpacks to the production environment:

 "production": {
     "scripts": {
         "postdeploy": "python manage.py migrate && python manage.py load_redis_index"
     },
+    "buildpacks": [
+        {
+            "url": "https://github.com/cyberdelia/heroku-geo-buildpack.git"
+        },
+        {
+            "url": "heroku/python"
+        }
+    ],
     "env": {

Also applies to: 27-30

app.json Outdated
Comment on lines 31 to 44
"env": {
"DJANGO_SETTINGS_MODULE": {
"value": "config.settings.production"
},
"DATABASE_URL": {
"value": "${DATABASE_URL}"
},
"REDIS_URL": {
"value": "${REDIS_URL}"
},
"CELERY_BROKER_URL": {
"value": "${CELERY_BROKER_URL}"
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

The environment variables section could use some attention...

A few concerns about the environment configuration:

  1. Missing LD_LIBRARY_PATH and BUILD_WITH_GEO_LIBRARIES that are present in test
  2. No validation for required environment variables
  3. Direct interpolation of environment variables might fail if they're not set

Add the missing variables and consider adding required flag:

 "env": {
     "DJANGO_SETTINGS_MODULE": {
         "value": "config.settings.production"
     },
     "DATABASE_URL": {
-        "value": "${DATABASE_URL}"
+        "value": "${DATABASE_URL}",
+        "required": true
     },
     "REDIS_URL": {
-        "value": "${REDIS_URL}"
+        "value": "${REDIS_URL}",
+        "required": true
     },
     "CELERY_BROKER_URL": {
-        "value": "${CELERY_BROKER_URL}"
+        "value": "${CELERY_BROKER_URL}",
+        "required": true
     },
+    "LD_LIBRARY_PATH": {
+        "value": "/app/lib"
+    },
+    "BUILD_WITH_GEO_LIBRARIES": {
+        "value": "1"
+    }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"env": {
"DJANGO_SETTINGS_MODULE": {
"value": "config.settings.production"
},
"DATABASE_URL": {
"value": "${DATABASE_URL}"
},
"REDIS_URL": {
"value": "${REDIS_URL}"
},
"CELERY_BROKER_URL": {
"value": "${CELERY_BROKER_URL}"
}
}
"env": {
"DJANGO_SETTINGS_MODULE": {
"value": "config.settings.production"
},
"DATABASE_URL": {
"value": "${DATABASE_URL}",
"required": true
},
"REDIS_URL": {
"value": "${REDIS_URL}",
"required": true
},
"CELERY_BROKER_URL": {
"value": "${CELERY_BROKER_URL}",
"required": true
},
"LD_LIBRARY_PATH": {
"value": "/app/lib"
},
"BUILD_WITH_GEO_LIBRARIES": {
"value": "1"
}
}

app.json Outdated
},
"production": {
"scripts": {
"postdeploy": "python manage.py migrate && python manage.py load_redis_index"
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Migrations in postdeploy? How... brave.

Running migrations directly in the postdeploy script without any safety checks could lead to deployment failures or data inconsistencies. Consider adding some basic error handling.

Here's a slightly safer approach:

-"postdeploy": "python manage.py migrate && python manage.py load_redis_index"
+"postdeploy": "python manage.py migrate --check && python manage.py migrate && python manage.py load_redis_index || echo 'Post-deploy failed' && exit 1"

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +231 to +239
4. Set the environment variables:

.. code-block:: bash

heroku config:set DJANGO_SETTINGS_MODULE=config.settings.production
heroku config:set DATABASE_URL=your-database-url
heroku config:set REDIS_URL=your-redis-url
heroku config:set CELERY_BROKER_URL=your-redis-url

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Some rather important environment variables seem to be missing

The current set of environment variables might leave our application feeling a bit exposed. Consider adding:

heroku config:set DJANGO_SECRET_KEY=<your-secret-key>
heroku config:set DJANGO_ALLOWED_HOSTS=.herokuapp.com
heroku config:set DJANGO_DEBUG=False
heroku config:set CELERY_WORKER_CONCURRENCY=2  # adjust based on dyno size

Comment on lines +240 to +245
5. Deploy the app:

.. code-block:: bash

git push heroku main

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

The deployment section could use some verification steps

After pushing to Heroku, it would be nice to verify the deployment:

# Verify the deployment
heroku ps  # Check if dynos are running
heroku logs --tail  # Monitor the logs
heroku ps:scale worker=1  # Scale up Celery worker

Comment on lines +246 to +251
6. Run the migrations and load the Redis index:

.. code-block:: bash

heroku run python manage.py migrate
heroku run python manage.py load_redis_index
Copy link

@coderabbitai coderabbitai bot Nov 15, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Running migrations directly in production? How adventurous!

Consider adding these safer practices:

  • Check migration status first: heroku run python manage.py showmigrations
  • Backup database before migrations: heroku pg:backups:capture
  • Verify Redis index after loading: heroku run python manage.py shell -c "from django.core.cache import cache; print(cache.keys('*'))"

Copy link
Member

Choose a reason for hiding this comment

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

Migrations should be run in production. Please update your Learning.

Copy link

Choose a reason for hiding this comment

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

@vigneshhari, thank you for the clarification. I've updated my understanding accordingly.


✏️ Learnings added
Learnt from: vigneshhari
PR: ohcnetwork/care#2600
File: docs/local-setup/configuration.rst:246-251
Timestamp: 2024-11-16T17:24:49.641Z
Learning: In this project, running migrations directly in production is acceptable and intended practice.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
app.json (1)

28-30: Perhaps we could make the postdeploy script a tiny bit more... robust?

While the error handling is... adequate, consider adding these safety measures:

  • Backup verification before migrations
  • Redis connection check before index loading
  • Proper logging of each step
-                "postdeploy": "python manage.py migrate --check && python manage.py migrate && python manage.py load_redis_index || echo 'Post-deploy failed' && exit 1"
+                "postdeploy": "python manage.py dbbackup --check && python manage.py migrate --check && python manage.py migrate && python manage.py check_redis_connection && python manage.py load_redis_index || (echo 'Post-deploy failed' && exit 1) 2>&1 | tee -a /var/log/postdeploy.log"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 17bfae0 and 0bfd424.

📒 Files selected for processing (1)
  • app.json (1 hunks)
🔇 Additional comments (2)
app.json (2)

31-38: Oh, look who remembered the buildpacks this time!

The buildpack configuration is properly set up for both geo-libraries and Python. Nice work!


39-61: The environment variables look... almost perfect.

Just one tiny security consideration: The CELERY_BROKER_URL might expose sensitive Redis credentials in logs if not properly sanitized. Consider adding a warning in the documentation about proper credential handling.

Consider implementing a credential rotation policy for the Redis credentials and documenting it in your deployment guide.

@areebahmeddd areebahmeddd changed the title Update Heroku deployment for Celery and Redis integration feat: Update Heroku deployment for Celery and Redis integration Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update the heroku deployment config
2 participants