RSS
 

The Solution: Use PHP’s pcntl_fork to limit execution time of MySQL queries

16 Dec

One of the worst parts of a web application can be the variability of mysql queries that get sent into your database.  You can add indices, tweak hardware configurations, etc., but wouldn’t it be nice to simply kill any database query that takes longer than whatever you deem is “too long”?

Well, no, not any query.  I would never want to kill a write — just a read-only query; in particular: search queries.

So, as it turns out, the hurdles for this are immense, and, because my solution uses PHP’s pcntl_fork() function, even my solution, while it works, it has to make assumptions and is not perfect.

That being said, it would seem easy enough for this to be built into PHP or for some similar mechanism to be built into MySQL: Execute this query but only if it takes less than n seconds.  If not, kill it.  This is not the case, however, so we’re left to our own cleverness.

There are hundreds of reasons why you would never want to do this, but I only need one reason to want to do it to try to implement it.

So here is my solution steps in techno-layman’s terms, followed by the necessary code:

  • Call a function to execute a MySQL query (again, preferably read-only)
  • Open a shared memory space so that we can pass the query results back to the parent from the child
  • Store process state information in a database
  • Fork, and execute the query in the child process
  • Keep time in the parent process
  • Kill the child process if it takes longer than n seconds
  • Return the results

 

Just to forwarn, I have tested this as proof of concept, but I am uncertain about the particulars of PHP’s shared memory and am not confident how reliable the shared memory implementation will in a production environment.  I plan to try it out, but right now I’m just getting the info out there.

So the state database will be defined as follows:

CREATE TABLE IF NOT EXISTS `pcntlFork` (
`idx` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`status` enum('0','1') NOT NULL DEFAULT '0',
`output` longblob NOT NULL,
PRIMARY KEY (`idx`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=58 ;

The PHP class is available to download here:  http://mysql-restrictor.googlecode.com/files/mysqlRestrictor.class.php (I had it inline, but WordPress did not want to format it properly)

And you would use it something like this

$m = new mysqlRestrictor();
$results = $m->dbQuery("SELECT * FROM `table`");
var_dump($results);

Since this took me an extremely long time to build and just verify that it even works, I welcome comments for improvement, and by all means use it for yourself. Let me know how it does in production!

 

A simple question on Sandusky and Penn State’s chopping block

15 Nov

I have seen a few posts generally asking the question, but I want to weigh in on it as well.  The United States is still equipped with a system called due process, but Penn State has already pulled the trigger on many people (most notably Joe Paterno) before any legal judgment has been handed to Sandusky.

Not that I think it will happen, but what if Sandusky is cleared of all charges?  Will that not completely invalidate all the action taken by the University?

 
 

A review: Science Story Poster Publishing (science-story.com)

29 Sep

A few weeks ago, I got it in my head that I really want to get a poster of the solar system, complete with as many major moons, dwarf planets, otherwise significant solar system objects, etc. as possible.  Let me tell you, it’s not an easy thing to find.  There are many great posters, but they are often old and outdated, or poor quality, or simply not filled with the level of objects I was looking for.  To top it all off, they cost at least $100, and really don’t have  great durability.  Considering I was looking for posters in the width range of about 8 feet, standard poster paper was not really going to last long on the wall of a 3-year-old.

Yes he’s 3, so maybe the poster was more for me, but as long as it lasts, eventually it will be for him too (again, of course, assuming he ultimately cares about that stuff, and why woudn’t he ?!??! I digress …)

Finally I happened across science-story.com (note the hyphen).  They had this incredible poster:

and it was only $49.  The only obvious thing I saw missing were Nix and Hydra (Pluto’s 2 smaller moons, and even when I’d ordered its 4th moon had just been discovered).

To top it off, the poster was almost 8′ in length and advertised as basically “untearable.”  Rock!

So, I placed the order.  The total was $62.

Then I got a little concerned.  My invoice was #7, meaning they’d only had 7 orders.  So, the price was very cheap, and no one had really ordered from this company.  I was concerned.

I emailed right away, hoping to get a reply.  Well, I got it.  Scott was very polite, kept me well informed.  Still, there was a delay of nearly a month.  He kept me informed, and it turned out the coating on the poster was having issues.  I, of course, was still very skeptical, but remained positive and kept emailing.

Finally, Scott very courteously sent me this poster:

to appease my impatience, and even offered a refund (after the topographical map poster had already shipped).  I was happy for the free gift, but really wanted the big poster, so I kept waiting.

Finally the big poster arrived, with yet another free gift (The Mona Lisa):

Of course, the highlight for me is still the giant poster, but the service was exceptional, and the product is exactly what I wanted (for a phenomenal price).

If you’re like me, or you’re a teacher looking for great classroom posters, or for any other reason you stumble upon science-story.com, do not hesitate to order.  In my view, you won’t be disappointed.

I will certainly order from them again …

 
No Comments

Posted in Reviews

 

Here’s a big +1 for AquaSource Customer Service

30 Aug

So I am currently redoing my downstairs bathroom (in brief for those that don’t know: completely gutted and replaced with awesomeness).  Cecily and I found what we considered to be the perfect faucet at Lowes:  AquaSource #0476590A with pull-down sprayer.

The problem is, I installed it, and somewhere along the line it started leaking on the top side of the sprayhead.  Fortunately, I kept the documentation, gave the AquaSource customer service department a call, and told them the part I needed (which was clearly listed in the documentation).  They told me there was no charge, and the item would be shipped out this week.

Talk about flooring me.  I’m honest, so being as I didn’t know what caused the problem (me or factory defect), I was expecting to pay for it.  Since I just got off the phone, there may be updates to this post, but assuming the part comes as expected (which I have no reason to doubt at this moment), now would be a good time to read this post’s title.  :)

 
 

2011 Washington DC Earthquake and insights on technology

23 Aug

I love how the whole world is abuzz within minutes after the record setting earthquake in Virginia, DC, but it also keeps me entertained keeping an eye on the Wikipedia articles:

Also, dicks.

 
 

PHP Random String and POST Form Generator

08 Jul

Just a little snippet of sample code.  Sometimes you just need a random string generator, and on top of that, a random form to test a page.

Maybe I’m just keeping this for my own future reference, but maybe someone else out there could use it too.  :)

