APScheduler + Gearman 构建分布式定时任务调度

APScheduler是基于类似于Java Quartz的一个Python定时任务框架,实现了Quartz的所有功能。提供了基于日期、固定时间间隔以及crontab类型的任务,并且可以持久化任务。 它利用sqlalchemy包实现job状态存储于关系数据库,例:

__oracle_url = ‘oracle://test1:test1@10.44.74.13:8521/biprod’
__configure = { ‘apscheduler.standalone’: True,
‘apscheduler.jobstores.sqlalchemy_store.class’: ‘apscheduler.jobstores.sqlalchemy_store:SQLAlchemyJobStore’,
‘apscheduler.jobstores.sqlalchemy_store.url’: __oracle_url}

from apscheduler.jobstores.sqlalchemy_store import SQLAlchemyJobStore
from apscheduler.scheduler import Scheduler

scheduler = Scheduler(standalone=False)
scheduler.add_jobstore(SQLAlchemyJobStore(url=__oracle_url), ‘default’)

使用请参考: http://pythonhosted.org/APScheduler/index.html

Gearman是一款开源的通用的分布式任务分发框架,自己本身不做任何实际的工作。它可以将一个个的任务分发给其他的物理机器或者进程,以达到工作的并行运行和LB。 有人说Gearman是分布式 计算框架其实是不太准确的,因为相较于Hadoop而言,Gearman更偏重于任务的分发而不是执行。Gearman扮演的角色更像是一系列分布式进程 的神经系统。

Gearman框架中一共有三个角色:

  1. Client: 提交任务的人。创建需要被执行的job然后发送给Job Server。
  2. Worker: 真正干活的人。向Job Server注册然后从Job Server处拿活干。
  3. Job Server:传说中的manager。接收client提交的Job,分发给相应的worker。并能在worker出现异常时重新派发job。

http://img1.51cto.com/attachment/201307/152852966.png

由于yum或apt-get安装的版本太低,一般都到手工下载最新版本编译安装。步骤如下:
1. 安装依赖包, sudo apt-get install gcc autoconf bison flex libtool make libboost-all-dev libcurl4-openssl-dev curl libevent-dev memcached uuid-dev libpq-dev
2. 下载安装程序, wget https://launchpad.net/gearmand/1.2/1.1.5/+download/gearmand-1.1.5.tar.g
3. 解压编译安装,
tar xvzf gearmand-1.1.5.tar.gz
cd gearmand-1.1.5
./configure
make
make install
4. 当运行 /usr/local/sbin/gearmand -d 时出现 error while loading shared libraries: libgearman.so.1, 运行如下 sudo ldconfig

启动gearman:

1. gearmand –pid-file=/var/run/gearman/gearmand.pid –daemon –log-file=/var/log/gearman-job-server/gearman.log –listen=192.168.56.101
gearmand –verbose DEBUG -d

2. 通过命令行工具来体验 Gearman 的功能:
启动 Worker:gearman -w -f wc — wc -l &
运行 Client:gearman -f wc < /etc/passwd

gearman -w -f testgm — python &
gearman -f testgm < test_gearman.py

Linux库共享文件的查找与判断

1.  查找库文件位置
     使用命令whereis,例:
$ whereis libpython2.7.so.1.0
libpython2.7.so.1: /usr/local/lib/libpython2.7.so.1.0
如果whereis没有结果,可以尝试如下操作: 在/etc/ld.so.conf中加入新行/usr/local/lib  ,保存后,运行/sbin/ldconfig –v
2. 库文件依赖关系– ldd
   []$ ldd /usr/local/lib/libpython2.7.so.1.0
linux-vdso.so.1 =>  (0x00007fff21122000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b67a021f000)
libdl.so.2 => /lib64/libdl.so.2 (0x00002b67a043b000)
libutil.so.1 => /lib64/libutil.so.1 (0x00002b67a063f000)
libm.so.6 => /lib64/libm.so.6 (0x00002b67a0842000)
libc.so.6 => /lib64/libc.so.6 (0x00002b67a0ac6000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c2ea00000)

查询linux的内核版本和发行版本

两种内核版本查询方式:
1)cat /proc/version
Linux version 2.6.18-238.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)) #1 SMP Thu Jan 13 16:24:47 EST 2011
2) uname -a
Linux hq 2.6.18-238.el5 #1 SMP Thu Jan 13 16:24:47 EST 2011 i686 i686 i386 GNU/Linux
三种发行版本查询方式:
1)lsb_release -a
LSB Version:    :core-4.0-ia32:core-4.0-noarch:graphics-4.0-ia32:graphics-4.0-noarch:printing-4                                                                         .0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 5.6 (Final)
Release:        5.6
Codename:       Final
2) cat /etc/issue
CentOS release 5.6 (Final)
Kernel \\r on an \\m
3) cat /etc/***_release
CentOS release 5.6 (Final)

python 邮件发送

python 2.7代码如下:

#coding: utf-8
import smtplib
from email.mime.text import MIMEText

#connect smtp server
msg = MIMEText(‘Hello’,’plain’,’utf-8′)
msg[‘Subject’] = ‘ Load data sucess!’
#msg[‘Date’] = formatdate(localtime=True)
smtp = smtplib.SMTP()
smtp.connect(‘proxy-in.xxx.com’)
smtp.sendmail(‘bidev@xxx.com’, ‘swer@xxx.com’, msg.as_string())

 

python 2.4.3

#coding: utf-8
import smtplib
from email.MIMEText import MIMEText

#connect smtp server
msg = MIMEText(‘Hello’,’plain’,’utf-8′)
msg[‘Subject’] = ‘ Load data sucess!’
smtp = smtplib.SMTP()
smtp.connect(‘proxy-in.xxx.com’)
smtp.sendmail(‘bidev@xxx.com’, ‘hsdf@xxx.com’, msg.as_string())

unable to correct problems you have held broken packages

OS:  Ubuntu 14.04

When installing a ubuntu desktop KDE,  some error  like “unable to correct problems you have held broken packages” happen.  So I finally found the problm is the package python3-software-properties is too new and can’t be compatible with the kde package. The soluation is follling:

sudo apt-get remove python3-software-properties

sudo apt-get install python3-software-properties=0.92.36

查看端口是否占用

环境 :CentOS

[root@sandbox ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1118 root 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1255 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1256 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1257 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1258 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1259 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1260 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1261 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)
httpd 1262 apache 4u IPv6 9178 0t0 TCP *:http (LISTEN)