Hello welcome to Yzone.net

使用MySQL UDFs来调用gearman分布式任务分发系统

在gearman job服务器内部,所有的job队列都是存放在内存中的,这就意味着一旦服务器重启或崩溃,未执行的job将会丢失而不会被worker服务器执行。 持久化队列将后台作业存放在一个外部持久的队列中。持久化队列只对后台jobs有效,因为前台jobs依附于客户端。 如果job服务器挡掉了,客户端会检测到,将会从其他地方重新启动这个前台job或者返回错误。 而后台jobs没有依附于客户端,如果要想让它运行则需要提交。

当向表插入数据的时候,触发执行某些任务。

一. 安装gearman-mysql-udf

# apt-get install libmysql++-dev
# wget https://launchpad.net/gearman-mysql-udf/trunk/0.6/+download/gearman-mysql-udf-0.6.tar.gz
# tar zxvf gearman-mysql-udf-0.6.tar.gz
# ./configure  --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib/mysql/plugin
# make
# make install
# mysql
mysql> CREATE FUNCTION gman_do RETURNS STRING
    SONAME "libgearman_mysql_udf.so";
mysql> CREATE FUNCTION gman_do_high RETURNS STRING
    SONAME "libgearman_mysql_udf.so";
mysql> CREATE FUNCTION gman_do_low RETURNS STRING
    SONAME "libgearman_mysql_udf.so";
mysql> CREATE FUNCTION gman_do_background RETURNS STRING
    SONAME "libgearman_mysql_udf.so";
mysql> CREATE FUNCTION gman_do_high_background RETURNS STRING
    SONAME "libgearman_mysql_udf.so";
mysql> CREATE FUNCTION gman_do_low_background RETURNS STRING
    SONAME "libgearman_mysql_udf.so";
mysql> CREATE AGGREGATE FUNCTION gman_sum RETURNS INTEGER
    SONAME "libgearman_mysql_udf.so";
mysql> CREATE FUNCTION gman_servers_set RETURNS STRING
    SONAME "libgearman_mysql_udf.so";
//设置gearman server
mysql> SELECT gman_servers_set("192.168.1.60:4730,192.168.1.60:4731") as gman_servers;  
+-------------------------------------+
| gman_servers                        |
+-------------------------------------+
| 192.168.1.60:4730,192.168.1.60:4731 |
+-------------------------------------+
// 新建表
mysql> create table udf_test(
-> id int unsigned auto_increment primary key,
-> val varchar(20) not null);  
// 创建触发器,当向表udf_test插入数据时候,执行任务。
mysql> create trigger sendmail before insert on udf_test for each row set @return=gman_do_background('MAIL','undef'); 
# perl -MCPAN -e shell
cpan> install Gearman::Worker  //安装Gearman::Worker模块
cpan> install Mail::SendEasy   //安装Mail::SendEasy模块
# vi WORKER_SENDMAIL.pl   //创建worker任务
use strict;
use Mail::SendEasy ;
use v5.10;
use Gearman::Worker;
my $worker=new Gearman::Worker;
$worker->job_servers('192.168.1.60:4730');
$worker->register_function(MAIL=>\&sendmail);
$worker->work while 1;
sub sendmail{
    my $job=shift;
    my $date=localtime;
    my $mail = new Mail::SendEasy(
        smtp => 'smtp.ttlsa.com' ,
        user => 'service@ttlsa.com' ,
        pass => '******',
    );
    print "$date\n";
    my $status = $mail->send(
        from    => 'service@ttlsa.com' ,
        from_title => 'ttlsa' ,
        to      => 'xxxxx@qq.com' ,
        subject => "MAIL Test $date" ,
        msg     => "$date" ,
        html    => "<b>test $date</b>" ,
    );
    if (!$status) { print $mail->error ;}
}
  • 运行 worker
# perl WORKER_SENDMAIL.pl &
# perl WORKER_SENDMAIL.pl &  //在后台运行两个worker

二. 测试

  • 向udf_test表插入数据
mysql> insert into udf_test value ('','a');
  • 查看是否收到邮件

三. Gearman server信息

# telnet 192.168.1.60 4730
Trying 192.168.1.60...
Connected to 192.168.1.60.
Escape character is '^]'.
status
MAIL    0       0       2

说明:第一列worker名称;第二列jobs执行数量;第三列jobs队列数量;第四列可用worker数量

转自: 运维生存时间


分类 tool    标签 gearman , mysql
文档结构