The function generates a string containing numbers and letters only (it’s easily customizable to contain other chars).  The form just creates inputs with random names from the string generator and random values from the string generator.


function randomString() {
        $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for ($i=0; $i<rand(10,20); $i++) { $string .= $chars[rand(0,strlen($chars))]; }
        return $string;
}

?><form action="" method="POST"><?php
        for ($i=1; $i<=rand(5,10); $i++) { ?><p /><input name="<?php echo randomString(); ?>" value="<?php echo randomString(); ?>" /><?php }
        ?><p /><input type="submit" /><?php
?></form><?php

 

How to process PayPal Express Checkout for third party merchants

18 May

This is a very simple one, but one that can take a lot of google-search-query-nuance tweaking to find.

It’s easy to find the documentation to do PayPal Express Checkout, but to find that one little field where you send an alternate user as the recipient of the payments, well that’s downright impossible.  It is not in the docs (at least not as of this writing 5-18-2011).

How simple is it? Very — the variable is “SUBJECT”

Yes, you specify an alternate “SUBJECT” of the transaction.

Normally your SetExpressCheckout request looks something like:

METHOD=<method_name>&VERSION=<version>&PWD=<API_Password>&USER=<API_UserName>&SIGNATURE=<API_Signature>&...

Now it will look like this:

METHOD=<method_name>&VERSION=<version>&PWD=<API_Password>&USER=<API_UserName>&SIGNATURE=<API_Signature>&SUBJECT=<Payee_PayPal_Account>...

Payee_PayPal_Account is the email address/username the user uses to log in.

