Thursday, 10 March 2016

Apache Error notifier using Gulp

I wanted an easy way to retrieve critical errors from my Apache error log. While i was finding a solution, Gulp came into the picture. First let me introduce you to Gulp.

Gulp is a build system which you can use to automate your tasks. Its same as Grunt if you have heard of it.

I have used Gulp to automate following tasks in past:
  1. Compiling SCSS 
  2. Compressing CSS
  3. Auto reloading my browser if CSS changes.
  4. Compiling Coffeescripts
Now it was time to use gulp for Apache notification. So I created Gulp task for the same. Here is the script.



First of all I have included 3 Gulp dependencies which is Gulp itself as i don't have gulp in my environment, Gulp notifier to display error notification and Gulp Intercept to Go through log file and retrieve the error.

I have defined Watch as default task. Default task is a task which Gulp keep running continuously. The task keep watching over the log file. Whenever the error log file changes, Task will go through the file and will retrieve recent errors and will display them.

The script will display mainly 2 types of critical errors:

  1. PHP Fatal Error
  2. PHP Parse Error

Setup:


Requirement:
  1. Node
Installation Steps:
  1. First setup Node.
  2. Install Gulp
  3. Then clone this repository. Or Download repository from Github.
  4. Run npm install
Start:
  1. Goto the cloned repository.
  2. First add path to your Apache's Php Error log file in variable "logPath". (Default is /var/log/apache2/error.log)
  3. Run sudo gulp

Wednesday, 16 September 2015

How to Fix "PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file" Error?

If you have ever needed to ZIP files from your application, I guess, you must have heard of or used PCLZIP library. It is very simple, easy to use and feature-rich library.

If you have ever used this library on Linux, you might have come across this error stating PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file.

This error occurs because PCLZIP is using your current working directory to create the temporary file. But on Linux, Apache user may not have permission to write files in the current working directory so it will throw this error.

You can either give writing permission to your working directory, which I won't recommend, or you can set a temporary directory for PCLZIP.

To set temporary directory for PCLZIP, add following line to your code before you include PCLZIP Library:

define('PCLZIP_TEMPORARY_DIR', '/tmp/');

Or you can also use the path of a directory which is writable by your web server.

Tuesday, 8 April 2014

pre-commit Hook to Prevent Debug Value / Code From Being Committed

It is not uncommon that sometimes you end up committing a wrong code, probably with debug values and codes that you have put for temporary basis to test out something. I have several times committed a code that had some hardcoded values or debug messages which I put to test my code in different scenarios. It is quite hard to spot this mistake if your "wrong code" is the part of a rare scenario. And when this code goes into the production and the rare time comes, you will see either scared users or screaming users.

To avoid such mistakes, I wrote a small bash script few weeks back to be executed in pre-commit hook of git. This script looks for a particular text ( I call it #DUMMYTEXT) in your code in your staging area and stops you from committing the code if found. Unfortunately, you have to make habit of putting your #DUMMYTEXT every time (or at least once in the file) you are putting dummy values or code.

What is a Git Hook?

Hooks are nothing but a some scripts that are automatically executed for certain events.  To read more about hooks, go to http://git-scm.com/book/en/Customizing-Git-Git-Hooks

In our case, we will be using pre-commit, a client side hook, which is executed just before the code is committed. You can do whatever you want to do and decide if you want to proceed with commit or abort the commit. 

How do I create a hook?

In Git, hooks are per-repo basis. That means, you create separate hooks for each repository. You will find .git ( hidden) directory in root your repository. Inside that directory, you will see a .hooks directory. Usually, it is filled with sample hooks. In our case, we will need to put our script in pre-commit file. You can either create new one or rename pre-commit.sample to pre-commit and put the validation code/script there.

Where is the code?

#PUT YOUR OWN WORD TO LOOK FOR. BUT KEEP IT UNIQUE
debugText='#DUMMYTEXT'

filelist=(`git diff --cached --name-only --diff-filter=ACMR`)

found=0

for FILE in ${filelist[*]}
do
 if git grep --cached -q "$debugText" "$FILE"; then
  
  if [ $found -eq 0 ]; then
   echo "[ERROR] Following file(s) contains $debugText. Please remove debug text and values(if any):"
  fi
  found=1
  echo "$FILE"
  
 fi
  
done

exit $found

How do I work with this?

I recommend making a habit to put debug text every time you are putting dummy texts or values. So that you do not miss any dummy values. 

So if any time, you mistakenly stage a file with dummy text/values and try to commit , this script will stop you from committing the code.

Notes

If you forget to put your debug text, there is no way anyone case save you. ;-)  Feedback/Suggestion is highly appreciated! :-)

Sunday, 15 September 2013

Tuesday, 12 March 2013

Batch file to execute MySQL scripts


@echo off
ECHO %USERNAME% started the File Execution Process at %TIME%  >status.txt

for %%f in (*.sql) do (
C:\wamp\bin\mysql\mysql5.5.20\bin\mysql -u root --delimiter=// < %%f >>status.txt
pause; 

)



Can someone extend this to let the files be executed in accordance of their modified time stamps

Monday, 11 March 2013

Search and Replace Tools

For multi line search and replace within a set of files,

I use on windows:
Text Crawler
ToolBuckets Multi Line Find and Replace Feature in Notepad++

On Ubuntu I simply use sed alongwith grep

What do you use?
Any cool tricks to simplify multi line search and replace