Categories
Work

Avro Phonetic Web User Control for ASP.NET

The idea is pretty simple: A web user control for ASP.NET that loads the Avro Phonetic keyboard and binds the text inputs and text areas.

The control can be downloaded from: http://masnun.googlecode.com/files/Avro.zip

How to use it?

1) Extract Avro.zip and you shall get two files in the Avro folder: “Avro.ascx” and “Avro.ascx.cs”. First one is the user control and second one is the codebehind file.

2) Crate a new website project in Visual Studio or open your existing project.

3) Copy the above mentioned two files (NOT the entire directory, just the files) into your project. For better organization you might want to create a “Controls” directory (if you haven’t already). Paste the two files inside your desired directory.

4) Now, we add the controls to a web form. Open a page, say Default.aspx. Just below the Page directives, add the following Register directives to register the control:

Here, Src is the location to the Avro.ascx file. TagPrefix and TagName makeup the custom tag that loads the control. In this case, you now have a <asp:Avro> tag which you can use to load the control.

5) Let’s add the control:

The control accepts two attributes – “Bangla” and “Callback”. Setting Bangla to “true” or “false” enables or disables Bangla by default. The Callback attribute accepts the name of a Javascript function which is called when the keyboard state changes.

6) Let’s test the control by adding a text area and the defined callback function:

Load the web page in your browser. If you have Firebug installed in Firefox, the callback should print out state of the Bangla layout on the console.

For brevity, here is my entire Default.aspx file:

Categories
Javascript PHP Work

Avro Phonetic Plugin for WordPress

Rifat vai has released an awesome jQuery plugin which adds Avro Phonetic layout to your text inputs. I converted it into a WordPress Plugin. Hats off to Rifat vai for the awesome job!

How to use it?

Click on the download link below. It should show you the raw php source of the plugin file. Save it as “avro-phonetic.php” and upload the file to “wp-content/plugins/” directory. Now go to your “Plugins” page from WP Dashboard. Activate the plugin.

Download: https://raw.github.com/masnun/Avro-Phonetic-WP-Plugin/master/avro-phonetic.php

Github Repo: https://github.com/masnun/Avro-Phonetic-WP-Plugin

Official Page: http://torifat.github.com/jsAvroPhonetic/

Knows Issues:

  • Doesn’t work with the Visual Editor (TinyMCE) of WordPress. Please use the HTML editor.
  • Doesn’t show any visual clue of which language (English or Bangla) is active.

    It now shows a black box with language identification

    Thanks to the Avro team for the nice icons.

  • Ctrl + M toggles between Bangla and English. It’s your responsibility to let the users know how to use it. The plugin doesn’t have any fancy instructions displayed to the users.

    It now has a widget. Add the widget to your sidebar.

  • Forces to load jQuery 1.7.2 without caring if an older version is already loaded. Had to do this because wp_enque_script() was not loading jQuery on some themes/setup.

    Thanks to Mehdi vai, the loading is now done using JS after checking if a version is loaded already! 🙂

Contribute:
Feel free to fork the codes on Github and send me pull requests. If you’re not used to Git, send me the modifications over email. I shall merge them and commit. My email address is available on masnun.com 🙂

Have fun!

Categories
PHP

Gearman, PHP and Supervisor: Processing background jobs with sanity!

On a Leevio project, I’ve been entrusted with the responsibility to build an engine that processes huge data with heavy network IO. The engine needs to integrate seamlessly with the web front end. Since we had many of the business logic already done in PHP, I preferred to go with PHP. It’s not like that we didn’t have an option. I could go for Python, but then I would have had to port my existing ZF models to Python or build some APIs to access data through another layer of abstraction. I wanted to keep it simple and short. And I had a feeling, if the pros (Yahoo!, Digg, MailChimp, GrooveShark, Xing and who not?) can do it with PHP, why can’t we?

I built the core engine on Gearman. It was simple, stupid and very easy to get things done. My workers were running fine in the test phase. But it didn’t turn out that easy when the workers grew complex. PHP was not built for long running requests. It’s internal caches and circular object references were draining memory like hell. With the different performance optimizations I have made, it has improved over the period of time but still it can sometimes leak memory like anything. Besides, the awesome garbage management was introduced only in PHP 5.3 and we didn’t dare upgrade (because some of the legacy technologies used in the project). The engine was working just fine but I was having a hard time keeping it alive. Sometimes the processes exhausted into Zombie Process.

I’ve tried register_shutdown_function to quit and and restart at the completion of every job processing. It worked well but was still not enough – the zombie processes were not being restarted. 🙁 I didn’t want to impose memory limit either – that might have terminated the process in the middle of a job (and the hell will break lose!) 🙁

I then started googling for any mechanism to manage PHP workers with Gearman. I found “Supervisor”. Grooveshark and many others use it with PHP and Gearman. It’s a very simple tool to handle your workers (not just Gearman workers, literally any command). It actually starts the processes as it’s subprocess and can identify the death of processes running under it. The most important part is – it can auto restart the processes based on custom configurations (depending on exit codes). The configuration is simple. You define a command, configure it and then run supervisor. With supervisor, I’m running the PHP processes in a similar fashion to the web page handling. Every time the work is complete, the process quits and supervisor restarts it. The process goes in an infinite loop, waits for new workload. When the workload arrives, it works and then again quits – this repeats.

The engine is now more stable and working fine. So, if you have to manage long running workers with PHP and Gearman, do consider Supervisor to manage your workers. It shall pay off!

Here’s a quick How To on getting started with Supervisor:

Installation
Supervisor is a Python daemon and available from pypi. So you can “easy_install” it. First, we install Python setuptools (to enable easy_install):

Now we install supervisor:

The I created “/etc/supervisord.conf” with the sample config by typing:

Then I removed the unnecessary parts and kept what I need. Check out the Configuration Manual for better understanding and full coverage on the available options.

After saving the configuration, run Supervisor in debug mode to check if the configuration is okay. Debug mode doesn’t send supervisord background and prints errors on the console. Use it like this:

When debugging is done, simply run:

It’ll run supervisor, spawn the sub processes, detach from terminal and get into daemon mode.

What’s Next?

Well, this does my job perfectly. But if you want to do some more tricks or advanced stuff, you should look at the Process Controll Extensions of PHP to take the advantages of native Unix environment!

Happy supervising!