Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created February 9, 2013 01:54
Show Gist options
  • Save clouddueling/4743472 to your computer and use it in GitHub Desktop.
Save clouddueling/4743472 to your computer and use it in GitHub Desktop.
running a worker with phpconsole and submitting curl requests on the fly
<?php
class Home_Controller extends Base_Controller
{
public function get_run()
{
phpconsole("WORKER: Initializing the CloudWorker.");
$love = true;
while ($love) {
// Get queue
$queues = Queue::where_status('pending')
->where('process_at', '<=', date('Y-m-d H:i:s'))
->get();
phpconsole("WORKER: Queues: " . count($queues));
// process queue by type
foreach ($queues as $queue) {
phpconsole("WORKER: Starting queue item #{$queue->id}: {$queue->type}");
switch ($queue->type) {
case 'create_contacts_from_csv' :
try {
$queue->download_file();
if (! $queue->to_contacts())
throw new Exception("Could not convert csv file to contacts.");
if (! $queue->delete_local_file())
throw new Exception('Could not delete local file.');
if (! $queue->delete_s3_file())
throw new Exception('Could not delete S3 file.');
$queue->completed();
phpconsole("WORKER: Queue item #{$queue->id} complete.");
} catch(Exception $e) {
phpconsole("ERROR: " . $e->getMessage());
mail($e->getMessage(), 'ERROR', $admin_email);
$queue->status = 'error';
$queue->message = $e->getMessage();
$queue->save();
}
break;
}
}
phpconsole("WORKER: Worker cycle complete.");
phpconsole_shutdown();
phpconsole_reset();
sleep(30);
}
}
}
<?php
// rest of file
function phpconsole_reset() {
phpconsole_settings();
$GLOBALS['phpconsole_snippets'] = array();
$GLOBALS['phpconsole_counters'] = array();
register_shutdown_function('phpconsole_shutdown');
$GLOBALS['phpcounter_initialized'] = false;
}
// rest of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment