Categories
Linux

Debugging MongoDB Connection Issues

I am working on a project where I connect to MongoDB from PHP using the PECL extension. Regardless of your stack, we might time to time face strange issues when our clients can’t connect to the mongodb server. In this post, I would document what I did in my case.

First test if the mongodb server can be connected to. Use the “mongo” shell client.

If the connection succeeds, then the server is definitely reachable. You have made some mistakes in configuring your client. A very common mistake is having a malformed connection URI. So double check if you typed in your host, port and other details properly in the connection string.

If the above fails, that means the server is not responding. To begin with, try restarting it.

If the server started well, try again. Still connection failed? OK, the server might be damaged. How about checking the log files?

Nothing interesting? Or do not understand the format? Well, then let’s try blindly repairing the server:

Your problem should be resolved by now. But if it doesn’t solve the issue, your problem is not a common one. Try Googling further or asking on StackOverflow or other programming forums.

Categories
PHP

Using PhpStorm from Command Line

I know the title is not so interesting because we all know PhpStorm can be launched from the command line just using the “pstorm” command. Like this:

But I love the word “storm” more than “pstorm”, so I did this in my .zshrc:

How do we open a directory in PhpStorm? Just like the usual:

Ah, that will open the current directory in PS 🙂

But what if the command line tool was not installed properly? What if, while upgrading, the tool broke? Or if like me, you install EAPs all the time to try out new features and the command line tool can’t find the EAP version’s path? Well, in this blog post, I will share the python script that is actually installed as the “pstorm” tool. I will also tell you what to modify in case it can’t find your PhpStorm. And of course, we will see how to install it.

So first, the python script. If it’s installed on your system, you can view the content:

Or to edit it with your preferred text editor (in my case vim):

If you don’t have the script installed, here’s mine:

If you know Python, feel free to read and understand what’s actually doing. Now, this script needs to know two things:

— The path to PhpStorm directory (where the executable is)
— The preferences directory

You should be able to find the first trying to locate where PhpStorm is installed. For the second, it’s actually platform dependent. On OS X, it’s inside the “Home” > “Library” > “Preferences” > “WebIde**” directory.

The preference directories are named with a zero appended to the major version of PhpStorm. So, for PS 7.x it’s WebIde70. For 6.x, it’s WebIde60.

Just configure the two variables in the script. Then copy it to your path, in my case, I have “~/.bin” added to my PATH. So I did this:

The last line makes it executable. Now open a new terminal and try the “pstorm” command.

Categories
Python

Embed a Python REPL in your program

So you love the Django shell? The interactive prompt that has your Django stuff loaded and ready for some quick prototyping? You always wanted to embed something similar in your Python apps too? Well, it’s painlessly simple with the “code” module. It offers us the “InteractiveConsole” class which we can extend to quickly get a REPL which is basically the Python REPL with our customizations. The code is heavily documented. Going through it should be adequate to understand whats going on:

What did we do? We subclassed InteractiveConsole to create our own implementation. And then ran it 🙂

Test run outputs: