Semaphores to avoid critical task collision

Standard

This is a sample from my cakephp shell script that has some critical processing. It is ran every second and sometimes the script can take over a second to process. Thus, I needed a way to avoid the script from accessing the same critical business logic and manipulating data.

The semaphore works by acquiring the key with the given id: 1068. I have set the max number of semaphores to 1 for the key. All other shell scripts that are ran will autorelease when they cannot acquire the semaphore (hence another shell script is still processing).

Forking a running process and returning values

Standard

Using pcntl_fork() allows for forking of a running processing in PHP. In this example I fork my shell script and process data and return the results. This would be useful in a case where you had to make api calls to lets say 4 vendors: amazon, itunes, youtube, flickr. You would assign each child id a task to run. They each would then write there results to a shared memory location for further manipulation. I have written this test in CakePHP shell script.

I would not recommend running this script inside of CakePHP. It may cause issues with the caching.

Couchbase Datasource

Standard

I ran into a problem where I needed to utilize a NoSQL database. Since CakePHP is more fit for relational style database (MYSQL) I had to create a custom datasource (link). The following datasource allows models to have basic functionality of Couchbase Server.

Example Usage:

Setup / Configuration:

To use the couchbaseSource you must specify it in your model. Do so by adding the following.
YourModel.php

You can also programmatically change datasouces / database on the fly. For example, sometimes I want to use my default MYSQL datasource. This can be accomplished like so:

To use be able to use this datasource it must be added to cakePHP list of available datasources.
Database.php

app/Model/Datasource/CouchbaseSource.php

Converting a json object to an array

Standard

Using restful web services relies on json results. You need your json object to be decoded and then converted to a array. Using the set library from CakePHP we can accomplish this. In this example I send a message to Android push gateway, then attempt to read the result.

The data variable now contains an array of information ready to parse.

Memcached optimization

Standard

Swapping out CakePHP’s default file logging for memcache can greatly improve performance. In my case I use couchbase server to manage NoSQL. From there administrative panel you can see every time your app has a page refresh.

Couchbase Server default bucket

Memcached optimization with CakePHP 2.x

To connect CakePHP with memcached place this in your app/Config/core.php

Your application will now benefit from using memcache instead of a standard logging system. In the event that you make major changes to you application you may need to “flush” the memcache bucket to clear the cached schema stored by CakePHP.

Running shells as repeating cronjobs

Standard

It is common to use shells to handle background tasks on your server. Sometimes that task needs to be executed as quickly as possible. For example, you have a queue of tasks that need to process as soon as entered.

CakePHP can be configured on Unix servers to run a modified shell script via crontab.

Begin by modifying the defaultShell that comes with cake. I have created a new script call secondShell.

The modifications make this shell run ever 2 seconds for 30 occurrences (2 x 30 = 60 seconds)

To run this shell place it in the crontab by using command “crontab -e” in terminal.

The */1 tells the scheduler to run the CakePHP Shell script every minute. Learn more about cron job scheduling

Wildcard Subdomain Prerouting

Standard

Ever have a need to have wildcard subdomains on your CakePHP application? This is the solution to that problem. Assume all subdomains have there own template folder, and the subdomains need to be able to be created and modified on the fly.

Create a subdomain model, as well as a table in your database called Subdomains with the following schema

Initially, the URL is used to extract the subdomain and the host name from the clients entry point. The contents are then assigned to an array in a custom variable using the internal cache. The bootstrap is called within the dispatcher stage of the MVC architecture as shown below.

MVC Structure

CakePHP MVC Structure

Bootstrap.php

The subdomain is now passed through the application and can be accessed by configure::read. A custom library called SubdomainRoute writes values to the cache and the router then reads the results. If a template is set after the SubdomainRoute is initialized it assumes the client is accessing our application on a valid subdomain found in the database.

Routes.php

The subdomain is determined by accessing the mysql subdomain table. The results are stored and later used for routing (above), as well as assigning a template folder for a particular subdomain (below).

app/Lib/Routes/SubdomainRoute.php

The final step is to tell cake which template the subdomain is associated too. This assumes that you have created the templates folder in the directory.

AppController.php