简体   繁体   English

我如何在Azure中为PHP应用程序设置Webjob

[英]How can i setup a webjob in azure for a php application

I have a situation where i need to process a large volume of database data once a day for my php application. 我有一种情况,我每天需要为我的php应用程序处理大量的数据库数据。 My application is deployed on azure, it is a webapp and is built on codeigniter v3.x. 我的应用程序部署在azure上,它是一个Web应用程序,并基于codeigniter v3.x构建。 I have been able to run a schedule on my local window machine using a .bat file and a .php file. 我已经能够使用.bat文件和.php文件在本地窗口计算机上运行计划。

here is the content of my batch file 这是我的批处理文件的内容

start D:\xampp\php\php.exe -f D:\xampp\htdocs\phpcode\cron.php

this I can write because i know the exact path of php. 我可以写这个,因为我知道php的确切路径。 Right now there is a simple php file cron.php which does the job. 现在有一个简单的php文件cron.php可以完成任务。 But what i plan is to write a controller class and call its function from the scheduler. 但是我计划的是编写一个控制器类,并从调度程序中调用其功能。

start <the path of php executable on azure> <call to something like 'http://www.example.com/controllername/functionname>

Please advise. 请指教。 Any other solution will also be helpful 任何其他解决方案也将有所帮助

As you want to run your scripts once a day, there is quite an easy solution there, please tell me if it does not meet your needs, There are others :). 由于您想每天运行一次脚本,因此这里有一个简单的解决方案,请告诉我它是否不能满足您的需求,还有其他:)。

  1. create 2 zip files containing your php and bat file (1 file per zip) 创建2个包含php和bat文件的zip文件(每个zip 1个文件)
  2. go to the old Azure portal ( https://manage.windowsazure.com ) 转到旧的Azure门户( https://manage.windowsazure.com
  3. go to your website, select the webjob tab. 转到您的网站,选择“ webjob”选项卡。
  4. Press on add a job 按下添加工作
  5. upload one of your Zip file and in the "How to run" selection box, select "run on schedule", press next 上传您的一个Zip文件,然后在“如何运行”选择框中选择“按计划运行”,然后按下一步
  6. On the next screen select "recurring job", and recur every one day. 在下一个屏幕上,选择“周期性工作”,然后每天重复一次。
  7. repeat from point 4 with your other script 从第4点开始使用其他脚本重复

The job will now run each day on the time you set up! 现在,该作业将在您设置的时间每天运行! Tell me if the solution fix your problems! 告诉我解决方案是否可以解决您的问题!

Cheers, 干杯,

Mike 麦克风

Few additions to @mandrax answer. @mandrax答案的补充很少。

  • You do not need the batch file, just use the php file as your WebJob, Azure will run it. 您不需要批处理文件,只需将php文件用作WebJob,Azure将运行它。

  • You can use the WebJob scheduler described in this answer , basically you just add a file called settings.job to your WebJob and describe the schedule there as a cron expression. 您可以使用此答案中描述的WebJob调度程序,基本上,您只需在WebJob中添加一个名为settings.job的文件,并将该时间表描述为cron表达式即可。

    For example {"schedule": "0 0 0 * * *"} for once a day at midnight. 例如{"schedule": "0 0 0 * * *"}每天凌晨12点一次。

I wanted to have a webjob to run using CodeIgniter's MVC structure, but nobody seemed to answer that question anywhere I could find. 我想让一个Webjob使用CodeIgniter的MVC结构运行,但是似乎没有人可以在我能找到的任何地方回答这个问题。 After hours of searching and testing, here is the answer. 经过数小时的搜索和测试,这就是答案。

You need a batch file to manually call php.exe with CI's index as the page and the controller and method as parameters. 您需要一个批处理文件来手动调用php.exe,并将CI的索引作为页面,并将控制器和方法作为参数。 In this example, my controller is called "cron" and my method is "clean_uploads". 在此示例中,我的控制器称为“ cron”,而我的方法为“ clean_uploads”。 If you wanted to access it manually in your browser, you'd go to http://yoursite.com/cron/clean_uploads 如果您想在浏览器中手动访问它,请访问http://yoursite.com/cron/clean_uploads

REM Set PHP location
SET php="D:\Program Files (x86)\PHP\v5.6\php.exe"

REM Set Index Location
SET index="D:\home\site\wwwroot\index.php"

REM Set Controller
SET controller="cron"

REM Set Method
SET method="clean_uploads"

REM Run Command
%php% %index% %controller% %method%

To find the location of php.exe and index.php, you'll need to run the following inside a CI Controller: 要找到php.exe和index.php的位置,您需要在CI控制器中运行以下命令:

$php_location   = exec("where php.exe");
$index_location = FCPATH . "index.php";

echo 'SET php="' . $php_location . '"' . "\n";
echo 'SET index="' . $index_location . '"' . "\n";

If you want to disable access to your controller via the browser, add the following to your constructor: 如果要禁用通过浏览器对控制器的访问,请将以下内容添加到构造函数中:

    if (php_sapi_name() != "cli") {
        show_404();
    }

Hope that helps someone else! 希望对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM