在后台Codeigniter Windows中运行控制器方法 [英] Run controller methods in background Codeigniter Windows

查看:282
本文介绍了在后台Codeigniter Windows中运行控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从我的程序开始。控制器的 index 函数使用一个URL和关键字数组,并将它们存储在DB中。现在使用 crawlLink 方法,并使用所有关键字和网址。针对所有关键字搜索URL,并且所有URL的子链接被生成并再次存储在DB中,DB也被搜索关键字。使用搜索方法在每个链接中搜索关键字。使用 extract_links 功能从所有网址中提取子链接。 搜索 extract_links 都有一个称为 get_web_page 的方法,它使用cURL获取网页的完整内容。 get_web_page 搜索功能中使用一次,以获取网页的内容,以便从中提取关键字。它还用于 extract_links 功能中,以提取包含有效网页内容的链接。

I'll start with what my program does. The index function of controller takes an array of URLs and keywords and stores them in DB. Now the crawlLink method with take all the keywords and URLs. The URLs are searched for all the keywords and the sublinks of all the URLs are generated and again stored in DB which are also searched for the keywords. Keywords are searched in each link using search method. The sublinks are extracted from all the URLs using extract_links function. search and extract_links both have a method called get_web_page which takes the complete content of the page using cURL. get_web_page is used once in search function to get content of web page so that keywords can be extracted from it. It is also used in extract_links function to extract links with valid page content.

现在 crawlLink 会调用搜索功能两次。一次从域链接中提取关键字,并第二次从子链接中提取关键字。因此, get_web_page 被称为三次。大约需要5分钟获得大约150个链接的内容。它被称为三次,所以它需要15分钟的处理时间。在这段时间里没有什么可做的。因此,我想在后台运行此进程并显示其处理过程中的状态。 extract_links get_web_page 包含在控制器中使用include_once。

Now crawlLink calls search function twice. Once to extract keywords from domain links and second time to extract keywords from sublinks. Hence, get_web_page is called thrice. It approximately takes 5 mins to get contents of around 150 links. And it is called thrice so it takes 15 minutes of processing time. During that duration nothing can be done. Thus, I want to run this process in background and show its status while processing. extract_links and get_web_page are included in the controller using include_once.

get_web_page函数如下:

The get_web_page function is as follows:

function get_web_page( $url )
{
    $options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle compressed
    CURLOPT_USERAGENT      => "spider", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

一旦输入来自用户的URL和关键字,就可以视为任务。现在这个任务可以开始,它将开始在后台运行。同时,可以定义并启动另一个任务。每个任务将具有诸如待办事项,进行中,待定,完成等状态。 Oscar Dias的简单任务板是我想要显示任务的确切方式。

An input of URLs and keywords once from the user can be considered as a task. Now this task can be started and it will start running in the background. At the same time another task can be defined and can be started. Each task will have statuses like "To Do", "In Progress", "Pending", "Done", etc. The Simple Task Board by Oscar Dias is the exact way I want the tasks to be displayed.

我阅读了很多方法来运行函数在背景,现在我在一个进退两难的方法采取。我读了关于exec,pcntl_fork,Gearman和其他,但都需要CLI,我不想使用。我尝试安装Gearman与Cygwin,但被困在Gearman安装,因为它找不到libevent。我已经单独安装了libevent,但仍然不工作。和Gearman需要CLI,所以放弃它。我不想使用CRON。我只想知道在我的情况下最好的方法。

I read about so many ways to run function in background that now I am in a dilemma about which approach to adopt. I read about exec, pcntl_fork, Gearman and other but all need CLI which I don't want to use. I tried installing Gearman with Cygwin but got stuck in Gearman installation as it cannot find libevent. I've installed libevent separately but still it doesn't work. And Gearman needs CLI so dropped it. I don't want to use CRON also. I just want to know which approach will be best in my scenario.

我使用PHP 5.3.8 | Codeigniter 2.1.3 | Apache 2.2.21 | MySQL 5.5.16 | Windows 7 64位

I am using PHP 5.3.8 | Codeigniter 2.1.3 | Apache 2.2.21 | MySQL 5.5.16 | Windows 7 64 bit

推荐答案

我试图实现的上述方法似乎不可能在Windows中实现。问题中列出的许多方法被删除或修改。然后我继续讨论使用 AJAX 的解决方法。

The above approach that I was trying to achieve didn't seem possible to be implemented in Windows. Many methods listed in the questions are either removed or modified. I then moved on to a workaround involving use of AJAX.

我执行控制器方法作为ajax请求,给它一个计数,它随着每个新的AJAX请求而递增。每个请求可以中止,虽然处理将继续,但最终结果在我的项目中,即使他们是不完整的。如果浏览器打开,那么该请求可以完成,稍后用户可以看到完整的结果。

I execute the controller method as an ajax request and give a count to it which increments with each new AJAX request. Each request can be aborted though the processing will continue but ultimately results matter in my project even if they are taken incomplete. And if the browser is open then that request may complete and later on the user can see the complete result.

在停止任务处理时,会显示一个CANCELED图标,将显示指向结果页面的链接,其中显示在取消任务之前生成的结果。在AJAX失败或AJAX成功我发送回任务的计数从服务器到客户端发送到客户端到服务器。因此,结果显示为一个独特的任务,不会搞乱。

On stopping the processing of a task a CANCELLED icon is shown and a link pointing to result page is shown which displays the results generated before the task was cancelled. On AJAX fails or AJAX success I send back the count of the task from server to client which was sent by the client to server. Thus results are displayed for a unique task and don't get messed up.

但是没有跟踪某个任务进展了多少。无法识别执行所需的时间。因此,这种方法对我有用,但有一些缺点。主要目的是用户不应该在某些任务正在进行时等待,而是通过上述解决方法以某种方式实现。

But there is no tracking of how much a certain task has progressed. The time taken for execution cannot be identified. Thus, this approach works for me but has some drawbacks. The main aim was the user should not be waiting while some task is in progress and that is somehow achieved by the above workaround.

这篇关于在后台Codeigniter Windows中运行控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