-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanUpLockFile.php
More file actions
138 lines (123 loc) · 4.31 KB
/
CleanUpLockFile.php
File metadata and controls
138 lines (123 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* ファイルクリーンアップ ライブラリ
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Cache', 'Cache');
App::uses('NetCommonsTime', 'NetCommons.Utility');
App::uses('CleanUpLog', 'CleanUp.Lib');
/* @see https://github.com/NetCommons3/NetCommons3/blob/6451c4b5ee2a17c74ea65eb7e4d757d148cd1835/app/Config/core.php#L368 */
//
// 基本的にcore.phpで定義されたキャッシュ方式を踏襲する
// キャッシュファイルのprefixの一部(cake_core_)をCleanUpで使用している特別Prefixに置き換えて使用する
//
// 全てを置き換えないようにしているのは、
// 複数サーバーが同一箇所のキャッシュ場所を使用した場合も
// コンタミしないように工夫されたprefixを消さないようにしているため
// デフォルトでは頭に"myapp_"がついています
// ※NC3ではキャッシュファイル名を「固定」にしてはいけないということ
// 必ずcore.phpで用いられるprefixを意識しないといけない
//
$cacheSetting = Cache::settings('_cake_core_');
$cleanUpLockFileSetting = array_merge($cacheSetting, [
'prefix' => preg_replace('/cake_core_/', 'netcommons_clean_up_lock_', $cacheSetting['prefix']),
'duration' => '+7 days'
]);
Cache::config(CleanUpLockFile::$cacheConfigName, $cleanUpLockFileSetting);
/**
* ファイルクリーンアップ ライブラリ
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\CleanUp\Lib
*/
class CleanUpLockFile {
/**
* ロックファイルの設定名
*
* @var string
*/
public static $cacheConfigName = 'netcommons_clean_up_lock';
/**
* ロックファイルのキャッシュキー
*
* @var string
*/
public static $cacheKey = 'CleanUp.lock';
/**
* ロックファイルの作成と時刻の書き込み。バッチ実行開始時
*
* @return void
*/
public static function makeLockFile() {
//touch(self::$lockFilePath);
//時刻をロックファイルに書き込む
$now = NetCommonsTime::getNowDatetime();
//file_put_contents(self::$lockFilePath, $now);
//Cache::write('CleanUp.lock', $now, 'netcommons_clean_up_lock');
Cache::write(self::$cacheKey, $now, self::$cacheConfigName);
CakeLog::info(__d('clean_up', 'Created a lock file.'), ['CleanUp']);
}
/**
* ロックファイルの削除。バッチ終了時
*
* @return bool true:削除|false:ファイルなし
*/
public static function deleteLockFile() {
//if (file_exists(self::$lockFilePath)) {
if (Cache::read(self::$cacheKey, self::$cacheConfigName)) {
//unlink(self::$lockFilePath);
Cache::delete(self::$cacheKey, self::$cacheConfigName);
CakeLog::info(__d('clean_up', 'Lock file was deleted.'), ['CleanUp']);
return true;
}
CakeLog::info(__d('clean_up', 'No lock file.'), ['CleanUp']);
return false;
}
/**
* ロックファイルの削除とログ出力設定。ロックファイル強制削除用
*
* @return bool true:削除|false:ファイルなし
*/
public static function deleteLockFileAndSetupLog() {
CleanUpLog::setupLog();
// ログ開始時のタイムゾーン変更
$timezone = CleanUpLog::startLogTimezone();
CakeLog::info(__d('clean_up',
'Start forcibly delete lock file processing.'), ['CleanUp']);
$isDelete = self::deleteLockFile();
// ログ終了時にタイムゾーン戻す
CleanUpLog::endLogTimezone($timezone);
return $isDelete;
}
/**
* ロックファイルの存在確認
*
* @return bool true:ロックあり|false:ロックなし
*/
public static function isLockFile() {
//if (file_exists(self::$lockFilePath)) {
if (Cache::read(self::$cacheKey, self::$cacheConfigName)) {
return true;
}
return false;
}
/**
* ロックファイルの読み込み
*
* @return string ファイルクリーンアップ開始時刻
*/
public static function readLockFile() {
//if (file_exists(self::$lockFilePath)) {
$cleanUpStart = Cache::read(self::$cacheKey, self::$cacheConfigName);
if ($cleanUpStart) {
//$cleanUpStart = file_get_contents(self::$lockFilePath);
$cleanUpStart = date('m/d G:i', strtotime($cleanUpStart));
return $cleanUpStart;
}
return '';
}
}