Archive for March, 2009

Run a cron job at multiple random times

Monday, March 30th, 2009

Say you want to run a cron job 10 times (or so) in a day at random times.

Here’s my solution:

1. Create a probability test that gives a 10% probability of the outcome you want

$p = mt_rand(1,10);

2. Add in a bit of extra randomness with a short sleep

$s = mt_rand(60,300);
sleep($s);

giving:

$p = mt_rand(1,10);

if ($p != 1) {

exit;

} else {

$s = mt_rand(60,300);
sleep($s);

Your stuff here…

}

3. Run your cron job 4 times an hour, 24 hours a day

*/15 * * * * /usr/bin/php myscript.php

At this, your cron job will run 96 times per day, and execute 1 in 10 of those times, which gives you 9 to 10 executions per day at random times.

OK, its not totally random, and you can’t guarantee the number of executions, but if you haveĀ  fixed number of executions per day, that’s not really random, is it?

Nod, nod, wink, wink….

Variables in Javascript Nested Functions

Thursday, March 19th, 2009

Like many programming/scripting languages, Javscript provides the ability to nest one function inside another function, a bit like this:

function outside () {

function inside() {

window.alert(’Hello World’);

}

}

This behaviour is most commonly seen when a developer needs to assign a function to an event that is created inside another function, for instance:

function outside () {

var menu =document.createElement(’SELECT’);

menu.onchange = function () {

window.alert(’Hello World’);

}

}

One of the difficulties that can arise here is that quite often it is necessary to pass a variable to the inner function. Say for instance, you want to create a 3 SELECTs, each having an onChange event which displays an alert unique to that SELECT. Would this work?

function outside () {

for (i=1;i<4;i++) {

var menu =document.createElement(’SELECT’);

menu.id = i;

menu.onchange = function (i) {

window.alert(’Menu’ + document.getElementById(i).id);

}

}

}

The simple answer is that no, it won’t.

What this execution will do is run the inner function at the same time as the outer function and assign the return value of the inner function to the ‘onchange’ property of the SELECT object, even if nothing is returned.

This isn’t what we want. What we want is for the inner function to be assigned to the onChange event of the SELECT.

There are couple of ways to do this, 3 of which are very well explained in this article.

Here’s one of them. What we do in this case is create a new property for the SELECT object, to which we assign the necessary variables, which we can then reference with the this object reference.

function outside () {

for (i=1;i<4;i++) {

var menu =document.createElement(’SELECT’);

menu.menuid = i;

menu.onchange = function () {

window.alert(’Menu’ + this.menuid);

}

}

}

This method isn’t without its problems either, but it will work in the majority of cases. If you want the Gold Standard solution, check out the referenced article above, preferably from a quiet, well-aired room with a nice view of distant snow-capped mountains (ie its a little complicated…)

Thoughts on Wordpress

Thursday, March 12th, 2009

Let’s be clear, Wordpress is great software. Its easy to install, its easy to use and it serves its purpose, blogging, very well.

However, the extent to which Wordpress is now being used to replace actual web development is getting ridiculous.

This really struck home to me recently when I saw an online shop based on Wordpress. It had a cart, a search function, a user database, the whole nine yards. I mean, come on, if you’re serious about selling stuff on the Internet, why on earth would you use blogging software as your shopping engine!!

A whole cottage industry also appears to have sprung up whereby so called Web Design companies and hacking free Wordpress templates and selling them on as bespoke brochure websites to unsuspecting customers.

This is pretty sharp practice. Wordpress templates are made available free by their designers to facilitate non-commercial use. Its never going to be possible to restrict their usage, but netiquette should dictate that these templates aren’t resold as bespoke designs.

Ultimately, growing user awareness will sort this out, which is always the Great Leveller on the Internet. At some stage, people are going to stop handing over good money to pay for something they can get for free on any number of community blogging sites.

And in the final analysis, bepsoke web development has never actually been easier. The availability of wonderful Javascript tools like the TinyMCE has brought CMS development well within the reach of even novice developers, so there should be no need to clutter up the web space with obsolete blogging functionality.

There it is then.

Wordpress for blogging, and that’s it.