Giter Site home page Giter Site logo

Comments (30)

coolseven avatar coolseven commented on August 18, 2024

@Tinywan 你好,windows 环境是可以执行任务的。
看了一下你的项目代码,猜测的原因是你在web端的入口文件 index.php 中修改了TP框架配置文件的目录,但是忘记了去修改命令行入口文件 think

from notes.

coolseven avatar coolseven commented on August 18, 2024

windows 端执行结果参考:

  • 发布:
    produce

  • 执行:
    consume

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 非常感谢

  • 你提交的代码,我pull下来是直接可以运行的,
  • think 的配置文件
// 定义配置文件目录和应用目录同级
define('CONF_PATH', __DIR__ . '/config/');
  • 你是否愿意一起搞这个项目?(我也主要是为了学习)

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan 不用客气。
我看到你的项目里面包含了 AI,FFmpeg,还有 wechat 等等,有点好奇你的项目是哪个方面的呢?是要实现什么需求呢?
另外,我最近正在使用的技术栈换成了 laravel 和 vue ,现在业余时间在用它们来开发一个可视化配置 xdebug 的小工具,如果你对这个项目感兴趣的话,也欢迎一起弄。

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 好啊!

  • 我主要搞的是流媒体,FFmpeg使用的比较多,公司使用Phalcon,以前用3.2写过几个项目,现在学习下5.0,感觉比较方便、容易上手。
  • 看来我们可以达成共识啊!我也主要是靠业余时间,多学习点东西。
  • 可以一起开搞,看来我们可以达成共识。
  • 一起共同努力!

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan
共勉!
之前我曾经想开发一个工具网站,可以让用户上传录音文件,后台通过一些第三方 Api 来转写为文本,并提供校对,翻译等功能,后来因为第三方 Api 对声音文件的长度都有时间限制,所以曾经尝试用 ffmpeg 去做文件分割,以及格式转换等,不过由于音视频处理涉及的内容实在太多,所以就搁置了。。。
以后在使用到 ffmpeg 时,希望能向你请教 : )

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 互学,一起...

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 使用队列做一个Excel 数据的导出

  • php think queue:work --queue mail 这个命令如何通过一个控制器去实现
  • 需求
    • 1、查询数据
    • 2、加入队列
    • 3、写入Excel表格
  • 我现在是已经加入队列中, 如何用控制器的一个方法去执行命令行:php think queue:work --queue mail ,而不知去命令行手动去执行

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan 我的理解是你的环境中没有设置常驻的队列服务,而是希望在遇到耗时任务的时候能够通过控制器来临时启动一个一次性的工作进程?

  • 如果你希望获取这个工作进程的输出结果,可以试一下tp内置的 调用方法:
Queue::push();

$command = 'queue:work' ;
$params = ['queue' => $queue , 'sleep' => 3];    // (注:这个写法是错误的,后面有说明)
$result = \think\Console::call($command ,$params);
  • 如果你不需要获取结果(很大可能是这样的),我觉得可以试一下 popen 函数,比如
Queue::push();

$command = 'php think queue:work --queue '.$queue ;
pclose(popen($command , 'r'));

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 理解的合适的

  • 如何设置常驻的队列服务?
  • Windows 环境测试运行不了,提示:Too many arguments.

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan
常驻的队列服务,我的理解是指:

  • 工作进程处于循环模式
  • 工作进程在后台运行
  • 工作进程意外退出时,可以通过某些手段来自动重启进程。

对于上面的要求:

  • 如果想让工作进程进入循环模式,只要在终端/命令行窗口中加入 --daemon 参数即可,这个windows 和 linux 应该都是支持的
php think queue:work --queue YourQueue --sleep 3 --daemon
  • 如果想让工作进程进入后台运行,即把输出结果转移到日志中而非显示到终端/命令行窗口上,可以添加下面的修饰:
// linux
php think queue:work --queue YourQueue --sleep 3 --daemon 1>queue.log 2>&1 &

// windows
start /b php think queue:work --queue YourQueue --sleep 3 --daemon 1>queue.log 2>&1
  • 如果想让工作进程在当前的终端关闭或断开之后仍然继续运行,即进入守护模式,可以添加下面的修饰符:
// linux
nohup php think queue:work --queue YourQueue --sleep 3 --daemon 1>queue.log 2>&1 &

