5

I am writing a Wordpress plug in php in and next step is some kind of add on to this plug in.

The add on would scrape data from web, sending forms etc. I have this part almost ready from the time before I had any thoughts about Wordpress plugin - it's coded in ruby using mechanize. I haven't found anything similar to mechanize in php anyway.

But I do not know what is the best way to call my ruby script from Wordpress. Some tasks will be managed by cron. What about the ones based on user request?

  • php script only triggers the ruby script. It won't wait/require anything from ruby's output
  • Wordpress plugin is fully portable and functional without ruby script. Ruby adds on something more. If somebody requires it.
  • everything will be running on my linux server where I have a root access
2
  • 2
    why would you write a PHP WordPress plugin in Ruby? Commented Nov 9, 2010 at 5:13
  • 1
    sorry for the confusion... Wordpress plugin in php, something on top of that plugin, kind of aside, will be coded in ruby. And sometimes I need to run ruby from php. Commented Nov 9, 2010 at 5:22

3 Answers 3

7

A WordPress plugin that depends on Ruby isn't going to be portable. That's OK if you're the only one who will be using it, though.

If the Ruby script needs to return a result that will be used immediately by the PHP script that's calling it, then something like exec() is the only way. Make sure you escape any arguments you pass to the Ruby script; otherwise you'll be vulnerable to injection attacks.

If the Ruby script doesn't need to return a result immediately (e.g. some background processing, such as thumbnail generation) then I think the best way would be for the PHP script to insert a row into a MySQL database or something similar. The Ruby script can work in the background or run from cron, check the database periodically for new jobs, and do whatever processing it needs to do. This approach avoids the performance overhead and security issues of exec(), and it's arguably also more scalable. (A similar approach would have the Ruby script listen on a socket, and your PHP scripts would connect to the socket. But this requires more work to get it right.)

Sign up to request clarification or add additional context in comments.

2 Comments

I like the database solution. (+1) I will probably go for this one. What if I want the execution to be instantaneous? To write some kind of web based application server?
@Radek If you want the execution to be instantaneous without using exec(), you should make the Ruby script check the database more often, perhaps every 3-5 seconds. If you have more control over the server, you might use Resque instead of MySQL for really efficient queue handling.
2

If i were you i would handle all the ruby stuff from the cron. Make a queue in the DB to hand user requests then make the script (in ruby?) invoked by cron grab all the unprocessed jobs from the queue and start running them, then remove the job from the queue (or set some kind of flag for it being done). This way you dont have to call exec which in most cases is going to be off limits unless the user is running on VPS/Dedicated server where they have root access.

You could also make this a seperate job and have it poll the DB for unprocessed jobs more regularly than the primary job... if necessary.

Still, this begs the question... why use ruby in a php blog/cms app??????

4 Comments

so is it ok to run the ruby script from cron every minute or so? so it looks like it was triggered from the Wordpress plugin?
Every few minutes should be ok... Whatever interval you go with - remember youre going to hit the DB with a query every time so its going to be part of the overall load on the DB.
ok, then there is another question. Should I use Wordpress db for the job queue or something else? Maybe there is something less costy
Well it depends on the usage... If youre letting people invoke some kind of job for this rb script to pick up on then it would probably be good to use the same db since the functionality for adding a job is going to come from the WP interface. If on the other hand this is something totally separate then it would be ok to use a different DB. Also depending on your setup and site stats the added DB traffic may be a complete non issue.
1

Use exec() to run the ruby interpreter, giving it the path to your ruby script.

http://php.net/manual/en/function.exec.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.