简体   繁体   中英

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. My application is deployed on azure, it is a webapp and is built on codeigniter v3.x. I have been able to run a schedule on my local window machine using a .bat file and a .php file.

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. Right now there is a simple php file cron.php which does the job. 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. go to the old Azure portal ( https://manage.windowsazure.com )
  3. go to your website, select the webjob tab.
  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
  6. On the next screen select "recurring job", and recur every one day.
  7. repeat from point 4 with your other script

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.

  • You do not need the batch file, just use the php file as your WebJob, Azure will run it.

  • 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.

    For example {"schedule": "0 0 0 * * *"} for once a day at midnight.

I wanted to have a webjob to run using CodeIgniter's MVC structure, but nobody seemed to answer that question anywhere I could find. 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. In this example, my controller is called "cron" and my method is "clean_uploads". If you wanted to access it manually in your browser, you'd go to 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_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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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