/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php


class SCHEDULER {
 const FLAGS_DEFAULT = 0;
 const AUTO_RESTART = 1;                // Re-start from the beginning if processing cycle is finished
 
 const ACTIVITY_CACHE = "cache";
 
 const MODE_CURRENT = "current";
 const MODE_ARCHIVE = "archive";
 const MODE_UNIVERSAL = false;

 const CACHE_CURRENT = SCHEDULER::MODE_CURRENT;       // Consider only currently active databases...
 const CACHE_ARCHIVE = SCHEDULER::MODE_ARCHIVE;       // Process also archived caches...
 const CACHE_UNIVERSAL = SCHEDULER::MODE_UNIVERSAL;   // Universal caching mode, we decide the best order for a single group of cachers...

 var $lock;
 var $sched_flags;
 var $sched_path;
 var $sched_activity;
 var $sched_mode;

 function __construct(REQUEST $req = NULL, $activity = SCHEDULER::ACTIVITY_CACHE, $mode = SCHEDULER::MODE_CURRENT, $flags = SCHEDULER::FLAGS_DEFAULT) {
    global $ADEI_SETUP;
    global $TMP_PATH;
    global $ADEI_CONTINUOUS_CACHING;

    if ($req) $this->req = $req;
    else $this->req = new REQUEST();
    
    if (!$mode)
        throw new ADEIException(translate("Universal scheduling mode is not supported yet"));

    $name = $ADEI_SETUP . "_" . $activity . "_" . $mode;
    if (!preg_match('/^[\-_a-zA-Z0-9]+$/', $name))
        throw new ADEIException(translate("Invalid symbols (%s) in scheduling activity and mode", $name));

    if ($ADEI_CONTINUOUS_CACHING) {
        if (($activity == SCHEDULER::ACTIVITY_CACHE)&&($mode == SCHEDULER::MODE_CURRENT))
            $flags |= SCHEDULER::AUTO_RESTART;
    }

    $dir = $TMP_PATH . "/scheduler";
    if (!is_dir($dir)) {
    	if (!@mkdir($dir, 0777 /*0755*/, true))
	    throw new ADEIException(translate("It is not possible to create scheduler directory \"$dir\""));

# When creating from apache, the 0777 mode is ignored for unknown reason	
	@chmod($dir, 0777);
    }

    
    $this->lock = new LOCK($name);
    $this->sched_flags = $flags;
    $this->sched_path = "$dir/$name";
    $this->sched_activity = $activity;
    $this->sched_mode = $mode;
 }
 
 private function GetList(REQUEST $user_req, $split, $flags = REQUEST::PRINT_ERRORS) {
     switch($split) {
	 case "servers":
	    $list = $user_req->GetServers($flags);
	    break;
	 case "sources":
	    $list = $user_req->GetSources($flags);
	    break;
	 case "groups":
	    $list = $user_req->GetGroups($flags);
	    break;
	 default:
	    throw new ADEIException(translate("Unsupported type of parallelization (%s)", $split));
    }

    $req_list = array();
    foreach ($list as $key => $sreq) {
#        if (isset($user_req->props['db_server'])&&($user_req->props['db_server'] != $sreq->props['db_server'])) continue;
#        if (isset($user_req->props['db_name'])&&($user_req->props['db_name'] != $sreq->props['db_name'])) continue;
        $req_list[$key] = $sreq->props;
    }

    return $req_list;
 }

 private function Schedule(array $req_list) {
    $fname = "{$this->sched_path}.cache";
    $rname = "{$this->sched_path}.reset";
    
    if (!$this->lock->Lock(LOCK::BLOCK|LOCK::EXCLUSIVE))
        throw new ADEIException(translate("Scheduler is not able to acquire a lock for \"%s\"", $fname));

    if (count($req_list) > 0) {
        if ((($this->sched_flags&SCHEDULER::AUTO_RESTART) == 0)&&(file_exists($fname))) 
            @file_put_contents($rname, "");
        $bytes = file_put_contents($fname, serialize($req_list));
        $this->lock->Unlock();
    } else {
        if (file_exists($fname))
            @unlink($fname);
        if (file_exists($rname))
            @unlink($rname);
    }

    if ($bytes === false)
        throw new ADEIException(translate("Scheduler is not able to open \"%s\"", $fname));
 }

 function ScheduleRequestList(REQUESTList $list, $split = "sources") {
    $user_req = $this->req;

    $req_list = array();
    foreach ($list as $sreq) {
        if (isset($user_req->props['db_server'])&&($user_req->props['db_server'] != $sreq->props['db_server'])) continue;
        if (isset($user_req->props['db_name'])&&($user_req->props['db_name'] != $sreq->props['db_name'])) continue;
        array_push($req_list, $sreq->props);
    }

    $this->Schedule($list);
 }

 function ScheduleRequest(REQUEST $req = NULL, $split = "sources") {
    if (!$req) $this->req = $req;
 
    $list = $this->GetList($req, $split);
    if ($this->sched_mode == SCHEDULER::MODE_ARCHIVE) {
        $wlist = $this->GetList($req, $split, REQUEST::LIST_WILDCARDED);
        $list = array_diff_key($wlist, $list);
    }
 
    $this->Schedule($list);
 }


 function Get() {
    $iname = "{$this->sched_path}.index";
    $fname = "{$this->sched_path}.cache";
    $rname = "{$this->sched_path}.reset";
    
    if (!$this->lock->Lock(LOCK::BLOCK|LOCK::EXCLUSIVE))
        throw new ADEIException(translate("Scheduler is not able to acquire a lock for \"%s\"", $fname));

    if (!file_exists($fname)) {
        $this->lock->Unlock();
        return array();
    }
    
    $data = @file_get_contents($fname);
    if ($data === false) {
        $this->lock->Unlock();
        throw new ADEIException(translate("Scheduler is not able to read \"%s\"", $fname));
    }
    
    $props_list = unserialize($data);
    if (($props_list === false)||(!is_array($props_list))) {
        $this->lock->Unlock();
        throw new ADEIException(translate("Scheduler is not able to parse \"%s\"", $fname));
    }

    if (count($props_list) == 0) {
        return array();
    }

    $index = @file_get_contents($iname);
    if ($index === false) $index = 0;
    else if ($index >= count($props_list)) {
            // We restarting standard, but pausing archive caching
        if (($this->sched_flags&SCHEDULER::AUTO_RESTART) == 0) {
            unlink($fname);
            if (file_exists($rname)) unlink($rname);
            else {
                $bytes = file_put_contents($iname, 0);
                $this->lock->Unlock();
                
                if ($bytes === false)
                    throw new ADEIException(translate("Scheduler is not able to open \"%s\"", $iname));

                return array();
            }
        }
        $index = 0;
    }

    $bytes = file_put_contents($iname, ($index + 1));
    $this->lock->Unlock();
    
    if ($bytes === false)
        throw new ADEIException(translate("Scheduler is not able to open \"%s\"", $iname));

    $keys = array_keys($props_list);
    $res = array_merge($props_list[$keys[$index]], array(
        "sched_activity" => $this->sched_activity,
        "sched_mode" => $this->sched_mode                       // In Universal mode, we should return the real mode of the returned item
    ));

    return $res;
 }

 function GetProps() {
    return $this->Get();
 }

}
?>