// windows 下似乎没有类似 nohup的命令,命令行窗口关闭之后,队列进程就强制结束了。。。
  • 如果想让工作进程在意外终止时能够自动重启,linux 环境下,你可能需要借助 supervisor 管理器来监控和自动启动你的队列服务, 之前有同学写了supervisor工具的使用方法 传送门。至于windows 环境,感觉应该没有这样的工具。。。

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan
你说的问题

Windows 环境测试运行不了,提示:Too many arguments.

有错误截图吗?怎么重现?

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 我直接上代码,还是你那天提交的代码

  • 控制器
class Demo
{
    public function index(  )
    {
        $consumer = HelloJob::class;    // app\queue\consumer\HelloJob
        $link = "http://www.baidu.com/backend/login/emailRegisterUrlValid?checkstr=11111111111&auth_key=909090909090";
        $payLoad = [
            'time' => time(),
            'mail' => "[email protected]",
            'url' => $link,
            ];
        $queue = 'mail';

        $pushed = Queue::push($consumer ,$payLoad ,$queue);
        if ($pushed) {
            Log::error("[100000000000000000]加入队列成功,开始执行队列:".$pushed);
            $command = 'queue:work' ;
            $params = ['queue' => $queue , 'sleep' => 3];
            $result = \think\Console::call($command ,$params);
            Log::error("[200000000000000000]队列执行结束,返回结果:".json_encode($result));
            echo 'job Pushed.' . '<br/>' .'job payload :' . '<br/>' .var_export($payLoad,TRUE);
        }else{
            echo 'Oops, something wrong with your queue';
        }
    }
}
  • 消费者
class HelloJob
{
    public function fire( $job ,$payload)
    {
        $done = $this->consume($payload);
        if ($done) {
            $this->deleteJob($job);
        }else{
            $this->releaseJob($job);
        }
    }

