Skip to content

Commit 0898f25

Browse files
committed
Background processing with gearman
1 parent 0ed6895 commit 0898f25

10 files changed

Lines changed: 457 additions & 39 deletions

File tree

‎Config/bootstrap.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
CakePlugin::load('Migrations');
112112
CakePlugin::load('Crud');
113113
CakePlugin::load('Elastic');
114+
CakePlugin::load('Gearman');
114115

115116
if (php_sapi_name() !== 'cli' && Configure::read('debug')) {
116117
App::uses('CakeEventManager', 'Event');

‎Console/Command/SyncShell.php‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
App::uses('AppShell', 'Console/Command');
3+
class SyncShell extends AppShell {
4+
5+
public $uses = ['Company'];
6+
7+
public $tasks = ['Gearman.GearmanWorker'];
8+
9+
public function getOptionParser() {
10+
return parent::getOptionParser()
11+
->addSubCommand('worker', [
12+
'help' => 'Creates a worker process'
13+
])
14+
->addSubCommand('company', [
15+
'help' => 'Syncs company information from facebook',
16+
'parser' => [
17+
'arguments' => [
18+
'id' => [
19+
'help' => 'The company id',
20+
'required' => true
21+
]
22+
]
23+
]
24+
]);
25+
}
26+
27+
public function worker() {
28+
$this->GearmanWorker->addFunction('sync_company', $this, 'processCompanyJob');
29+
$this->GearmanWorker->work();
30+
}
31+
32+
public function processCompanyJob($data) {
33+
$this->args[0] = $data;
34+
$this->company();
35+
}
36+
37+
public function company() {
38+
$data = $this->Company->find('first', ['conditions' => ['id' => $this->args[0]]]);
39+
$facebookId = $data['Company']['facebook_id'];
40+
41+
try {
42+
$facebook = new Facebook(Configure::read('Facebook'));
43+
$company = $facebook->api('/' . $facebookId);
44+
} catch (Exception $e) {
45+
$this->error('Something bad happened: ' . $e->getMessage());
46+
}
47+
48+
$data = $data['Company'];
49+
foreach ($company['location'] as $key => $location) {
50+
if (empty($location)) {
51+
continue;
52+
}
53+
$data[$key] = $location;
54+
}
55+
56+
if (isset($company['website'])) {
57+
$data['website'] = $company['website'];
58+
}
59+
60+
$this->Company->create();
61+
if ($this->Company->save($data, ['callbacks' => false])) {
62+
$this->log(sprintf('Company %s data was synced from facebook', $this->Company->id), 'info');
63+
}
64+
}
65+
66+
}

‎Event/EventCentral.php‎

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
App::uses('CakeEventListener', 'Event');
4+
App::uses('GearmanQueue', 'Gearman.Client');
5+
46
class EventCentral implements CakeEventListener {
57

68
/**
@@ -11,7 +13,8 @@ class EventCentral implements CakeEventListener {
1113
*/
1214
public function implementedEvents() {
1315
return array(
14-
'Model.beforeSave' => 'processFacebookData'
16+
'Model.beforeSave' => 'processCompanies',
17+
'Model.afterSave' => 'processFacebook'
1518
);
1619
}
1720

@@ -23,7 +26,7 @@ public function implementedEvents() {
2326
* @param CakeEvent $event
2427
* @return boolean
2528
*/
26-
public function processFacebookData(CakeEvent $event) {
29+
public function processCompanies(CakeEvent $event) {
2730
if ($event->subject() instanceof User) {
2831
$user = $event->subject();
2932
$data = $user->data[$user->alias];
@@ -46,37 +49,23 @@ public function processFacebookData(CakeEvent $event) {
4649
continue;
4750
}
4851

49-
try {
50-
$facebook = new Facebook(Configure::read('Facebook'));
51-
$company = $facebook->api('/' . $workPlace['employer']['id']);
52-
} catch (Exception $e) {
53-
die('An error occured');// @todo handle this
54-
}
55-
5652
$user->Company->create();
5753
$data = [
5854
'name' => $workPlace['employer']['name'],
5955
'facebook_id' => $workPlace['employer']['id']
6056
];
61-
62-
foreach ($company['location'] as $key => $location) {
63-
if (empty($location)) {
64-
continue;
65-
}
66-
67-
$data[$key] = $location;
68-
}
69-
70-
if (isset($company['website'])) {
71-
$data['website'] = $company['website'];
72-
}
73-
7457
$user->Company->save($data);
75-
7658
$ids[] = $user->Company->id;
7759
}
7860
$user->data['Company']['Company'] = $ids;
7961
return true;
8062
}
8163
}
64+
65+
public function processFacebook($event) {
66+
if ($event->subject() instanceof Company) {
67+
GearmanQueue::execute('sync_company', $event->subject()->id);
68+
}
69+
}
70+
8271
}

‎View/Companies/add.ctp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<div class="companies form">
2+
<?php echo $this->Form->create('Company'); ?>
3+
<fieldset>
4+
<legend><?php echo __('Add Company'); ?></legend>
5+
<?php
6+
echo $this->Form->input('name');
7+
echo $this->Form->input('street');
8+
echo $this->Form->input('city');
9+
echo $this->Form->input('state');
10+
echo $this->Form->input('country');
11+
echo $this->Form->input('zip');
12+
echo $this->Form->input('latitude');
13+
echo $this->Form->input('longitude');
14+
echo $this->Form->input('website');
15+
echo $this->Form->input('user_count');
16+
echo $this->Form->input('facebook_id');
17+
echo $this->Form->input('User');
18+
?>
19+
</fieldset>
20+
<?php echo $this->Form->end(__('Submit')); ?>
21+
</div>
22+
<div class="actions">
23+
<h3><?php echo __('Actions'); ?></h3>
24+
<ul>
25+
26+
<li><?php echo $this->Html->link(__('List Companies'), array('action' => 'index')); ?></li>
27+
<li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li>
28+
<li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li>
29+
</ul>
30+
</div>

‎View/Companies/edit.ctp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<div class="companies form">
2+
<?php echo $this->Form->create('Company'); ?>
3+
<fieldset>
4+
<legend><?php echo __('Edit Company'); ?></legend>
5+
<?php
6+
echo $this->Form->input('id');
7+
echo $this->Form->input('name');
8+
echo $this->Form->input('street');
9+
echo $this->Form->input('city');
10+
echo $this->Form->input('state');
11+
echo $this->Form->input('country');
12+
echo $this->Form->input('zip');
13+
echo $this->Form->input('latitude');
14+
echo $this->Form->input('longitude');
15+
echo $this->Form->input('website');
16+
echo $this->Form->input('user_count');
17+
echo $this->Form->input('facebook_id');
18+
echo $this->Form->input('User');
19+
?>
20+
</fieldset>
21+
<?php echo $this->Form->end(__('Submit')); ?>
22+
</div>
23+
<div class="actions">
24+
<h3><?php echo __('Actions'); ?></h3>
25+
<ul>
26+
27+
<li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Company.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('Company.id'))); ?></li>
28+
<li><?php echo $this->Html->link(__('List Companies'), array('action' => 'index')); ?></li>
29+
<li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li>
30+
<li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li>
31+
</ul>
32+
</div>

‎View/Companies/index.ctp‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<div class="companies index">
2+
<h2><?php echo __('Companies'); ?></h2>
3+
<table cellpadding="0" cellspacing="0">
4+
<tr>
5+
<th><?php echo $this->Paginator->sort('id'); ?></th>
6+
<th><?php echo $this->Paginator->sort('name'); ?></th>
7+
<th><?php echo $this->Paginator->sort('street'); ?></th>
8+
<th><?php echo $this->Paginator->sort('city'); ?></th>
9+
<th><?php echo $this->Paginator->sort('state'); ?></th>
10+
<th><?php echo $this->Paginator->sort('country'); ?></th>
11+
<th><?php echo $this->Paginator->sort('zip'); ?></th>
12+
<th><?php echo $this->Paginator->sort('latitude'); ?></th>
13+
<th><?php echo $this->Paginator->sort('longitude'); ?></th>
14+
<th><?php echo $this->Paginator->sort('website'); ?></th>
15+
<th><?php echo $this->Paginator->sort('user_count'); ?></th>
16+
<th><?php echo $this->Paginator->sort('facebook_id'); ?></th>
17+
<th><?php echo $this->Paginator->sort('created'); ?></th>
18+
<th><?php echo $this->Paginator->sort('updated'); ?></th>
19+
<th class="actions"><?php echo __('Actions'); ?></th>
20+
</tr>
21+
<?php foreach ($companies as $company): ?>
22+
<tr>
23+
<td><?php echo h($company['Company']['id']); ?>&nbsp;</td>
24+
<td><?php echo h($company['Company']['name']); ?>&nbsp;</td>
25+
<td><?php echo h($company['Company']['street']); ?>&nbsp;</td>
26+
<td><?php echo h($company['Company']['city']); ?>&nbsp;</td>
27+
<td><?php echo h($company['Company']['state']); ?>&nbsp;</td>
28+
<td><?php echo h($company['Company']['country']); ?>&nbsp;</td>
29+
<td><?php echo h($company['Company']['zip']); ?>&nbsp;</td>
30+
<td><?php echo h($company['Company']['latitude']); ?>&nbsp;</td>
31+
<td><?php echo h($company['Company']['longitude']); ?>&nbsp;</td>
32+
<td><?php echo h($company['Company']['website']); ?>&nbsp;</td>
33+
<td><?php echo h($company['Company']['user_count']); ?>&nbsp;</td>
34+
<td><?php echo h($company['Company']['facebook_id']); ?>&nbsp;</td>
35+
<td><?php echo h($company['Company']['created']); ?>&nbsp;</td>
36+
<td><?php echo h($company['Company']['updated']); ?>&nbsp;</td>
37+
<td class="actions">
38+
<?php echo $this->Html->link(__('View'), array('action' => 'view', $company['Company']['id'])); ?>
39+
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $company['Company']['id'])); ?>
40+
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $company['Company']['id']), null, __('Are you sure you want to delete # %s?', $company['Company']['id'])); ?>
41+
</td>
42+
</tr>
43+
<?php endforeach; ?>
44+
</table>
45+
<p>
46+
<?php
47+
echo $this->Paginator->counter(array(
48+
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
49+
));
50+
?> </p>
51+
<div class="paging">
52+
<?php
53+
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
54+
echo $this->Paginator->numbers(array('separator' => ''));
55+
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
56+
?>
57+
</div>
58+
</div>
59+
<div class="actions">
60+
<h3><?php echo __('Actions'); ?></h3>
61+
<ul>
62+
<li><?php echo $this->Html->link(__('New Company'), array('action' => 'add')); ?></li>
63+
<li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li>
64+
<li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li>
65+
</ul>
66+
</div>

‎View/Companies/map.ctp‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,41 @@
88
}
99
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
1010

11-
$.getJSON('/users.json', function(data) {
12-
$.each(data.data, function(k, v) {
13-
$.each(v.User.companies, function(k, val) {
14-
var position = new google.maps.LatLng(val.location.lat, val.location.lon);
15-
new google.maps.Marker({
16-
position: position,
17-
map: map,
18-
title: val.name
11+
function load() {
12+
$.getJSON('/users.json', function(data) {
13+
$.each(data.data, function(k, v) {
14+
$.each(v.User.companies, function(k, val) {
15+
var position = new google.maps.LatLng(val.location.lat, val.location.lon);
16+
new google.maps.Marker({
17+
position: position,
18+
map: map,
19+
title: val.name
20+
});
1921
});
2022
});
2123
});
24+
}
25+
26+
google.maps.event.addListener(map, "dragend", function(){
27+
load();
2228
});
29+
30+
function getBorders() {
31+
var pointA = {
32+
lat:map.getBounds().getNorthEast().lat(),
33+
lng:map.getBounds().getSouthWest().lng()
34+
};
35+
var pointB= {
36+
lat:map.getBounds().getSouthWest().lat(),
37+
lng:map.getBounds().getNorthEast().lng()
38+
};
39+
var b = pointB.lat + "," + pointB.lng;
40+
var a= pointA.lat + "," + pointA.lng;
41+
return {southEast:b, northWest:a}
42+
}
43+
load();
2344
}
24-
45+
2546
google.maps.event.addDomListener(window, 'load', initialize);
2647
</script>
2748

0 commit comments

Comments
 (0)