[CentOS6][SOS JobScheduler] php で 外部API(XMLコマンド) を使用する


Create: 2013/04/04
LastUpdate: 2013/04/05
[ メニューに戻る ]

下図の環境を使用します。
環境の詳細を知りたい場合は、メニューに戻って構築手順を参照してください。


JobScheduler の外部APIを利用すると、ジョブの実行や登録などの操作を自分のアプリケーションから行うことができます。
ここでは、php で 外部API(XMLコマンド)を使用してみます。
手順については、「JobScheduler HowTo」を参考にしました。

php でJobSchedulerへの接続


php のsocket関数を使用して、JobScheduler に接続し、XMLコマンドを送信することができます。
以下は、ジョブの実行計画を取得する例です。
<?php

    // Socket Open
    $fp = fsockopen("192.168.1.61", "4444", $errno, $errstr, 60);

    // build xml command
    $cmd = '<show_calendar/>';

    // send command
    fwrite( $fp, $cmd );

    // get response
    //while( !feof($fp) ) {
    //  echo fgets($fp);
    //}
    echo get_answer($fp);

    // Socket Close
    fclose($fp);

    return 0;

    function get_answer($fp)
    {
       $answer = ''; $s = '';
       while ( !ereg("</spooler>", $s) && !ereg("<ok[/]?>", $s) ) {
         $s = fgets($fp, 1000);

         if (strlen($s) == 0) { break; }
         $answer .= $s;
         $s = substr($answer, strlen($answer)-20);

         // chr(0) am Ende entfernen.
         if (substr($answer, -1) == chr(0)) {
           $answer = substr($answer, 0, -1);
           break;
         }
       }
       $answer = trim($answer);

       return $answer;
  }

?>
fscokopen()で JobScheduler のIPアドレスと、ポート番号を指定してソケットをオープンし、そのソケットにXMLコマンドを送信しています。
JobScheduler は、XMLコマンドを送信したソケットに、XMLコマンドの実行結果を返してきますが、単順に fgets() すると終了(EOF)判定できないので、get_answer() のような処理を施して実行結果を取得する必要があります。
このプログラムを "test.php" に保存すれば、以下のようにしてコマンドラインから実行できます。
$ php ./test.php

php 用の開発キット


JobScheduler の開発元である SOS GmbH では、上記のようなソケット通信処理や面倒なエラーハンドリングをカプセル化して使いやすくした php のclass を提供しています。
開発キットは、「JobScheduler HowTo」からダウンロードして解凍すれば使用できます。
"package_scheduler.zip" と "package_classes.zip" の2つありますが、ここでは、"package_scheduler.zip"だけ使用します。
$ wget http://www.sos-berlin.com/download/scheduler/sources/sos/scheduler/php_xml_interface/package_scheduler.zip
$ unzip package_scheduler.zip
解凍すると、"scheduler" ディレクトリが作成されて、以下のようなファイルが格納されています。
$ ls ./scheduler
scheduler_api_testsuite.php                    sos_scheduler_job_elements.inc.php
sos_scheduler.inc.php                          sos_scheduler_jobcommand_launcher.inc.php
sos_scheduler_command.inc.php                  sos_scheduler_lock.inc.php
sos_scheduler_command_modifyhotfolder.inc.php  sos_scheduler_object.inc.php
sos_scheduler_history.inc.php                  sos_scheduler_order_commands.inc.php
sos_scheduler_hotfolder_launcher.inc.php       sos_scheduler_ordercommand_launcher.inc.php
sos_scheduler_job.inc.php                      sos_scheduler_process_class.inc.php
sos_scheduler_job_chain.inc.php                sos_scheduler_runtime.inc.php
sos_scheduler_job_chain_elements.inc.php       sos_scheduler_runtime_elements.inc.php
sos_scheduler_job_commands.inc.php
以下は、開発キットの使用例です。require_once で使用する class を読み込みます。
このプログラムも "test.php" と同じく、ジョブの実行計画を取得しています。
<?php
     require_once( './scheduler/sos_scheduler_command.inc.php');

     //open socket
     $comm = new SOS_Scheduler_Command('192.168.1.61',4444,60);

     //build command
     $cmd = '<show_calendar/>';

     if (!$comm->connect()) {
        echo $comm->get_error() . ' ' . __FILE__ . ' ' . __LINE__ ;
        return 0;
     }

     //send command
     $comm->command($cmd);

     //receive response
     if ($comm->get_answer_error()) {
        echo $comm->get_error() . ' ' . __FILE__ . ' ' . __LINE__ ;
     }

     echo $comm->answer;

     //close socket
     $comm->disconnect();

?>
このプログラムを "test2.php" に保存して実行すると、以下のようになります。結果を見やすいように xmllint コマンドで整形しています。
$ php ./test2.php | xmllint -format -
<?xml version="1.0" encoding="ISO-8859-1"?>
<spooler>
  <answer time="2013-04-04 14:13:35.637">
    <calendar>
      <at at="2013-04-08T20:00:00" job="/sos/update/scheduler_check_updates"/>
      <at at="2013-04-05T00:00:01" job="/sos/dailyschedule/CreateDaysSchedule"/>
      <at at="2013-04-06T00:00:01" job="/sos/dailyschedule/CreateDaysSchedule"/>
      <at at="2013-04-07T00:00:01" job="/sos/dailyschedule/CreateDaysSchedule"/>
      <at at="2013-04-08T00:00:01" job="/sos/dailyschedule/CreateDaysSchedule"/>
      <at at="2013-04-09T00:00:01" job="/sos/dailyschedule/CreateDaysSchedule"/>
      <at at="2013-04-10T00:00:01" job="/sos/dailyschedule/CreateDaysSchedule"/>
      <at at="2013-04-04T20:00:00" schedule="/test/schedule1" job="/test/shell_job"/>
      <at at="2013-04-05T20:00:00" schedule="/test/schedule1" job="/test/shell_job"/>
      <at at="2013-04-08T20:00:00" schedule="/test/schedule1" job="/test/shell_job"/>
      <at at="2013-04-09T20:00:00" schedule="/test/schedule1" job="/test/shell_job"/>
      <at at="2013-04-10T20:00:00" schedule="/test/schedule1" job="/test/shell_job"/>
      <period begin="2013-04-04T00:00:00" end="2013-04-05T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
      <period begin="2013-04-05T00:00:00" end="2013-04-06T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
      <period begin="2013-04-06T00:00:00" end="2013-04-07T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
      <period begin="2013-04-07T00:00:00" end="2013-04-08T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
      <period begin="2013-04-08T00:00:00" end="2013-04-09T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
      <period begin="2013-04-09T00:00:00" end="2013-04-10T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
      <period begin="2013-04-10T00:00:00" end="2013-04-11T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
      <period begin="2013-04-11T00:00:00" end="2013-04-12T00:00:00" repeat="1800" job="/sos/dailyschedule/CheckDaysSchedule"/>
    </calendar>
  </answer>
</spooler>
この開発キットは、上記のようなXMLコマンドの送信以外にも、いろいろな使い方ができます。
使用例が、"scheduler/scheduler_api_testsuite.php" に記載されているので、参照してみてください。
ちなみに、上記のように自分でXMLコマンドをコーディングしない、XML-PHPインタフェースもあります。
XML-PHPインタフェースを使用したい場合は、"package_classes.zip"を、"scheduler" ディレクトリ内で解凍します。
$ ls ./scheduler/class/
sos_class.inc.php  sos_globals.inc.php  sos_history.inc.php