    private function consume( $payload )
    {
        try {
            Log::error("think-queue running on windows".date("Y-m-d H:i:s"));
            send_email($payload["mail"], '物联网智能数据--', $payload["url"]);
            echo ' Job Processed send_email!' . ' payload: '. PHP_EOL . var_export($payload,true) .PHP_EOL;
            return true;
        } catch (\Exception $e) {
            echo 'Hello Job Process Failed!' ;
            var_dump($e);
            return false;
        }
    }
}
  • 错误提示 (http://www.tinywan_thinkphp5.com/queue/demo/index)
    toomant
  • 我的思路:
    • 1、新用户使用邮箱注册,后台逻辑就是把该邮箱加入队列后,让队列自动执行(发送邮件)
    • 2、或者有个观察者(watch)监控到有新的队列成员加入,该队列的执行也会自动触发
    • 3、以前了解过观察者模式,但具体没怎么研究过。是不是可以使用观察者模式去实现来?
    • 4、昨天也看了下:php-resque,,感觉TP5的一样,也要手动在命令行执行。
  • 问题:
    • 邮件已经成功加入到队列中,如果让这个队列立刻自动执行?
  • 需求:
    • 邮箱注册
    • Excel 数据导出,(上万条)MySQL数据库数据的导出或者写入操作
    • 短信通知,微信邀请码的自动批量短信发送给指定的手机用户
    • ...

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan

  • 对于那个 Too many arguments. 的问题, 是我的代码写错了,不好意思
$command = 'queue:work' ;
 // $params = ['queue' => $queue , 'sleep' => 3];    // error : Too many arguments.
$params = ['--queue' => $queue , '--sleep' => 3];  // 修正后的写法(注:这个也是错误的,后面有说明)
$result = \think\Console::call($command ,$params);
  • 对于你后来的问题和需求

邮件已经成功加入到队列中,如何让这个队列立刻自动执行?

我的思路是:

  1. 先启动一个后台常驻的队列服务,windows 或者linux 都可以,方法在上面已经写了传送门
  2. 在请求的控制器中推送消息到这个队列服务即可,队列服务会在合适的时间自动触发发送邮件的任务

注意:你的需求中,存在“立刻”和“自动”两个关键词,然而,队列服务的目的是保证你的邮件会自动发送,但是无法保证立刻发送。因为你的消息在队列服务中是排着队等待被执行的,你推送的消息默认是排在最末尾的。
如果你需要尽快的被执行,那么可以参考一下TP的队列的优先级功能。

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 修改后也是一样的报错结果:Too many arguments.

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan
至于观察者模式和队列的问题,在我的理解中,观察者模式一种设计模式,队列服务是一种解决方案,两者是不同层面的东西。
队列服务本身要解决的核心功能是消息的推送,存储,提取和消费。
在消息推送的环节,TP的队列服务对外提供了 Queue::push() 这个方法用于推送消息到队列。
你可以选择在控制器中直接创建一个“给指定用户发送邮件”的消息并推送到队列,
也可以选择只触发一个“新用户注册成功”事件,事件管理器获取到这个事件之后再分发给监听了这个事件的类去处理,监听了这个事件的类的内部再帮你创建一个“给指定用户发送邮件”的消息并推送到队列,但这个事件的处理过程本身跟队列是没有特别的联系的

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan
我还是写错了,哈哈哈
正确的写法应该是:

$command = 'queue:work' ;
 // $params = ['queue' => $queue , 'sleep' => 3];    // error : Too many arguments.
// $params = ['--queue' => $queue , '--sleep' => 3]; // error : Too many arguments.
$params = ['--queue=yourQueue'  , '--sleep=3'];    // 修正后的写法
$result = \think\Console::call($command ,$params);

你可以再试试

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven OK了

  • 但是是同步的,一直是阻塞的,直到邮件发送成功才返回,也就是发布和执行是顺序执行的
  • 我感觉应该单独写一个action 直接用命令去执行,但是没有找到一个很好的执行方案
  • TP5 的Queue 基本就是按照php-resque 这个思路去设计的。

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan
队列服务的目的就是为了解决你说的阻塞和顺序执行的问题呀。
在真实环境中,使用 \think\Console::call('queue:work' ,$params); 这种写法肯定是错误的,这种写法仅仅就是方便调试而已。
就像之前说的,你需要

  • 先搭建好一个常驻的队列服务以及对应的消费者,
  • 然后,在控制器中,只需要保证消息推送成功就可以了,队列服务会在合适的时间自动地获取并消费你的消息。
  • 控制器没有必要也无法知道推送的消息是否执行了,或者是否执行成功了

就拿windows环境来说,你可以试一下:

  • 先打开一个命令行窗口,执行 php think queue:work --daemon 命令,开启一个循环模式的队列进程,
  • 然后,不要关闭这个窗口,刷新你的页面(页面对应的控制器中存在 Queue::push() 相关的代码)
  • 观察页面的加载结果和命令行窗口的输出结果。这两者之间会有一定的延迟,你可以把命令行中的 --sleep 参数调大一点,这个延迟会更明显。
  • 这个延迟就体现了队列服务的特点和价值: 把耗时任务从业务系统抽离出来异步执行。

from notes.

coolseven avatar coolseven commented on August 18, 2024

@Tinywan
php-resque 这个库没有用过,不知道你有没有时间把这两个队列库的特性和异同做一个整理?

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 我昨天只是在Linux 做了个测试,也是要在命令行去执行的,TP5应该就是借鉴的这个做的。

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@coolseven 我还是去Linxu去测试吧!Windows 搞不成事情

from notes.

Lance-93 avatar Lance-93 commented on August 18, 2024

您好,我按照demo执行到这步的时候 php think queue:work --queue helloJobQueue 显示以下信息
windows上:
image
centos上:
image
请问我该如何进行下去

from notes.

Lance-93 avatar Lance-93 commented on August 18, 2024

原来是这里少了 变量符
image

from notes.

coolseven avatar coolseven commented on August 18, 2024

原来是这里少了 变量符
image

非常感谢!
现已修复.

from notes.

wellson1 avatar wellson1 commented on August 18, 2024

作者大大您好,我按照你的教程写了一个demo,队列已经成功建立,并且在redis里面可以看到队列数据。
但是在命令行执行命令:php think queue:work --queue testqueue,时,报错: [Exception] redis鎵╁睍鏈畨瑁?。。
然后,我想问下,queue的配置文件位置,是固定的吗?可以不可以放到其它位置。
我刚刚学习php不久,希望大牛能抽空回复一下。谢谢。

from notes.

Tinywan avatar Tinywan commented on August 18, 2024

@wellson1 配置文件就是固定的哈

from notes.

skygt35 avatar skygt35 commented on August 18, 2024

您好 我执行 php think queue:work --queue helloJobQueue 执行没有结果 只有提示Processed: app\api\Jobcl\Jobc处理这个队列 我在看数据库 和 think log都没执行结果的记录 不知道问题出现在哪里

from notes.

skygt35 avatar skygt35 commented on August 18, 2024

肯请作者大佬 指点一下 小弟刚学php不久 每次都掉坑 太悲惨了

from notes.

jaceZhang avatar jaceZhang commented on August 18, 2024

from notes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.