source: pro-violet-viettel/sourcecode/application/helpers/lockfile_helper.php @ 622

Last change on this file since 622 was 622, checked in by dungnv, 10 years ago
File size: 3.1 KB
Line 
1<?php
2
3if ( ! function_exists('createLockFile'))
4{
5        function createLockFile ($lockFilePrefix, $maxReach, $timeToUnlock, $lockFilePath = '', $checkLockOnly = FALSE, $colSep = '|')
6        {
7                $currentTimeStamp = time();
8                $filePath = $lockFilePath.$lockFilePrefix.'_lock';
9                $fileExisted = file_exists($filePath);
10                $content = '';
11                $lastTimeLock = '';
12                $lockCount = 0;
13                $isLocked = 0;
14                $ready = 0;
15       
16                if ($fileExisted) {
17                        $content = file_get_contents($filePath);
18                }
19       
20                if ($content != '') {
21                        list($lockCount, $lastTimeLock, $isLocked) = explode ($colSep, $content);
22                }
23       
24                $timeDiffMin = $lastTimeLock != '' ? dateDifference(date('Y-m-d H:i:s',$currentTimeStamp), date('Y-m-d H:i:s',$lastTimeLock), '%i') : 0;
25
26                if ($lockCount < $maxReach && $timeDiffMin < $timeToUnlock && !$checkLockOnly) {
27                        $lockCount ++;
28                        $isLocked = $lockCount < $maxReach ? 0:1;
29                        $ready = 1;
30                }
31                else if ($timeDiffMin > $timeToUnlock) {
32                        $lockCount = 1;
33                        $isLocked = 0;
34                        $ready = 1;
35                }
36       
37                $lockTime = ($isLocked == 1) ? $lastTimeLock : $currentTimeStamp;
38                $content = $lockCount . $colSep . $lockTime . $colSep . $isLocked;
39               
40                if($ready == 1 && !$checkLockOnly) {
41                        $fh = fopen ($filePath,'w');
42                        fwrite ($fh, $content);
43                        fclose ($fh);
44                }
45       
46                return (int)$isLocked;
47        }
48}
49
50if ( ! function_exists('releaseLockFile'))
51{
52        function releaseLockFile ($lockFilePrefix, $lockFilePath = '')
53        {
54                $filePath = $lockFilePath.$lockFilePrefix.'_lock';
55                $fileExisted = file_exists($filePath);
56                if ($fileExisted) {
57                        unlink($filePath);
58                }
59        }
60}
61
62/**
63 * dateDifference
64 * Return difference between two dates
65 *
66 * @access      public
67 * @param       date    date_1
68 * @param       date    date_2
69 * @param       string  differenceFormat ()
70 * @return      string
71 *
72 * @example '%y Year %m Month %d Day %h Hours %i Minute %s Seconds', %y Year %m Month %d Day','%m Month %d Day'
73 * @example '%d Day %h Hours', '%d Day', '%h Hours %i Minute %s Seconds', '%i Minute %s Seconds','%h Hours', '%a Days'
74 */
75
76if ( ! function_exists('dateDifference'))
77{
78        function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
79        {
80                $datetime1 = date_create($date_1);
81                $datetime2 = date_create($date_2);
82
83                $diff = date_diff($datetime1, $datetime2);
84                $total = 0;
85                switch( $differenceFormat){
86                        case "%y":
87                                $total = $diff->y + $diff->m / 12 + $diff->d / 365.25; break;
88                        case "%m":
89                                $total= $diff->y * 12 + $diff->m + $diff->d/30 + $diff->h / 24;
90                                break;
91                        case "%a":
92                                $total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60;
93                                break;
94                        case "%h":
95                                $total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i/60;
96                                break;
97                        case "%i":
98                                $total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s/60;
99                                break;
100                        case "%s":
101                                $total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i)*60 + $diff->s;
102                                break;
103                }
104               
105                return (int)$total;
106                /* if( $diff->invert)
107                        return -1 * $total;
108                else    return $total; */
109        }
110}
Note: See TracBrowser for help on using the repository browser.