PHPackages                             janfish/async-caller - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. janfish/async-caller

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

janfish/async-caller
====================

v1.1.3(5y ago)162MITPHPPHP &gt;=7.1.0

Since Aug 9Pushed 5y ago1 watchersCompare

[ Source](https://github.com/zeng444/asyncCaller)[ Packagist](https://packagist.org/packages/janfish/async-caller)[ RSS](/packages/janfish-async-caller/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (7)Used By (0)

消息中间件
=====

[](#消息中间件)

特性
--

[](#特性)

- 支持多种类型的异步调用，使用消息队列服务保证消费事务一致性
- 支持多进程、安全关闭、安全重启、后台进程化运行
- 支持内存回收，进程完成指定任务数后，自动注销重建
- 消息队列服务器断线自动延时重连
- 支持自定义任务模版

安装
--

[](#安装)

```
composer require janfish/async-caller

```

服务端
---

[](#服务端)

- 启动服务

```
$asyncModel = new Janfish\Phalcon\AsyncCall\Server([
    'host' => 'beanstalkd2',
    'port' => '11300',
    'tube' => 'test_tube',
    'workerNum' => 2,
    'reserveTimeout' => 2,
    'maxRequest' => 2000,
    'cron' => 200,
    'daemonize' => false,
    'pidFile' => __DIR__.'/.async_task.pid',
    'logPath' => __DIR__.'/async.log',
]);
$asyncModel->start();
```

参数默认说明host127.0.0.1Beanstalkd服务地址port11300Beanstalkd服务端口tubeasync\_call\_test消费队列名，多个填写数组，单个填写字符串workerNum1进程数reserveTimeout2读取队列超时时间，单位秒maxRequest1000最大完成任务数，当一个进程达到最大任务数，将平滑重启cron200定时任务周期，默认200毫秒daemonizefalse是否已后台进程方式运行pidFile/tmp/async\_task\_%d.pid运行时pid记录的地址logPath日志文件地址，设置后将写日志- 重启服务

```
$asyncModel = new Janfish\Phalcon\AsyncCall\Server([
    'pidFile' => __DIR__.'/.async_task.pid',
]);
$asyncModel->restart();
```

- 关闭服务

```
$asyncModel = new Janfish\Phalcon\AsyncCall\Server([
    'pidFile' => __DIR__.'/.async_task.pid',
]);
$asyncModel->stop();
```

- linux命令关闭

> 主进程pid直接kill，子进程会检查自己任务执行完后自行退出

```
kill -9 $pid
```

docker 下运行
----------

[](#docker-下运行)

- docker下运行无法平滑重启和关闭的问题，原因是非进程模式下运行asyncCaller是使用容器内PID=1进程的，docker下默认PID=1进程不处理发起的sigterm信息的
- 解决方案是docker挂起命令以下script脚本，然后此脚本会运行在PID=1下，asyncCaller运行在其他PID，就可以正常接收信号了

```
#!/bin/bash
FOLDER=/data/asyncCallerHub/demo
CMD=${FOLDER}/server_start.php
LOG_FILE=${FOLDER}/async.log
php -f ${CMD}
tail -f ${LOG_FILE}
```

然后是平滑重启

```
docker exec -d  swoole-cli php /data/asyncCallerHub/demo/server_restart.php

```

开启多套服务
------

[](#开启多套服务)

- 只需要定义不同的pid即可

```
$asyncModel = new Janfish\Phalcon\AsyncCall\Server([
    'host' => 'beanstalkd2',
    'port' => '11300',
    'tube' => 'test_tube1',
    'pidFile' => __DIR__.'/.async_task.pid',
],'\\Core\\Mailler');
$asyncModel->start();
```

```
$asyncModel = new Janfish\Phalcon\AsyncCall\Server([
    'host' => 'beanstalkd2',
    'port' => '11300',
    'tube' => 'test_tube2',
    'pidFile' => __DIR__.'/.async_task_2.pid',
],'\\Core\\Mailler');
$asyncModel->start();
```

客户端
---

[](#客户端)

```
$asyncModel = new \Janfish\Phalcon\AsyncCall\Client([
    'host' => 'beanstalkd',
    'port' => '11300',
    'tube' => 'test_tube',
]);
$result = $asyncModel->asyncCall([
   'model' => 'Order',
   'modelParams' => $insuranceQuotation->id,
   'method' => 'cancel',
   'delay' => 10,
   'retryTimeTable' =>  [time() + 10, time() + 120, time() + 3600]
   'retryIntervalTime' => 600,
   'retryStopAt' => date('Y-m-d H:i:s', strtotime('+2 hours')),
   'forceSync' => false,
   'ttr'=> 60,
   'errorRetry'=> false,
]);

```

参数类型必填说明modelstring是调用的ORM模型对象名称modelParamsmixed否调用的ORM的条件参数，空字符串，criteria条件，或数字主键IDmethodstring是调用ORM对象的方法methodParamsarray否调用的方法参数delayint否延时执行，单位秒retryTimeTableint否按指定时刻表重复运行任务，当此参数填写时，禁止使用retryIntervalTime和retryStopAt参数，单位秒retryIntervalTimeint否当结果为false延时执行的间隔时间，单位秒retryStopAtdatetime否延时执行的停止执行时间 Y-m-d h:i:sforceSyncbool否强制同步执行,默认falsettrint否最大执行一个任务的时间，默认60errorRetrybool否任务出错后是否重试 ，默认false订制命令解析
------

[](#订制命令解析)

- 继承 \\Janfish\\Phalcon\\AsyncCall\\Command，并实现其中的方法，定义如何解析和执行收到的消息

```
