❯~/pixelastic

Show and hide Flash elements without redraw

Imagine you have a Flash element in your HTML markup. If you apply any show/hide logic to it, the flash will be reset and played back to its first frame.

Actually, as soon as you change the display property of an element, if forces all inner Flash element to be restarted.

There are a few ways you can work around that, and here are the two main I'm using

Your Flash file is in a tab

Fairly common scenario, you have tabs on your page (maybe using jQuery UI or a custom code). When you display your Flash tab, all others are hidden, and when you display another tab, the Flash one is hidden.

Instead of using display:none / display:block, we'll be a little more sneaky and not really "hide" the element, but simply put it out of the user view.

Just apply the following CSS rule to the element you want to hide. It will just display it offscreen.

.tabHidden {
  position:absolute;
  top:0px;
  left:-9999em;
}

Don't forget to add a position:relative to the HTML parent of your tab for the absolute positionning to work.

You have multiple Flash files in a list

This one is trickier. Imagine you have a list of elements, and each row contains a Flash file. You want to filter the list based on various criterias and only show the rows matching.

You can't use the previous technique here because some elements of your list will be in relative positionning while others will be in absolute positionning. And everytime you change the position property of your element, the Flash will be reset.

The trick here is to play with your element dimensions and visibility. You can change the visibility without triggering a redraw. As this will only make the element invisible but still taking space, you just have to put its height and width to 0 to make it effectively disappear.

.listElementHidden {
  visibility:hidden;
  width:0px;
  height:0px:
  margin:0px;
}

The margin:0px is here to clear any margin you might have defined around your element.

Selecting checked radio button with jQuery and IE8

Ever tried to select the checked radio button of a form using jQuery? Well I did, and hundreds of time before, and never ran into any issues.

Today's the first time, and it involves my dear friend IE8. Seems like either jQuery or IE8 had trouble with my radio button selection.

I finally managed to get what I wanted but with a sightly different syntax for IE8.

First, the markup

<form id="myForm">
  <input name="data[Payment][value]" id="payment_1500" type="radio" value="1500" />
  <label for="payment_1500">1500</label><br />

  <input name="data[Payment][value]" id="payment_2000" type="radio" value="2000" />
  <label for="payment_2000">2000</label><br />

  <input name="data[Payment][value]" id="payment_5000" type="radio" value="5000" />
  <label for="payment_5000">5000</label><br />

  <button id="test">Select</button>
</form>

Pretty simple, isn't it? I only have three radio buttons, and I would like to get the selected value when pressing the Select button.

What should work everywhere

The following code is pretty straightforward and I expected it to just work.

var selected1 = $('#myForm input[name="data\\[Payment\\]\\[value\\]"]:checked');
var value1 = selected1.val();
console.log(value1);

Note that we have to double escape the [ and ] characters and wrap in quotes the name value. Nothing fancy, just classic string protection. This code works perfectly on Firefox and Chrome, and I deployed it in production for a few weeks.

Then, I got report of users that would have love to use the form, but got an error because no value was selected. I tested and tested it again without finding the cause. Then it occurs to me that all those reports came from user using IE8.

So I rebooted my VM, launched IE8 and was able to reproduce the bug on my first try.

What the heck is IE8 doing?

Well, that's a deep question, and I've ask this myself countless times before. Once more, IE is doing things in its own weird way.

After some fiddling, I managed to make it work, by just slightly altering the syntax.

var selected2 = $('#myForm input[name="data\\[Payment\\]\\[value\\]"]').filter(':checked');
var value2 = selected2.val();
console.log(value2); 

Yep, that's right, I simply moved the :checked selector in its own filter call and it worked. Took me a while to figure, but this finally turned out to be an easy fix.

You can test it yourself with this jsFiddle example. Don't forget to enable the log panel in IE8 by pressing F12 before running the code.

Enabling Flash calls to distant server when working locally

Default Flash security config prevents you from loading content from an external website when running a Flash file locally.

