How do you deploy cron jobs to production?

后端 未结 9 971
北海茫月
北海茫月 2021-02-01 18:42

How do people deploy/version control cronjobs to production? I\'m more curious about conventions/standards people use than any particular solution, but I happen to be using git

相关标签:
9条回答
  • 2021-02-01 19:01

    You said:

    I'm more curious about conventions/standards people use than any particular solution

    But, to be fair, the particular solution will depend in your environment and there is no universal elegant silver bullet. Given that you happen to be using Python/Django, I recommend Celery. It is an asynchronous task queue for Python, which integrates nicely with Django. And, on top of the features that it gives as an asynchronous task queue, it also has specific features for periodic tasks.

    I have personally used the django-celery-beat integration and it integrates perfectly with Django settings and behaves correctly in distributed environments. If your periodic tasks are related to Django stuff, I strongly recommend to take a look at Celery I started using it only for certain asynchronous mailing and ended up using it for a lot of asynchronous tasks + periodic sanity checks and other web application maintenance stuff.

    0 讨论(0)
  • 2021-02-01 19:03

    There are really 3 options of manually deploying a crontab if you cannot connect your system up to a configuration management system like cfengine/puppet.

    You could simply use crontab -u user -e but you run the risk of someone having an error in their copy/paste.

    You could also copy the file into the cron directory but there is no syntax checking for the file and in linux you must run touch /var/spool/cron in order for crond to pickup the changes.

    Note Everyone will forget the touch command at some point.

    In my experience this method is my favorite manual way of deploying a crontab.

    diff /var/spool/cron/<user> /var/tmp/<user>.new
    crontab -u <user> /var/tmp/<user>.new
    

    I think the method I mentioned above is the best because you don't run the risk of copy/paste errors which helps you maintain consistency with your version controlled file. It performs syntax checking of the cron tasks inside of the file, and you won't need to perform the touch command as you would if you were to simply copy the file.

    0 讨论(0)
  • 2021-02-01 19:05

    Having your project under version control, including your crontab.txt, is what I prefer. Then, with Fabric, it is as simple as this:

    @task
    def crontab():
        run('crontab deployment/crontab.txt')
    

    This will install the contents of deployment/crontab.txt to the crontab of the user you connect to the server. If you dont have your complete project on the server, you'd want to put the crontab file first.

    0 讨论(0)
  • 2021-02-01 19:06

    I use Buildout to manage my Django projects. With Buildout, I use z3c.recipe.usercrontab to install cron jobs in deploy or update.

    0 讨论(0)
  • 2021-02-01 19:17

    Using Fabric, I prefer to keep a pristine version of my crontab locally, that way I know exactly what is on production and can easily edit entries in addition to adding them.

    The fabric script I use looks something like this (some code redacted e.g. taking care of backups):

    def deploy_crontab():
        put('crontab', '/tmp/crontab')
        sudo('crontab < /tmp/crontab')
    
    0 讨论(0)
  • 2021-02-01 19:23

    You can also take a look at:

    http://django-fab-deploy.readthedocs.org/en/0.7.5/_modules/fab_deploy/crontab.html#crontab_update

    django-fab-deploy module has a number of convenient scripts including crontab_set and crontab_update

    0 讨论(0)
提交回复
热议问题