Django APScheduler + uwsgi 定时任务重复运行

基于某些原因可能在开发的时候通过django的manage.py运行定时任务没有任何的问题,但是一旦到了线上环境通过nginx+uwsgi来运行就会发现定时任务不断的重复执行,并且基本都执行失败了。发生这个问题的原因在于uwsgi启动了多个进程来提供服务,于是每次启动的时候定时任务都会跟着再启动一次,于是有4个进程的话,对应的服务就会启动4次,除了第一次可能执行成功后面的基本都会挂掉。

要解决这个问题其实也不难,只要保证在第一次启动的时候添加定时任务并且执行,以后启动的进程不再处理定时任务即可。但是在这种条件下通过python的进程互斥其实貌似并不是非常好使,具体可以看这个:

uWSGI employs some tricks which disable the Global Interpreter Lock and with it, the use of threads which are vital to the operation of APScheduler. To fix this, you need to re-enable the GIL using the --enable-threads switch. See the uWSGI documentation for more details.

Also, assuming that you will run more than one worker process (as you typically would in production), you should also read the next section.

https://apscheduler.readthedocs.io/en/latest/faq.html#how-can-i-use-apscheduler-with-uwsgi

Continue Reading

阿里云oss 批量检测文件是否存在

虽然阿里云oss的sdk提供了检测文件是否存在,但是在批量处理的时候你就会发现检测一次需要联网一次,如果文件过多最后会提示你链接数超过限制,最终无法进行检测了。

下面是阿里云提供的示例代码:

# -*- coding: utf-8 -*-
import oss2

# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth('', '')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '')

exist = bucket.object_exists('')
# 返回值为true表示文件存在,false表示文件不存在。
if exist:
	print('object exist')
else:
	print('object not eixst')

那么其实可以反过来想,直接拉文件目录落下来进行比较,列举文件的代码如下:

# -*- coding: utf-8 -*-

# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth('api-key', 'api-secret')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'https://oss-cn-beijing.aliyuncs.com', 'bucket-name')

file_arrary = []
# 设置Delimiter参数为正斜线(/)。
for obj in oss2.ObjectIterator(bucket, delimiter='/'):
    # 通过is_prefix方法判断obj是否为文件夹。s
    if obj.is_prefix():  # 文件夹
        #print('directory: ' + obj.key)
        for obj2 in oss2.ObjectIterator(bucket, prefix='%s' % obj.key):
            #print('file: ' + obj2.key)
            file_arrary.append(obj2.key)
    else:  # 文件
        file_arrary.append(obj.key)

如果要判断文件是否存在,只需要在数组中进行比较就可以了

file_arr = []
for file in file_arr:
    if file in file_arrary :
           print('esixts')
    else:
           print('not exists')

 

ubuntu 16.04 uwsgi自启动

网上的关于uwsgi的自启动的方法还是挺多的,具体搜索一下就知道了,这里简单的写一下官方推荐的方法,通过systemd启动服务。如果用这个方法需要首先确定systemd的版本大于211。

通过下面的命令获取systemd版本信息:

root@mars:/etc/systemd/system# systemctl --version
systemd 229
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN

按照官方的文档编写service文件,放入/etc/systemd/system目录下然后执行

systemctl start emperor.uwsgi.service即可启动服务。service文件如下:
[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --ini /var/www/html/project/uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

这个脚本需要注意两个地方,一个是uwsgi的可执行文件路径,另外一个是uwsgi.ini配置文件路径。可执行文件路径可以通过whois uwsgi获取。

Continue Reading

django 主动抛出 403 异常

网上的做法基本都是下面的代码

return HttpResponseForbidden()

试了一下,效果一般,没有异常页面显示,最终显示的是浏览器的异常页面,如下图:

如果要想让服务器截获异常并且显示错误页可以用下面的方式:

id = request.GET.get('id', '')
timestamp = request.GET.get('timestamp', '')
accesskey = request.GET.get('accesskey', '')

if timestamp == '' or accesskey == '' or id == '':
    raise PermissionDenied

Continue Reading

ngix+uwsgi+django 以及阿里云rds数据库数据导入

网上关于如果通过nginx来运行django的文章还是蛮多的,但是有的地方写的可能不够细致,于是在实际操作的时候可能会出现一些问题,具体可以参考这个链接:https://www.cnblogs.com/frchen/p/5709533.html。但是实际在不熟的时候却发现uwsgi的配置文件貌似不怎么好用,于是去官网看了一下,近习惯了修改:

[uwsgi]
socket = 127.0.0.1:8001
chdir = /var/www/html/
wsgi-file = Project/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191

#stats=%(chdir)/uwsgi/uwsgi.status

pidfile=uwsgi.pid

buffer-size = 65536

需要注意的一点是,这个chdir 是项目的根目录,也是工程文件的父目录,后面的wsgi-file则是工程目录和wsgi文件的结合路径,如果这个搞错了,后面的就直接跑不起来了。此时就可以通过uwsgi uwsgi.ini来启动服务了,但是如果此时通过浏览器直接去访问http://192.168.1.100:8001这个地址很有可能是失败的。要配置好nginx的代理之后通过nginx进行访问,nginx配置文件如下:

Continue Reading