A typical Developer Blog
by Gordon Franke
Icon

How can i run multiple instances of memcached on one server?

Problem

I need more then one memcached server.

Solution

Virtual machine

I can build for each memcached server a virtal machine.

Problems

  • more memory (system+memcached)
  • one ip address for each instance
  • more work

Multiple instances via ports

For this it needs to patch the init.d and start-memcached scripts to run multiple instances.

After the patch it search for configuration files that match the following pattern /etc/memcached_*.conf. When it found no file it use the /etc/memcached.conf configuration file. So it breaks no backward compatibility.

When you want to create 2 memached instances copy the configuration file and change the port, memory …

cp /etc/memcached.conf /etc/memcached_myserver_1.conf
cp /etc/memcached.conf /etc/memcached_myserver_2.conf

Commands

start all servers

/etc/init.d/memcached start

start only a specific server

/etc/init.d/memcached start myserver_1

stop all servers

/etc/init.d/memcached stop

stop only a specific server

/etc/init.d/memcached stop myserver_1

The patches

/usr/share/memcached/scripts/start-memcached

26,30d25
> if (scalar(@ARGV) == 2) {
> 	$etcfile = shift(@ARGV);
> 	$pidfile = shift(@ARGV);
> }
>

/etc/init.d/memcached

16a17
> DAEMONNAME=memcached
18d18
< NAME=memcached
20d19
< PIDFILE=/var/run/$NAME.pid
26a26,63
> FILES=(/etc/memcached_*.conf);
> # check for alternative config schema
> if [ -r "${FILES[0]}" ]; then
>   CONFIGS=();
>   for FILE in "${FILES[@]}";
>   do
>     # remove prefix
>     NAME=${FILE#/etc/};
>     # remove suffix
>     NAME=${NAME%.conf};
>
>     # check optional second param
>     if [ $# -ne 2 ];
>     then
>       # add to config array
>       CONFIGS+=($NAME);
>     elif [ "memcached_$2" == "$NAME" ];
>     then
>       # use only one memcached
>       CONFIGS=($NAME);
>       break;
>     fi;
>   done;
>
>   if [ ${#CONFIGS[@]} == 0 ];
>   then
>     echo "Config not exist for: $2" >&2;
>     exit 1;
>   fi;
> else
>   CONFIGS=(memcached);
> fi;
>
> CONFIG_NUM=${#CONFIGS[@]};
> for ((i=0; i < $CONFIG_NUM; i++)); do
>   NAME=${CONFIGS[${i}]};
>   PIDFILE="/var/run/${NAME}.pid";
>
30c67
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
50c87
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
54c91
<       N=/etc/init.d/$NAME
---
>       N=/etc/init.d/$DAEMONNAME
59a97
> done;

How can i use embed forms or simgle form fields depend on a form field?

You have a register form with a field account_type and two embed forms Company and Address.

You want remove the embed Company form, when the field account_type is sfGuardUserProfile::TYPE_PRIVATE otherwise you want remove the embed Address form.

The solution is to override the bind method in your form.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    // unset request param and validator
    if($taintedValues['account_type'] == sfGuardUserProfile::TYPE_PRIVATE)
    {
      unset(
        $taintedValues['Company'],
        $this->validatorSchema['Company']
      );
    }
    else
    {
      unset(
        $taintedValues['Address'],
        $this->validatorSchema['Address']
      );
    }
 
    parent::bind($taintedValues, $taintedFiles);
  }

How can i check the count of database queries in my unit test?

1
2
3
$manager    = new sfDatabaseManager($configuration);
$connection = $manager->getDatabase('doctrine')->getDoctrineConnection();
$connection->count();

How can i call a symfony event from doctrine model?

1
2
$dispatcher = sfProjectConfiguration::getActive()->getEventDispatcher();
$dispatcher->notify($event);

How can i deactivate all checkboxes on a website with javascript?

1
2
3
4
5
6
7
8
inputs = document.getElementsByTagName('input');
for(i=0; inputs.length > i;i++)
{
  if(inputs[i].type == 'checkbox')
  {
    inputs[i].checked= false;
  }
}

How can i mark all required form fields with a *?

create file lib/gfFormHerlp.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
 
/**
 * Some usefull form helper
 *
 * @package    symfony
 * @subpackage helper
 * @author     Gordon Franke <gfranke@nevalon.de>
 * @link       http://www.nevalon.de
 */
class gfFormHelper
{
  /**
   * Add * to required field labels
   *
   * @param sfForm $form
   * @param string $symbol
   * @param string $title
   *
   * @return void
   */
  public static function addRequiredToLabel(sfForm $form, $symbol = '*', $title = 'This field is mandatory.')
  {
    $widgetSchema = $form->getWidgetSchema();
    $validatorSchema = $form->getValidatorSchema();
 
    foreach($form->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
    {
      $label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName();
      if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true) {
        $label .= '<sup title="' . $widgetSchema->getFormFormatter()->translate($title) . '">' . $symbol . '</sup>';
      }
      $widgetSchema->setLabel($key, $label);
    }
  }
}

add at the end of your form setup or better when you have subforms into the configure method this line

1
gfFormHelper::addRequiredToLabel($this);

How can i unit test my own User class functions?

Use this in your testcode to create the user object

1
2
3
4
5
6
7
8
$_SERVER['session_id'] = 'test';
 
$dispatcher = new sfEventDispatcher();
$sessionPath = sfToolkit::getTmpDir().'/sessions_'.rand(11111, 99999);
$storage = new sfSessionTestStorage(array('session_path' => $sessionPath));
 
$myUser = new myUser($dispatcher, $storage);
$myUser->setAttribute('user_id', 1, 'sfGuardSecurityUser');