Hope this helps!  Took us valuable time to find …

 

Nano global search and replace tabs to spaces or spaces to tabs

17 May

I have been an avid user of nano/pico since about 1999, and yes, many naysayers think it is crap for programming, but it works for me, and I like it.

That being said, one of the major issues I’ve had is that the Xorg select/paste always copies tab characters as the corresponding number of spaces.  So, when I select text in one file, paste into another, I have to replace all the spaces with tabs.

Typically I paste into gedit first, do the replace there, then c/p into the file.  This preserves the tabs.

(If you’re still wondering why I use nano, I just like having my editor accessible as long as I have server access.  I never got used to vi, and nano is more than effective for me.)

I have always thought nano should be able to search and replace tabs and spaces, but I could never get it to work.  Even without the gedit technique, I would typically just replace all double spaces to nothing, then manually insert the tabs.  My workarounds, again, are generally sufficient.  I have not NEEDED search and replace of tabs within nano, but today I decided I wanted to really find out if I could.

And it required some digging …

But, I finally found it: verbatim input!

Nano has a feature to disable character interpretation, and for one character, accept input literally.

To turn it on (again it’s for just the first character typed), hit alt-SHIFT-V (alt-V without shift may trigger x-windows menus), then just hit the tab key (it may or may not show a note that you’re in verbatim input mode).

You only need to do this in the search / replace prompts.  Obviously, you can type tabs directly into the file.

So an example — let’s say you want to convert any instance of 8 spaces to a tab character.

Here is the command stack:

control-W (search)
control-R (replace)
hit space 8 times, then hit enter
alt-shift-V (verbatim input)
hit the tab key, hit enter

Proceed as normal.

Hopefully this post is easier to find than the seemingly impossible digging I just undertook …

 

Red Riding Hood – A Review

19 Mar

My aim is to keep this brief and to just put forth a few reactions about the movie Red Riding Hood, but I have a slight tendency toward verbosity.

My wife and I watched the movie last night (March 18 2011), and I hadn’t even heard of it until 2 days before.  She mentioned that it might be good to see, so I looked up reviews, and read only one, and I’m unable to retrieve it now.  It basically just said that the movies horrid reviews (mostly related to the acting) were unwarranted, that Gary Oldman was brilliant (as usual), and that the costuming was fantastic.  To that end, I thought the cast list was great, so we decided to watch the movie.

From here in, I make no promises not to spoil the movie (if you’re of the camp that believes that the movie itself has not already accomplished that).

My thoughts? Yes, the movie feels like any of the Twilight movies, for reasons not short of the fact that Billy Burke is in both.  Is the acting bad? The directing? The writing?  Well, the movie is not all that much of a disappointment.  It lacks any real philosophical substance, but that alone is not the purveyor of entertainment.  The color of the film was fantastic, that I can say for sure.

As much as I hate to say it, the film suffers from a technological flaw — it was filmed in too high of a resolution.  The images are so sharp, it gave me the sense that I was watching a rehearsal in person.  It’s as if I could see the pixelization of the actor’s techniques, and, for me, that even included those of Gary Oldman.

The story is, intriguing, not original, but alluring, and with that, you will definitely walk away with something.  Luckily, we got our tickets for free, so I don’t have that nagging feeling that I wasted money on it.

So beyond the resolution, the directing was also pretty amateurish in parts.  The effects were nice, but the camera seemed to literally tell you what the director was up to.  There is a scene where Gary Oldman is in a tower, waiting for the wolf, and he peers out at his lookouts, who all, in sequence with the camera, make a point to show themselves, and wave to Solomon (Oldman’s character).  It was like watching a high school play, and it was really there that I lost faith in the director.

In addition, the  story tries so hard to hide who the werewolf is, that you’re all over the place with theories.  Yes, in a way it’s great that they succeed in making you think it could be anyone in the town.  I applaud that, but because of the heightened attention to try to figure it out, it becomes immediately obvious who it is.  In my opinion that’s a fail.

