gearman+mysql实现持久化队列

默北 Gearmangearman+mysql实现持久化队列已关闭评论14,062字数 1902阅读6分20秒阅读模式

持久化队列是在0.6版本中新添的一项功能,允许将队列存放在drizzle或mysql中。

0.7版本允许将队列存放在memcached。文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

0.9版本可以将队列存放在sqlite3或postgresql。文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

在gearman job服务器内部,所有的job队列都是存放在内存中的,这就意味着一旦服务器重启或崩溃,未执行的job将会丢失而不会被worker服务器执行。文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

持久化队列将后台作业存放在一个外部持久的队列中。持久化队列只对后台jobs有效,因为前台jobs依附于客户端。如果job服务器挡掉了,客户端会检测到,将会从其他地方重新启动这个前台job或者返回错误。而后台jobs没有依附于客户端,如果要想让它运行则需要提交。文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

The persistent queue works by calling a module callback function right before putting a new job in the internal queue for pending jobs to be run. This allows the module to store the job about to be run in some persistent way so that it can later be replayed during a restart. Once it is stored through the module, the job is put onto the active runnable queue, waking up available workers if needed. Once the job has been successfully completed by a worker, another module callback function is called to notify the module the job is done and can be removed. If a job server crashes or is restarted between these two calls for a job, the jobs are reloaded during the next job server start. When the job server starts up, it will call a replay callback function in the module to provide a list of all jobs that were not complete. This is used to populate the internal memory queue of jobs to be run. Once this replay is complete, the job server finishes its initialization and the jobs are now runnable once workers connect (the queue should be in the same state as when it crashed). These jobs are removed from the persistent queue when completed as normal. NOTE: Deleting jobs from the persistent queue storage will not remove them from the in-memory queue while the server is running.文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

创建数据库和表文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

create database gearman
create table `gearman_queue` (
`unique_key` varchar(64) NOT NULL,
`function_name` varchar(255) NOT NULL,
`priority` int(11) NOT NULL,
`data` LONGBLOB NOT NULL,
`when_to_run` INT, PRIMARY KEY  (`unique_key`)
)

启动gearmand文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

gearmand -q libdrizzle --libdrizzle-host=127.0.0.1 --libdrizzle-user=gearman --libdrizzle-password=password --libdrizzle-db=gearman --libdrizzle-table=gearman_queue --libdrizzle-mysql

创建一个后台job文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

gearman -f testqueue -b xx00

查看队列文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

select * from gearman.gearman_queue

执行队列中的job文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

gearman -f testqueue -w

查看队列文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/

select * from gearman.gearman_queue  //这条job删除掉了
文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/文章源自运维生存时间-https://www.ttlsa.com/gearman/gearman-mysql/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 01/01/2012 16:17:20
  • 转载请务必保留本文链接:https://www.ttlsa.com/gearman/gearman-mysql/
  • drizzle
  • gearmand
  • memcached
  • mysql
  • postgresql
  • sqlite3
  • 分布式
  • 持久化
  • 队列