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

Last change on this file since 620 was 620, checked in by dungnv, 11 years ago
File size: 2.3 KB
Line 
1<?php
2
3if ( ! function_exists('createLockFile'))
4{
5        function createLockFile ($lockFilePrefix, $maxReach, $timeToUnlock, $lockFilePath = '', $checkLockOnly = 0, $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 == 0) {
27                        $lockCount ++;
28                        $isLocked = $lockCount < $maxReach ? 0:1;
29                        $ready = 1;
30                }
31                else if ($timeDiffMin > $timeToUnlock) {
32                        $lockCount = 0;
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 == 0) {
41                        $fh = fopen ($filePath,'w');
42                        fwrite ($fh, $content);
43                        fclose ($fh);
44                }
45       
46                return $isLocked;
47        }
48}
49
50if ( ! function_exists('releaseLockFile'))
51{
52        function releaseLockFile ($lockFilePrefix, $lockFilePath = '')
53        {
54                $filePath = $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                $interval = date_diff($datetime1, $datetime2);
84
85                return $interval->format($differenceFormat);
86
87        }
88}
Note: See TracBrowser for help on using the repository browser.