Beyond that, I concur that the costumes were nice, although my wife and I discussed how their period is unclear, so it is difficult to determine a consistent time/geographical context.  Obviously, this also takes away from the believability.  Combine this with the high resolution recording, and you find a wicker basket containing Gary Oldman’s hand that looks like it was purchased at Wal-Mart for $4.79.

Furthermore, the writer sucks with dialog, but I think it’s satisfactorily compensated for with the story’s intrigue.  Out of 5, I’d probably give the movie a 3, I would see it again (for free), but it lacks a director with a vision to turn fantasy into a tangible universe.  It is this crack in the movie’s structure that gives the impression of unimpassioned acting, and therefore the ultimate frivolity of this movie’s universe.

 
No Comments

Posted in Reviews

 

A Change Of Pace: 803 calories, 4.20 miles, 42 minutes … on a Saturday

05 Feb

I’ve posted before about how playing basketball causes me to be anti-motivated to work out on Fridays.  Typically, I schedule 3 workouts a week: Monday, Wednesday, and Friday mornings before work.  Basketball, however, is Wednesday evenings, and, although I have rarely skipped the Wednesday workout the morning before, basketball puts me home late, I go to bed late, and I’m tired which tends to last into Friday mornings as well.  On top of that, I’m usually pretty sore.

That has been evolving, however, and I’d thought this week that I’d finally get that Friday workout in — really boost the stats.  After all, I’ve hit that 6 month mark, and I’m itching to see drastic results.

Well, Friday morning came and went, and although the soreness didn’t keep me away, the tiredness did.  However, my home projects, other than music, are relatively slim, so I decided my time would be better spent at work this Saturday.  I got in around noon, worked on a few things — mostly I just needed to get me upcoming road map in place, clear out itineral clutter (yes I just copyrighted that phrase).  That being done, I decided to go work out.

Things did not go as planned.  I started out, did my walk minute, sprint minute, walk minute, and into my first 8 minute mile, and I felt like I was going to die.  I was moving at 7.5 mph @ 0.5% incline, but I just couldn’t make it.  Four minutes in, I had to reduce the incline, and after the mile, I had to walk.  This seemed like a setback, because I’ve been doing 2 consecutive miles for a few weeks now.

Then I talked myself out of it.  I wasn’t even going to work out at all again until Monday, so this was just a freebie.  So, I walked a bit, and tried for that second mile.  Might as well try for 2, even if they aren’t consecutive right?

Well that lasted 2 minutes.  Side splitting, breathing issues, imminent death — you know the routine.  So I walked a bit more, and decided I had to hit 2 running miles even if they were slower.  I set the speed at 6mph (10 minute mile) @ 2% incline.  I made it a half mile.

A few minutes of walking, I was bound and determined.  After all, I’ve even been talking about going for 3 consecutive miles (and given that I just did 2 the other day for the first time ever, 3 would obviously be unprecedented) — could I really stop myself if I hadn’t even actually “run” for 2 total?

So, back to the 7.5mph again (8 minute mile).  I made it 4 minutes.  At this point, do the math, I was at 2.25 miles.  I walked a bit, and did another half mile at 7.5 (4 minutes).  I couldn’t make it any further, so I had to stop.  Well, the walking changed my mind, so I did yet another 2 minutes at 7.5.  Psychologically, I needed to be able to say I ran 3 miles, even if they weren’t back to back to back.

All said and done, given that I wasn’t in a rush (as I usually am — have to get done before everyone else shows up … can’t just go an hour every day), I ended up burning, according to the machine, 803 calories, walking/running 4.20 miles, all in 42 minutes.  That’s effectively 4.2 back to back to back to back 10 minutes miles.

That’s worth writing about.  I think I will when I get the time.

Oh, and all that was on top of adding some tricep exercises to my weight routine and 10 lbs. to my curl bar.  At this rate, I will be crazy super buff just in time for the Dolphins to win the Super Bowl again.

 
No Comments

Posted in Personal