Skip to content

Instantly share code, notes, and snippets.

@methodin
Created February 1, 2011 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methodin/806001 to your computer and use it in GitHub Desktop.
Save methodin/806001 to your computer and use it in GitHub Desktop.
Common functions
<?php
// Log to a file
function dump($data,$file='/var/log/mylog.log')
{
file_put_contents($file, $data."\n", FILE_APPEND);
}
// Use some locking for a cron job
function lockit($name)
{
$file = '/tmp/'.$name.'.lock';
$fp = fopen($file, 'w+');
if(!flock($fp, LOCK_EX | LOCK_NB)) {
dump('Could not get lock');
exit(-1);
}
return $fp;
}
// Handle creating a Mongo instance
function mongo()
{
$connection = new Mongo(DB);
$db = $connection->databasename;
$db->authenticate(DB_USER,DB_PASS);
return $db;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment