-
Notifications
You must be signed in to change notification settings - Fork 4
/
StorageFiles.php
144 lines (132 loc) · 2.86 KB
/
StorageFiles.php
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
139
140
141
142
143
144
<?php
namespace Jamm\Tasks;
class StorageFiles implements IStorage
{
private $semaphore_file;
private $tasks_dir;
private $semaphore_life_time = 180;
/**
* @param string $tasks_dir - temporary directory, when files of tasks will be stored
*/
public function __construct($tasks_dir)
{
$this->tasks_dir = realpath($tasks_dir);
$this->semaphore_file = $this->tasks_dir.'/.semaphore';
}
/**
* @return ITask
*/
public function get_next_task()
{
$dir = $this->get_tasks_list();
if (empty($dir)) {
return false;
}
sort($dir);
foreach ($dir as $filepath) {
$task = $this->read_task($filepath);
if (is_object($task)) {
if (!unlink($filepath)) {
if (file_exists($filepath)) {
trigger_error('Can not delete task!', E_USER_WARNING);
return false;
}
}
return $task;
}
}
return false;
}
public function semaphore_create()
{
if (file_exists($this->semaphore_file)) {
return false;
}
return file_put_contents($this->semaphore_file, 1, LOCK_EX);
}
public function semaphore_delete()
{
if (file_exists($this->semaphore_file)) {
return unlink($this->semaphore_file);
}
return true;
}
public function semaphore_exists()
{
if (file_exists($this->semaphore_file)) {
if (filemtime($this->semaphore_file) < (time() - $this->semaphore_life_time)) {
unlink($this->semaphore_file);
return false;
}
else {
return true;
}
}
else {
return false;
}
}
/**
* @param string $id
* @return bool|ITask
*/
public function read_task($id)
{
$content = file_get_contents($id);
if (empty($content)) {
return false;
}
return unserialize($content);
}
public function store($task_object, $unique = false, $priority = 1)
{
$content = serialize($task_object);
$priority = '['.intval($priority).']';
if ($unique) {
$filename = '1'.md5($content);
if (file_exists($filename)) {
return true;
}
}
else {
$filename = $priority.$this->get_new_filename($this->tasks_dir.'/'.$priority);
}
return file_put_contents($this->tasks_dir.'/'.$filename, $content, LOCK_EX);
}
private function get_new_filename($dir)
{
$name = $t = round(microtime(true)*100);
$i = 0;
while (file_exists($dir.$name.'.task')) {
$i++;
$name = $t.'_'.$i;
}
return $name.'.task';
}
public function get_tasks_list()
{
$dir = scandir($this->tasks_dir);
if (empty($dir)) {
return false;
}
$tasks = array();
foreach ($dir as $file) {
$filepath = $this->tasks_dir.'/'.$file;
if ($file == '.' || $file == '..' || $filepath == $this->semaphore_file) {
continue;
}
if (is_file($filepath)) {
$tasks[] = $filepath;
}
}
return $tasks;
}
public function getSemaphoreLifeTime()
{
return $this->semaphore_life_time;
}
public function setSemaphoreLifeTime($semaphore_life_time)
{
$this->semaphore_life_time = $semaphore_life_time;
}
}