Wednesday, May 31, 2017

Amazon EC2 Create Swap Space


Recently in one of my project we have laravel application deployed on amazon ec2 instance. When running composer update command, we were facing issue of swap space as it was not defined. So in this blog I will explain how you can create swap space in your amazon ec2 instance. Please note these steps are specifically for ubuntu instance. If you are running other version of liunx, please check commands first and then use it.

1) Create swap file

sudo fallocate -l 1G /swapfile

2) Verify it

ls -lh /swapfile

It should show following output.

-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

3) Enable it and make it accessible by only root user.

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile

That's it now you have swap space configured in your instance. 

Monday, May 29, 2017

Force Quit an App in Mac OSX from Terminal


Recently I faced an issue with OpenOffice app on MAC. For some reason following open office window is displayed every time I open a document.

And then no matter what I do it does not go away. I tried force quit. Tried to close it from Activity monitor but it does not work. So then only option I had is to kill it with terminal. You can use this procedure to kill any unresponsive app in your OSX. 

First of all we have to find out process id of the unresponsive app. Since the app is unresponsive, it must be consuming lots of CPU resources. To find out this, open terminal and type following command.

top -o cpu

It will show following window. 

As you can see in first column we have PID and in second column we have app name. Find out PID from the name of unresponsive application and then type following command in terminal.

kill PID

 And it will kill unresponsive app. Hope this helps you.

Friday, May 26, 2017

PHP strtotime() does not work with dd/mm/yyyy Format


This is quick blog regarding PHP strototime function. In our recent project we were importing data from CSV where we faced two issues.

1) If we try to import date like 01/05/2016 considering dd/mm/yyyy format, it saves date in database as 2015-01-05

2) If we try to import date like 31/08/2016 considering dd/mm/yyyy format, it saves data in database as 1970-01-01

In short month and day is swiped while saving. So in case of date like 31/08/2016,  31 is assumed as month and there is no such month in calendar so it gave error and returns date like 1970-01-01

This is first time we faced such issue. I was not sure what's the exact issue but I assumed it has something to do with separator. So we tried same dates with  - instead of / and it worked. So to solve this temporary, we had following solution.

$date = '31/08/2016';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

So we replace / with - and it worked but solving problem is not enough, we shall go in deep to check why we had this problem so curiously I looked into function documentation of strtotime and found out following.

"Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. "

So indeed the problem here was with the separator.

Hope this helps you.

Wednesday, May 24, 2017

How to exit the Vim editor?

Every time I use vi editor on terminal in OSX or Linux, I face issue on exiting Vim editor on terminal. So in this blog I am going to explain complete procedure on exiting vim editor on terminal in OSX or Linux.

What is Vim editor?

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems and with Apple OS X.

How to Use Vim on Terminal?

Go to terminal and type following command

vi "NameOfFile"

and it will open Vim editor like this.

This is blank file. To start editing file press "I" key and then you can start editing file.

Now the tricky part is exiting vim editor on terminal. Here are exact procedure.

Hit the Esc key, vim goes into command mode.

Type Shift key + : and you will see : at bottom of the file.

Now you have to type command to tell editor what you want to do with the file. Here are some options you have.

q to quit
q! to quit without saving
wq to write and quit
wq! to write and quit even if file has only read permission
x to write and quit
qa to quit all
Now press "Return key" and it will exit vim editor.

Saturday, May 20, 2017

React Native undefined is not an object (evaluating 'this.props.navigator.push)


Recently we have been working with React Native for one of our application where we faced an issue with Android Navigator. Basically we had two lists Category List and Sub Category List and on tap of category list item we wanted to render sub category list. Following is the code for that.

<TouchableOpacity onPress={()=> this.pressRow(rowData)} >
 <View style={thumbnailContainerStyle}>
 source={{ uri: rowData.image }}
 <View style={headerContentStyle}>
<Text style={headerTextStyle}>{}</Text>

And following is function where we were using navigator.push to go to next view.

pressRow (rowData) {

    if(rowData.subcategory == true)
        name: SubCategoryList,
        passProps: {}
    else if(rowData.subcategory == false)
        name: ProductList,
        passProps: {}


Here we are getting problem with error, this.props.navigator is undefined so were not able to go to second screen.

The problem here is on scope as we were having category list rendered in app container like this.

<CategoryList />

So props of parent are not passed to child and hence navigator was not available. To solve this all you have to do it add your child component like this.

<CategoryList  navigator={this.props.navigator} />

And now navigator object will be available. Hope this solves your problem.