简体   繁体   English

您如何将 cron 作业部署到生产中?

[英]How do you deploy cron jobs to production?

How do people deploy/version control cronjobs to production?人们如何部署/版本控制 cronjobs 到生产? I'm more curious about conventions/standards people use than any particular solution, but I happen to be using git for revision control, and the cronjob is running a python/django script.我对人们使用的约定/标准比任何特定解决方案都更感兴趣,但我碰巧使用 git 进行修订控制,而 cronjob 正在运行 python/django 脚本。

If you are using Fabric for deploment you could add a function that edits your crontab.如果您使用Fabric进行部署,您可以添加一个编辑 crontab 的函数。

def add_cronjob():
    run('crontab -l > /tmp/crondump')             
    run('echo "@daily /path/to/dostuff.sh 2> /dev/null" >> /tmp/crondump')
    run('crontab /tmp/crondump')

This would append a job to your crontab (disclaimer: totally untested and not very idempotent).这会将作业附加到您的 crontab(免责声明:完全未经测试且不是非常幂等)。

  1. Save the crontab to a tempfile.将 crontab 保存到临时文件。

  2. Append a line to the tmpfile.在 tmpfile 中追加一行。

  3. Write the crontab back.写回 crontab。

This is propably not exactly what you want to do but along those lines you could think about checking the crontab into git and overwrite it on the server with every deploy.这可能不是您想要做的,但是沿着这些思路,您可以考虑将 crontab 检查到 git 中,并在每次部署时在服务器上覆盖它。 (if there's a dedicated user for your project.) (如果您的项目有专门的用户。)

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.使用 Fabric,我更喜欢在本地保留我的 crontab 的原始版本,这样我就可以确切地知道生产中的内容,并且可以轻松地编辑条目以及添加条目。

The fabric script I use looks something like this (some code redacted eg taking care of backups):我使用的结构脚本看起来像这样(编辑了一些代码,例如处理备份):

def deploy_crontab():
    put('crontab', '/tmp/crontab')
    sudo('crontab < /tmp/crontab')

You can also take a look at:你也可以看看:

http://django-fab-deploy.readthedocs.org/en/0.7.5/_modules/fab_deploy/crontab.html#crontab_update 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 django-fab-deploy 模块有许多方便的脚本,包括 crontab_set 和 crontab_update

You can probably use something like CFEngine/Chef for deployment (it can deploy everything - including cron jobs)您可能可以使用 CFEngine/Chef 之类的东西进行部署(它可以部署所有内容 - 包括 cron 作业)

However, if you ask this question - it could be that you have many production servers each running large number of scheduled jobs.但是,如果您问这个问题 - 可能是您有许多生产服务器,每个服务器都运行大量计划作业。 If this is the case, you probably want a tool that can not only deploy jobs, but also track success failure, allow you to easily look at logs from the last run, run statistics, allow you to easily change the schedule for many jobs and servers at once (due to planned maintenance...) etc.如果是这种情况,您可能需要一个工具,它不仅可以部署作业,还可以跟踪成功失败,让您轻松查看上次运行的日志、运行统计信息、轻松更改许多作业的计划以及服务器一次(由于计划维护......)等。

I use a commercial tool called "UC4".我使用一种名为“UC4”的商业工具。 I don't really recommend it, so I hope you can find a better program that can solve the same problem.我真的不推荐它,所以我希望你能找到一个更好的程序来解决同样的问题。 I'm just saying that administration of jobs doesn't end when you deploy them.我只是说当您部署作业时,作业的管理并没有结束。

Having your project under version control, including your crontab.txt, is what I prefer.我更喜欢将项目置于版本控制之下,包括 crontab.txt。 Then, with Fabric , it is as simple as this:然后,使用Fabric ,就这么简单:

@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.这会将deployment/crontab.txt的内容安装到您连接到服务器的用户的 crontab 中。 If you dont have your complete project on the server, you'd want to put the crontab file first.如果您在服务器上没有完整的项目,您应该首先put crontab 文件。

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.如果您无法将系统连接到像 cfengine/puppet 这样的配置管理系统,那么手动部署 crontab 确实有 3 个选项。

You could simply use crontab -u user -e but you run the risk of someone having an error in their copy/paste.您可以简单地使用crontab -u user -e但您冒着某人在复制/粘贴中出错的风险。

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.您也可以将文件复制到 cron 目录中,但没有对该文件进行语法检查,并且在 linux 中您必须运行touch /var/spool/cron以便 crond 获取更改。

Note Everyone will forget the touch command at some point.注意每个人都会在某个时候忘记触摸命令。

In my experience this method is my favorite manual way of deploying a crontab.根据我的经验,这种方法是我最喜欢的手动部署 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.它对文件内的 cron 任务执行语法检查,并且您不需要像简单地复制文件那样执行 touch 命令。

If you're using Django, take a look at the jobs system from django-command-extensions .如果您使用的是 Django,请查看django-command-extensions中的作业系统

The benefits are that you can keep your jobs inside your project structure, with version control, write everything in Python and configure crontab only once.好处是您可以将作业保留在项目结构中,使用版本控制,用 Python 编写所有内容并仅配置 crontab 一次。

I use Buildout to manage my Django projects.我使用Buildout来管理我的 Django 项目。 With Buildout, I use z3c.recipe.usercrontab to install cron jobs in deploy or update.使用 Buildout,我使用z3c.recipe.usercrontab在部署或更新中安装 cron 作业。

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 .鉴于您碰巧使用 Python/Django,我推荐Celery It is an asynchronous task queue for Python, which integrates nicely with Django.它是一个用于 Python 的异步任务队列,它与 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.我个人使用过django-celery-beat集成,它与 Django 设置完美集成,并在分布式环境中正确运行。 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.如果您的定期任务与 Django 相关,我强烈建议您看一看 Celery 我开始仅将它用于某些异步邮件,最终将其用于许多异步任务 + 定期健全性检查和其他 Web 应用程序维护内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行 nginx 的 Elastic Beanstalk 中运行 Django 管理 &gt; 命令作为 cron 作业 - How do I run Django Management > Commands as cron jobs in Elastic Beanstalk running nginx 如何使用 Django 模型动态创建 cron 作业? - How to dynamically create cron jobs with Django model? Django:如何部署生产数据库 - Django: How to deploy production database 您如何上传图片并将其发布(在生产中)? - how do you upload an image and post that image (in production)? Django:如何在使用 cron 作业时测试并发问题 - Django: how to test for concurrency issues when using cron jobs Django cron作业是进程还是线程? - Are Django cron jobs processes or threads? 如何在django中添加第三方可重用应用程序并在openshift中进行部署? - How do you add a third party reusable app to django and deploy in openshift? 如何使用jwilder / nginx-proxy部署Flask或Django应用程序? - How do you deploy a flask or django application using jwilder/nginx-proxy? 如何在django cron作业中使用的函数中添加参数? (django-crontab) - How to add parameters to function used in django cron jobs? (django-crontab) Django:如何在生产中的apache服务器上部署静态文件 - Django: How to deploy static files on an apache server in production
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM