Thursday, March 27, 2014

Run PHP As a Background Process

I would like to run a PHP script as a background process on my OSX machine. In the test script, I use file_put_contents method to write the log in a specific file.  When I directly execute the command line PHP script, I can see that the log is written into the file, which means, everything is fine. But, when I use it as a background process, it is not working. I have tried many ways that just failed to run script as a background process. At the end, I solve the problem. I want to record how I make it work at the end.

The commands which I used were failed:

 php /path/to/my/test/script/cli_test.php argument1 &
 php -f /path/to/my/test/script/cli_test.php argument1 & 
 php /path/to/my/test/script/cli_test.php argument1 > output.txt 2>&1 &
 nice -20 php  /path/to/my/test/script/cli_test.php argument1 > output.txt 2>&1 &
 nohup  /path/to/my/test/script/cli_test.php argument1 1>/dev/null 2>/dev/null &
 php /path/to/my/test/script/cli_test.php argument1 > /dev/null 2>&1 & echo $! 

The result I got was always showing a sudden stop like below -

 [1]+  Stopped     nohup php /path/to/my/test/script/cli_test.php argument1 > /dev/null 2> /dev/null

And, the process keeps hanging in the background, never die.

I found the solution in a discussion - http://ubuntuforums.org/showthread.php?t=977332 .
According to the discussion that the discussion says, I tried the following command and it worked very well.


php -q /path/to/my/test/script/cli_test.php argument1 < /dev/null
nohup php -q /path/to/my/test/script/cli_test.php argument1 </dev/null 2> /dev/null & echo $! 

I do not know where the bug sits, is it a PHP command line or OSX bug? Ubuntu users think it is a OS level bug, but it seems to me it is a PHP command line bug. Anyways, as long as it works and I do not have to use other solution like "pcntl_fork".


Friday, February 28, 2014

Using cache for database query

I recently think about how to use cache to improve database query performance. However, one may know one way is to cache the query result for those tables that have relative fewer update. I consider this and think of the whole algorithm as below pseudocode (I am using Java styles).

(1) Simple Query Cache



//Firstly, get the sql string
String sql = this.getSqlStatement(); Object value = this.getCache(sql);
if (value == null) {
    //execute the sql statement to retrieve data from database
    Object result = this.execute(sql);
    //set the result in cache, using sql as the key
    this.setCache(sql, result);
    return result;
} else {
    return value;
}


Above algorithm puts no consideration about querying stale data. Other operations, such as update, on the database may cause the query to have incorrect results. Therefore, I am thinking to keep a list of modified tables, using that for determining which table is updated after query result is cached. The following is my new algorithm.

(2) Sophisticated Method


For each update, delete, insert operation, 
mark the timestamp of execution and the table that is modified,
put the data in cache.
 
For each query
       Get the table modified timestamp T1 from cache
       Get the cache timestamp T2 for query
       if (T2 < T1) {
           execute the query
           set the query into the cache
           set the timestamp into the cache
           return the query result
       } else {
            get the cache
            return the cache
      }


Now, the key is that we need benchmark tests to ensure the performance increase. I will write that later.

Friday, February 21, 2014

CSS 3 Index


List all the CSS3 properties below. Total 308 properties.

align-content auto
align-self auto
all depending on individual properties
animation depending on individual properties
animation-fill-mode none
animation-iteration-count 1
animation-name none
animation-play-state running
animation-timing-function ease
appearance normal
azimuth ? center
backface-visibility visible
background depending on individual properties
background-attachment scroll
background-clip border-box
background-color transparent
background-image none
background-origin padding-box
background-position 0% 0%
background-repeat repeat
background-size auto
baseline-shift baseline
binding none
bleed 6pt
bookmark-label content()
bookmark-level none
bookmark-state open
bookmark-target none
border depending on individual properties
border-bottom depending on individual properties
border-bottom-color current color
border-bottom-left-radius 0
border-bottom-right-radius 0
border-bottom-style none
border-bottom-width medium
border-collapse separate
border-color depending on individual properties
border-image none
border-image-outset 0
border-image-repeat stretch
border-image-slice 100%
border-image-source none
border-image-width 1
border-left depending on individual properties
border-left-color current color
border-left-style none
border-left-width medium
border-radius 0
border-right depending on individual properties
border-right-color current color
border-right-style none
border-right-width medium
border-spacing ? 0
border-style depending on individual properties
border-top depending on individual properties
border-top-color current color
border-top-left-radius 0
border-top-right-radius 0
border-top-style none
border-top-width medium
border-width depending on individual properties
bottom auto
box-decoration-break slice
box-shadow none
box-sizing content-box
break-after auto
break-before auto
break-inside auto
caption-side top
chains none
clear none
clip auto
clip-path none
clip-rule nonzero
color implementation dependent
color-interpolation-filters auto
color-profile auto
column-count auto
column-fill balance
column-gap normal
column-rule depending on individual properties
column-rule-color current color
column-rule-style medium
column-rule-width medium
column-span none
column-width auto
columns depending on individual properties
contain none
content normal
counter-increment none
counter-reset none
crop auto
cue depending on individual properties
cue-after none
cue-before none
cursor auto
direction ltr
display inline
dominant-baseline auto
drop-initial-after-adjust text-after-edge
drop-initial-after-align baseline
drop-initial-before-adjust text-before-edge
drop-initial-before-align caps-height
drop-initial-size auto
drop-initial-value initial
elevation level
empty-cells show
filter none
flex depending on individual properties
flex-basis auto
flex-direction row
flex-flow depending on individual properties
flex-grow 0
flex-shrink 1
flex-wrap nowrap
float none
float-offset 0 0
flood-color black
flood-opacity 1
flow-from none
flow-into none
font depending on individual properties
font-family implementation dependent
font-feature-settings normal
font-kerning auto
font-language-override normal
font-size medium
font-size-adjust none
font-stretch normal
font-style normal
font-synthesis weight style
font-variant normal
font-variant-alternates normal
font-variant-caps normal
font-variant-east-asian normal
font-variant-ligatures normal
font-variant-numeric normal
font-variant-position normal
font-weight normal
grid depending on individual properties
grid-area depending on individual properties
grid-auto-columns auto
grid-auto-flow none
grid-auto-position 1 / 1
grid-auto-rows auto
grid-column depending on individual properties
grid-column-end auto
grid-column-start auto
grid-row depending on individual properties
grid-row-end auto
grid-row-start auto
grid-template depending on individual properties
grid-template-areas none
grid-template-columns none
grid-template-rows none
hanging-punctuation none
height auto
hyphens manual
icon auto
image-orientation auto
image-resolution normal
ime-mode auto
inline-box-align last
justify-content auto
justify-items auto
justify-self auto
left auto
letter-spacing normal
lighting-color white
line-break auto
line-height normal
line-stacking depending on individual properties
line-stacking-ruby exclude-ruby
line-stacking-shift consider-shifts
line-stacking-strategy inline-line-height
list-style depending on individual properties
list-style-image none
list-style-position outside
list-style-type disc
margin depending on individual properties
margin-bottom 0
margin-left 0
margin-right 0
margin-top 0
marker-offset auto
marks none
mask border-box
mask-box-image see individual properties
mask-box-image-outset 0
mask-box-image-repeat stretch
mask-box-image-slice 0 fill
mask-box-image-source none
mask-box-image-width auto
mask-clip border-box
mask-image none
mask-origin border-box
mask-position center
mask-repeat no-repeat
mask-size border-box
mask-source-type auto
mask-type luminance
max-height none
max-lines none
max-width none
min-height 0
min-width 0
move-to normal
nav-down auto
nav-index auto
nav-left auto
nav-right auto
nav-up auto
object-fit fill
object-position 50% 50%
opacity 1
order 0
orphans 2
outline depending on individual properties
outline-color invert
outline-offset 0
outline-style none
outline-width medium
overflow depending on individual properties
overflow-wrap normal
overflow-x visible
overflow-y visible
padding depending on individual properties
padding-bottom 0
padding-left 0
padding-right 0
padding-top 0
page auto
page-break-after auto
page-break-before auto
page-break-inside auto
page-policy start
pause implementation dependent
pause-after implementation dependent
pause-before implementation dependent
perspective none
perspective-origin 50% 50%
pitch medium
pitch-range 50
play-during auto
position static
presentation-level 0
punctuation-trim none
quotes text
region-fragment auto
rendering-intent auto
resize none
rest depending on individual properties
rest-after none
rest-before none
richness 50
right auto
rotation 0
rotation-point 50% 50%
ruby-align auto
ruby-overhang none
ruby-position before
ruby-span none
size auto
speak auto
speak-as normal
speak-header once
speak-numeral continuous
speak-punctuation none
speech-rate medium
stress 50
string-set none
tab-size 8
table-layout auto
target depending on individual properties
target-name current
target-new window
target-position above
text-align start
text-align-last auto
text-combine-horizontal none
text-decoration none
text-decoration-color currentColor
text-decoration-line none
text-decoration-skip objects
text-decoration-style solid
text-emphasis depending on individual properties
text-emphasis-color currentColor
text-emphasis-position over right
text-emphasis-style none
text-height auto
text-indent 0
text-justify auto
text-orientation mixed
text-outline none
text-overflow clip
text-shadow none
text-space-collapse collapse
text-transform none
text-underline-position auto
text-wrap normal
top auto
transform none
transform-origin 50% 50% 0
transform-style flat
transition depending on individual properties
transition-delay 0s
transition-duration 0s
transition-property all
transition-timing-function ease
unicode-bidi normal
vertical-align baseline
visibility visible
voice-balance center
voice-duration auto
voice-family implementation dependent
voice-pitch medium
voice-range medium
voice-rate normal
voice-stress normal
voice-volume medium
volume medium
white-space normal
widows 2
width auto
word-break normal
word-spacing normal
word-wrap normal
wrap-flow auto
wrap-through wrap
writing-mode horizontal-tb
z-index auto

Thursday, February 20, 2014

PHP MSSQL database driver installation

Install FreeTDS for Linux or MacOS driver. The driver from Microsoft is not working for linux based system.

(1) Download the latest FreeTDS from the official website : http://www.freetds.org

(2) Using compiler is better. Even though there are some binary package that you can download, it is sometimes not working. Compiling the driver often work perfectly.

(2.1) Compile FreeTDS (take version 0.82 as an example)
Extract the package:
tar zxvf freetds-0.82.tar.gz

Go to the directory:
cd freetds-0.82

./configure --prefix=/usr/local/freetds --with-tdsver=8.0
make
make install


- with-tdsver = 8.0 refers to the tds 8.0 version ( if this parameter is not used, according to the default compiler, it compiles to 5.0 and 5.0 connection to the database is 4000 , not SQLServer default port 1433 . )

(2.2) Compile PHP Module
To support for MSSQL, adding compiler parameters -- with-mssql = /usr/local/freetds.

Enter the php source directory, MSSQL module's source directory:
cd /home/wxw/gd/php-5.4.17/ext/mssql/

Generate compile profiles
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds/
make
make install


Add the mssql.so into php.ini, the path will be prompted after installation
vim /usr/local/php/lib/php.ini
extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/mssql.so"

Restart Apache server
/usr/local/apache2/bin/apachectl restart

During the compilation, if we encounter an error of not being able to find FreeTDS, like the following.
configure: error: Directory /usr/local/freetds is not a FreeTDS installation directory

You need to create two emtpy files.
touch /usr/local/freetds/include/tds.h
touch /usr/local/freetds/lib/libtds.a

To build and install the PDO_DBLIB extension, in Terminal, go to the new PDO_DBLIB extension directory (php-5.4.17/ext/pdo_dblib) and run the below commands to build the pdo_dblib extension.

phpize
./configure--with-php-config=/usr/bin/php-config--with-pdo-dblib=/usr/local/freetds/
make
sudo cpmodules/pdo_dblib.so/usr/lib/php/extensions/no-debug-non-zts-20100525/


We need to edit is php.ini so open php.ini in your text editor with root privileges.
Search for the line ; extension_dir = "./"
Replace the above line with extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20100525/"

Adding the extensions.
extension=pdo_dblib.so
Save the file and exit