Skip to content

Instantly share code, notes, and snippets.

View boutell's full-sized avatar

Tom Boutell boutell

View GitHub Profile
@boutell
boutell / loadwatch.php
Created October 15, 2012 22:22
CPU load watcher: sends email when the load average changes by at least one full point
<?php
// Install me via cron:
// MAILTO=somewhere@important.com
// * * * * * /usr/local/bin/php /home/someaccount/loadwatch.php
$fields = explode(' ', file_get_contents('/proc/loadavg'));
$avg = $fields[0];
$last = @file_get_contents("/home/gps/cpulast.txt");
@boutell
boutell / assignhyperthreads.php
Created October 4, 2012 14:31
Spread out your fastcgi processes among the hyperthreads and cores of your CPU
<?php
// This script will spread out your php-cgi fastcgi processes
// evenly over the hyperthreads of your server.
// If your php-cgi process is not /usr/local/bin/php-cgi,
// tweak accordingly.
//
// tom@punkave.com, @boutell, punkave.com
@boutell
boutell / checkhyperthreads.php
Created October 4, 2012 14:12
Check distribution of fastcgi processes over the hyperthreads and cores of your server
<?php
// Are your fastcgi processes spread out evenly over the available hyperthreads on your server?
// This script will tell you.
//
// If your php-cgi process is not /usr/local/bin/php-cgi, tweak accordingly.
//
// In my experience they do tend to distribute pretty well over time, but see also
// assignhyperthreads.php.
//
// Why does this print 'Redirecting' but then send 'You are special' anyway?
function secure(req, res, next) {
if (!req.user) {
console.log('Redirecting');
req.session.afterLogin = req.url;
res.redirect('/login');
return;
}
else
{
@boutell
boutell / gist:3183250
Created July 26, 2012 17:06
Deleting search objects related many-to-one to a given posting
// In my Search entity class:
/**
* @ORM\ManyToOne(targetEntity="Posting", inversedBy="words")
* @ORM\JoinColumn(name="posting_id", referencedColumnName="id", onDelete="cascade")
*/
protected $posting;
// Later, when I want to delete the Search objects for a given Posting entity:
@boutell
boutell / gist:3151698
Created July 20, 2012 16:30
Count rows without throwing an exception if there are none
// If there are no matches, getSingleScalarResult will throw an exception
try
{
$this->numResults = $countQb->getQuery()->getSingleScalarResult();
} catch (\Doctrine\ORM\NoResultException $e)
{
// This happens when zero rows are returned
$this->numResults = 0;
}
macintosh-5:~ boutell$ set | grep PATH
MANPATH=/opt/local/share/man:
PATH=/Users/boutell/mongodb/bin:/usr/local/bin:.:/Users/boutell/bin:/Users/boutell/.gem/bin:/opt/symfony1.4/data/bin:/opt/local/bin:/opt/local/sbin:/opt/local/apache2/bin:/Users/boutell/personalassistant/festival/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/pktools/bin
macintosh-5:~ boutell$
@boutell
boutell / gist:2764357
Created May 21, 2012 20:10
Rasmus test
<?php
define('NUM_TESTS', 1000);
$before = memory_get_usage(true);
$test = array();
class Foo
{
@boutell
boutell / short_slugs_for_mongoose.js
Created April 15, 2012 01:42
Guarantee short yet unique slugs for any model
// Extend any Mongoose model that has a unique index on a 'slug' field so that
// if a unique index error occurs on 'slug', a random digit is added to the slug
// and the save operation is retried until it works. This is concurrency-safe even
// if you have lots of inserts going on from multiple machines etc.
//
// By Tom Boutell, tom@punkave.com
//
// NOTE: YOU MUST HAVE THIS PULL REQUEST... https://github.com/LearnBoost/mongoose/pull/837
//
// (As of this writing that commit is not in LearnBoost/mongoose yet.)
@boutell
boutell / mongoose-save-error-test-2.js
Created April 14, 2012 19:52
New Mongoose retry-when-save-fails test
/**
* Create a situation where a unique index error will occur. Try to save the
* object again after making the relevant field unique. Then do an update on one of the objects.
*
* Expected behavior: each object after the first saves successfully the second (or third...) time and we wind
* up with ten objects. Actual behavior with current Mongoose: we wind up with one object because the
* isNew flag is never reset to true when an insert fails. Behavior with my pull request: the isNew flag
* is reset if an insert (not an update) fails, allowing save handlers to try again on error after doing things
* like making slugs more unique.
*