i'm lost figuring out why php script produces different results when run via cli (producing right results) vs when run within node.js via exec function.
here's php. note fopen('blah.txt', 'w') line, i'm using check if script reaches place.
calculator.php
class calculator{ public static $_var = array(); public function calculate($xml, $xsl, $frequency){ $reader = new domdocument; if(!$reader->load($xsl)) throw new exception('xsl not loaded: ' . $xsl); $processor = new xsltprocessor; $processor->importstylesheet($reader); $processor->registerphpfunctions(); $processor->setparameter('', 'frequency', $frequency); $dictionary = new domdocument; if(!$dictionary->load($xml)) throw new exception('xml not loaded: ' . $xml); $processor->transformtoxml($dictionary); } public static function connectnodes($name, $dataset, $processor, $destination, $frequency){ $var = self::$_var; self::$_var = array(); $test = fopen('blah.txt', 'w'); fclose($test); ob_start(); require 'templates/' . $processor . '.php'; $xsltsheet = new domdocument; $xsltsheet->loadxml(ob_get_clean()); $xsltproc = new xsltprocessor; $xsltproc->importstylesheet($xsltsheet); $dataset = new domdocument; if(is_file('../datafiles/xml/' . $dataset . '.xml') && calculator::makedirs('../datafiles/json/' . $destination . '/' . $frequency)){ //proceed if dataset exists , folders created correctly $dataset->load('../datafiles/xml/' . $dataset . '.xml'); if(!is_file('../datafiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json')){ $filemaker = fopen('../datafiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json', 'w'); fclose($filemaker); } $xsltproc->transformtouri($dataset, '../datafiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json'); } else { throw new exception('either dataset not exist('. $dataset .'), or directories not created (datafiles/json/'. $destination . '/' . $frequency .')'); } } public static function populatevarwithxpath($var){ self::$_var[] = $var; } public static function makedirs($dirpath, $mode=0777) { return is_dir($dirpath) || mkdir($dirpath, $mode, true); } } when run via cli, blah.txt created in directory of script ran. when run via node.js, doesn't create blah.txt!
here's node script:
script runner
var ipc = require('./ipc'); var exec = require('child_process').exec; ipc.listen(function(frequency){ if(frequency.tostring().match(/integrator /)){ var freq = frequency.tostring().replace(/integrator /, ''); console.log('received message: ' + freq); var cmd = exec('php use_calculator.php metricsdictionary.xml metricsxmlreader.xsl ' + frequency); cmd.on('close', function(exitcode, stdout, stderr){ if(exitcode === 0){ console.log('exitcode: ' + exitcode + ' ; stdout: ' + stdout + ' ; stderr: ' + stderr); } else { console.log('try again?'); } }); cmd.on('error', function(err){ throw err; }); } }); the funny thing here is, if move blah.txt calculate() function (which run first), both cli , node create file.
a little more context:
the use_calculator.php file call calculate() function , pass 3 required arguments it. then, within calculate(), xml , xsl file merge.
within xsl rules on how handle xml data, , call connectnodes() function -- process new xml , xsl. it's inception of xml/xsl.
the point there's 1 xml file listing details of , how other xml data should processed. file read in calculate(). calculates each piece of data, calls connectnode() function in inception-like way.
to make things clearer (i hope), here's metricsxmlreader.xsl calls connectnodes()
metricsxmlreader.xsl
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:php="http://php.net/xsl"> <xsl:output method="html" omit-xml-declaration="yes" /> <xsl:template match="metrics"> <xsl:for-each select="metric"> <xsl:if test="frequency = $frequency"> <xsl:for-each select="xpath/var"> <xsl:variable name="pusher" select="php:functionstring('calculator::populatevarwithxpath', .)" /> </xsl:for-each> <xsl:variable name="trigger" select="php:functionstring('calculator::connectnodes', name, dataset, processor, destination, frequency)" /> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
my first guess that, wherever php trying write file to, can't. try absolute path rights /tmp.
also should make sure can handle parallel requests. because doesn't scale. mentioned pipeing solution.
hp
ps: have tried it?
Comments
Post a Comment