• Date: July 14, 2011
  • Author: Slawek Lukasiewicz
  • Comments: No Comments
  • Category: Linux

Bash script: do something when server is responding too slow

Some time ago I had to write script that should perform some actions when server is responding too slow. I had little time for this task, so I wrote simple bash script using cURL, and set is as Cron job.

curl -s URL --max-time 5 > /dev/null 2> /dev/null
if [ $? -eq 28 ]
then
 #perform some
 # actions
fi

In above script –max-execution option is used. With this option if connection lasts longer than n seconds (in my example 5 seconds) curl stops and return code 28.

  • Date: June 27, 2011
  • Author: Slawek Lukasiewicz
  • Comments: No Comments
  • Category: MySQL

MySQL: custom sort order

Usually when we need to sort query result we use ORDER BY clause with interesting column. In most cases this will be sufficient, but sometimes we need to sort result in custom way. MySQL provides us some capabilities to perform this operation.
Continue Reading →

  • Date: June 25, 2011
  • Author: Slawek Lukasiewicz
  • Comments: 2 Comments
  • Category: PHP

Method chaining

Method chaining technique provides possibility to invoke method on returned object.

$someObject->doSomethingAndReturnObject()->invokeMethodOnReturnedObject();

Method chaining is a technique familiar to everyone using PHP libraries like Doctrine. It can make code cleaner and more readable.

class Calculator
{
 
	private $_amount = 0;
 
	public function add($amount)
	{
		$this->_amount += $amount;
 
		return $this;
	}
 
	public function multiply($amount)
	{
		$this->_amount *= $amount;
 
		return $this;
	}
 
	public function getAmount()
	{
 
		return $this->_amount;
	}
 
	public function __toString()
	{
 
		return (string)$this->getAmount();
	}
}
 
$calculatorInstance = new Calculator();
$calculatorInstance->add(5)->multiply(10);
print $calculatorInstance;
# 50

This solution is also known as Fluent interface pattern.

In example above methods return current object, but they can return different object instead.

$instanceOfClassA->doSomethingAndReturnInstanceOfClassB()->invokeMethodOnInstanceOfClassB();