To prevent this, you have to go to [this link](http://www.macromedia.com/suppo rt/documentation/en/flashplayer/help/settings_manager04.html) and add the directory where your file is located.

Fortunatly, you can specify a top level directory and all subdirectories will inherit the authorization.

If you don't do this step, your Flash file will refuse to download external content.

First steps with puppet

I've never used puppet before this project. It in installed on our main server, monitoring the whole pool of servers used to host our game. It includes php hosts, sql data nodes and sql front servers as well as a couple of load balancer.

Puppet keeps a snapshot of the config that must be deployed on each server and keep them in sync. We just have to edit the puppet files, and the same config is replicated on each server.

I did not install puppet, our sysadmin did it, and I have no idea how he did it actually, I'm just fiddling with specific configurations here. But as it took me quite a bit to grasp, here is how it works, and some quirks to be aware of.

The puppet files

Puppet is installed in /etc/puppet. Our different modules are stored in /etc/puppet/modules. A module is a set of programs and config files that work together.

For example, we have one for the loadbalancers, one for the http part, one for mysql, and even one for vim. I'm going to focus on the http one, because it contains config files for memcache, lighttpd and php.

Modules are split in two parts: the manifest and the files. You can find each in /etc/puppet/modules/{modulename}/manifests and in /etc/puppet/modules/{modulename}/files.

As you can imagine, the manifest contains a list of directive telling puppet what programs need to be installed, what directories need to be present (with specific user and group) as well as what files need to be copied.

The files/ directory contains all the files that puppet might need for this module. It will copy them from this directory to the final servers, according to the manifest config.

Manifest syntax

The manifest uses a very simple syntax, using blocks of key => value to define config. I've mostly use the simple file directives, to copy files and create directories.

One can also use package directories, to install things and handle dependencies, but I haven't messed with that (yet) so I won't talk about them here.

Here are a couple of simple file orders:

file { "/etc/lighttpd/scripts":
  ensure  => directory,
  owner   => root,
  group   => root,
  mode    => 550
}
file { "/etc/lighttpd/lighttpd.conf":
  owner   => root,
  group   => root,
  mode    => 644,
  source  => "puppet:///http/$target/lighttpd/lighttpd.conf",
  require => Package["lighttpd"],
  notify  => Service["lighttpd"],
}

The first part will create a /etc/lighttpd/scripts directory on the final server, with a user and group root and 550 permissions mode.

The second part will copy the file stored in /etc/puppet/modules/http/{target}/lighttpd/lighttpd.conf on the puppet server to /etc/lighttpd/lighttpd.conf on the final server. I'm not quite sure what the require and notify lines are for.

$target is a special variable. In my case it can take either the value valid or prod, allowing me to define custom config based on the environment. Valid and prod environment are supposed to have the exact same config, but separating them allow for testing a config change in one environment without potentially breaking the working prod environment. I'm not exactly sure how this $target var is set, this is one more thing our sysadmin did. Thanks to him.

Pulling changes

Puppet is configured to check its servers every 30mn. If some config files changed, or a directory isn't there anymore, it will recreate them, according to the manifest. But you can force the pulling of changes from the final servers themselves. Just log to one of the servers puppet is monitoring and type

$ sudo puppetd --test

The --test argument is a bit misleading. It actually does not seem to do any test at all, it simply pulls the puppet files from the puppet server and updates its files accordingly. It will notify you of any errors it might find, and even print a diff of the files changed.

One last thing

I've lost 30mn checking and re-checking my puppet file because my php.ini changes weren't updated. In fact, I just forgot to reload lighttpd...

PHP session on disk with lighttpd, and write access

On HappyLife, we used to use Memcache to handle the php sessions. We discovered a while back after much trouble that Memcache might not have been the best solution for sessions on a high traffic site. But this will be the subject of another post.

Anyway, I switched back to classical session, stored on disk.

Here's the relevant php.ini config

[Session]
session.save_handler = files
session.save_path = '/tmp/php'

/tmp/php should be writable by www-data, so

$ chown www-data:www-data /tmp/php