CoderKungfu / PHP-Queue-Windows-Azure-Queue-Demo

Demo app for Windows Azure Service Bus Queue backend.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#PHP-Queue Windows Azure Queue Demo

Demo app for Windows Azure Service Bus Queue backend.

Sample code from my presentation at The Singapore Xmas Azure Workshop on 20 Dec 2012. Here are the presentation slides.

##Setup Instructions

###Pre-requisites

  1. Register for a Windows Azure account. You can use the free 90 days trial here.

  2. Create a Windows Azure website (tutorial here).

  3. Create a Windows Azure Service Bus Namespace (tutorial here).

  4. Create a Windows Azure Blob Storage Account (tutorial here).

  5. Create 2 new Containers: photosupload and photoscdn.

  6. Download the sample code.

    $ git clone git://github.com/miccheng/PHP-Queue-Windows-Azure-Queue-Demo.git
  7. Download and install Composer

    $ curl -s "http://getcomposer.org/installer" | php
  8. Download the Windows Azure SDK & PHP-Queue via Composer.

    $ php composer.phar install

###Environment Variables

You have to prepare 2 connection strings:

  • queue_connection_string - For connecting to your Service Bus Namespace.
  • wa_blob_connection_string - For connecting to your Blob Storage Account.

Refer to the Windows Azure tutorials above to find out how to get these connection string/credentials

####Adding to Windows Azure Website

  1. Login to the Windows Azure Management Portal
  2. Click on the Website that you created.
  3. Click on "Configure"
  4. Scroll down to "app settings" and add your connection strings.

Adding to your bash environment (Linux/OSX)

  1. In console, type:

    $ export queue_connection_string='Endpoint=<EndpointURL>;SharedSecretIssuer=owner;SharedSecretValue=<SharedSecret>'
    $ export wa_blob_connection_string='DefaultEndpointsProtocol=https;AccountName=<YourNameSpace>;AccountKey=<AccountKey>'
  2. To check that your have the info in your environemnt:

    $ export $queue_connection_string

    It should print out your connection string.

  3. To make sure these environment variables are added whenever you boot up or SSH into your machine, edit the .bash_profile file in user's home directory.

    $ vim ~/.bash_profile

    Add:

    export queue_connection_string='Endpoint=<EndpointURL>;SharedSecretIssuer=owner;SharedSecretValue=<SharedSecret>'
    export wa_blob_connection_string='DefaultEndpointsProtocol=https;AccountName=<YourNameSpace>;AccountKey=<AccountKey>'

OR use the env.php file

  1. cd to bin folder.

  2. Copy env.sample.php to env.php.

  3. Edit that file and fill in your connection string info for your Windows Azure Service Bus Namespace and Windows Azure Blob Storage account.

    <?php
    # Rename this file to env.php and place in the same folder as cli.php and daemon.php
    putenv("queue_connection_string='Endpoint=https://yournamespace.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=XXXXX'");
    putenv("wa_blob_connection_string='DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=XXXXXX'");

Running the Sample Codes

  1. First, publish the sample code to your Windows Azure Website

    $ git add remote azure https://<username>@<WindowsAzureGitRepo>
    $ git push azure master

Simple Queue

  1. REST interface

    $ curl -XPOST http://<yourwebsitename>.azurewebsites.net/Noob/ -d "var1=foo&var2=bar"
    $ curl -XPOST http://<yourwebsitename>.azurewebsites.net/Noob/?REQUEST_METHOD=PUT -d "t=meh"
  2. CLI interface

    $ php cli.php Noob add --data '{"name":"Dino Bing"}'
    $ php cli.php Noob work

Gallery

  1. Visit the gallery website: http://<yourwebsitename>.azurewebsites.net/gallery/

  2. Click on "Upload" to upload a photo.

  3. Run the Job.

    $ php cli.php Photos work
  4. Go back to the Gallery page and refresh.

  5. Your new photo should appear.

Running the Photo Worker Daemon

  1. Start a Windows Azure Virtual Machine (use the CentOS 6.2 linux distro).

  2. SSH into your VM.

  3. Create a new file install.sh and use these contents.

    #!/bin/sh
    sudo yum install -y screen git php php-gd php-pear php-process
    curl -s https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    git clone git://github.com/miccheng/PHP-Queue-Windows-Azure-Queue-Demo.git
    cd PHP-Queue-Windows-Azure-Queue-Demo
    composer install
  4. Run the script to install the required software to run the daemon. The script would also checkout a copy of the sample code into your current folder.

    $ sh install.sh
  5. cd to the bin directory.

  6. Start the daemon:

    $ php daemon.php start
  7. To stop the daemon:

    $ php daemon.php stop

About

Demo app for Windows Azure Service Bus Queue backend.


Languages

Language:PHP 98.5%Language:Shell 1.5%