-
Notifications
You must be signed in to change notification settings - Fork 108
/
cleanspool.h
351 lines (344 loc) · 11.7 KB
/
cleanspool.h
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#ifndef CLEANSPOOL_H
#define CLEANSPOOL_H
#include "voipmonitor.h"
#include "sql_db.h"
class CleanSpool {
public:
struct CleanSpoolDirs {
string main;
string rtp;
string graph;
string audio;
string audiograph;
};
struct CleanSpoolOptMax {
unsigned int maxpoolsize;
unsigned int maxpooldays;
unsigned int maxpoolsipsize;
unsigned int maxpoolsipdays;
unsigned int maxpoolrtpsize;
unsigned int maxpoolrtpdays;
unsigned int maxpoolgraphsize;
unsigned int maxpoolgraphdays;
unsigned int maxpoolaudiosize;
unsigned int maxpoolaudiodays;
unsigned int maxpoolaudiographsize;
unsigned int maxpoolaudiographdays;
};
struct CleanSpoolOptOther {
int maxpool_clean_obsolete;
int cleanspool_interval;
int cleanspool_sizeMB;
int autocleanspoolminpercent;
int autocleanmingb;
int cleanspool_enable_run_hour_from;
int cleanspool_enable_run_hour_to;
};
struct sSpoolDataDirIndex {
enum eTypeItem {
_ti_spool = 1,
_ti_sensor = 2,
_ti_date = 4,
_ti_hour = 8,
_ti_minute = 16,
_ti_type = 32
};
sSpoolDataDirIndex() {
hour = -1;
minute = -1;
_type = tsf_na;
}
unsigned getSettedItems() {
unsigned typeItem = 0;
if(spool.length()) typeItem |= _ti_spool;
if(sensor.length()) typeItem |= _ti_sensor;
if(date.length()) typeItem |= _ti_date;
if(hour >= 0) typeItem |= _ti_hour;
if(minute >= 0) typeItem |= _ti_minute;
if(type.length()) typeItem |= _ti_type;
return(typeItem);
}
bool eqSettedItems(const sSpoolDataDirIndex& other) {
if(spool.length() && spool != other.spool) return(false);
if(sensor.length() && sensor != other.sensor) return(false);
if(date.length() && date != other.date) return(false);
if(hour >= 0 && hour != other.hour) return(false);
if(minute >= 0 && minute != other.minute) return(false);
if(type.length() && type != other.type) return(false);
return(true);
}
bool eqHour(const sSpoolDataDirIndex& other) {
return(spool == other.spool &&
sensor == other.sensor &&
date == other.date &&
hour == other.hour);
}
bool isHour() {
return(hour >= 0 &&
minute < 0 &&
!type.length());
}
eTypeSpoolFile getTypeSpoolFile() {
if(type.length()) {
return(_type);
}
return(tsf_na);
}
bool operator == (const sSpoolDataDirIndex& other) const {
return(this->date == other.date &&
this->hour == other.hour &&
this->minute == other.minute &&
this->spool == other.spool &&
this->sensor == other.sensor &&
this->type == other.type);
}
bool operator < (const sSpoolDataDirIndex& other) const {
return(this->date < other.date ? 1 : this->date > other.date ? 0 :
this->hour < other.hour ? 1 : this->hour > other.hour ? 0 :
this->minute < other.minute ? 1 : this->minute > other.minute ? 0 :
this->spool < other.spool ? 1 : this->spool > other.spool ? 0 :
this->sensor < other.sensor ? 1 : this->sensor > other.sensor ? 0 :
this->type < other.type);
}
friend ostream& operator << (ostream& os, const sSpoolDataDirIndex& index) {
if(index.spool.length()) os << "spool: " << index.spool << " ";
if(index.sensor.length()) os << "sensor: " << index.sensor << " ";
if(index.date.length()) os << "date: " << index.date << " ";
if(index.hour >= 0) os << "hour: " << index.hour << " ";
if(index.minute >= 0) os << "minute: " << index.minute << " ";
if(index.type.length()) os << "type: " << index.type << " ";
return(os);
}
string encode_hour();
void decode_hour(string str);
string spool;
string sensor;
string date;
int hour;
int minute;
string type;
eTypeSpoolFile _type;
};
struct sSpoolDataDirItem {
sSpoolDataDirItem() {
size = 0;
is_dir = false;
}
string encode();
void decode(string str);
string path;
long long size;
bool is_dir;
};
class cSpoolData {
public:
cSpoolData() {
_sync = 0;
}
void add(sSpoolDataDirIndex &index, sSpoolDataDirItem &item) {
data[index] = item;
}
long long getSumSize();
long long getSplitSumSize(long long *sip, long long *rtp, long long *graph, long long *audio, long long *audiograph);
void getSumSizeByDate(map<string, long long> *sizeByDate);
map<sSpoolDataDirIndex, sSpoolDataDirItem>::iterator getBegin();
map<sSpoolDataDirIndex, sSpoolDataDirItem>::iterator getMin(bool sip, bool rtp, bool graph, bool audio, bool audiograph);
bool existsFileIndex(sSpoolDataDirIndex *dirIndex);
void erase(map<sSpoolDataDirIndex, sSpoolDataDirItem>::iterator iter) {
data.erase(iter);
}
map<sSpoolDataDirIndex, sSpoolDataDirItem>::iterator end() {
return(data.end());
}
void removeLastDateHours(int hours);
void clearAll() {
data.clear();
}
bool isEmpty() {
return(data.size() == 0);
}
bool saveHourCacheFile(sSpoolDataDirIndex index);
bool loadHourCacheFile(sSpoolDataDirIndex index, string pathHour);
bool existsHourCacheFile(sSpoolDataDirIndex index, string pathHour);
bool deleteHourCacheFile(sSpoolDataDirIndex index);
void fillDateHoursCheckMap();
void clearDateHoursCheckMap();
bool existsDateHourInCheckMap(const char *date, int hour);
void saveDeletedHourCacheFiles();
void eraseDir(string dir);
void lock() {
__SYNC_LOCK(_sync);
}
void unlock() {
__SYNC_UNLOCK(_sync);
}
private:
map<sSpoolDataDirIndex, sSpoolDataDirItem> data;
map<uint64_t, bool> date_hours_map;
list<sSpoolDataDirIndex> list_delete_hour_cache_files;
volatile int _sync;
};
struct sLoadParams {
sLoadParams() {
enable_cache_load = false;
enable_cache_save = false;
no_cache_last_hours = 0;
clean_orphan_dirs_older_than = 0;
}
bool enable_cache_load;
bool enable_cache_save;
int no_cache_last_hours;
int clean_orphan_dirs_older_than;
};
public:
CleanSpool(int spoolIndex);
~CleanSpool();
void addFile(const char *datehour, eTypeSpoolFile typeSpoolFile, const char *file, long long int size);
void run();
void do_convert_filesindex(const char *reason);
void check_filesindex();
void check_index_date(string date, SqlDb *sqlDb);
string getMaxSpoolDate();
void getSumSizeByDate(map<string, long long> *sizeByDate);
string printSumSizeByDate();
string printSumSizeByType();
string getOldestDate();
static void run_cleanProcess(int spoolIndex = -1);
static void run_clean_obsolete(int spoolIndex = -1);
static void run_test_load(string type, int spoolIndex = -1);
static void run_reindex_all(const char *reason, int spoolIndex = -1);
static void run_reindex_date(string date, int spoolIndex = -1);
static void run_reindex_date_hour(string date, int hour, int spoolIndex = -1);
static void run_check_filesindex(int spoolIndex = -1);
static void run_check_spooldir_filesindex(const char *dirfilter = NULL, int spoolIndex = -1);
static void run_reindex_spool(int spoolIndex = -1);
static string run_print_spool(int spoolIndex = -1, bool refresh = false);
static string get_oldest_date(int spoolIndex = -1);
static bool suspend(int spoolIndex = -1);
static bool resume(int spoolIndex = -1);
static bool isSetCleanspoolParameters(int spoolIndex);
static bool isSetCleanspool(int spoolIndex);
static bool check_datehour(const char *datehour);
static bool check_date_dir(const char *datedir);
static bool check_hour_dir(const char *hourdir);
static bool check_minute_dir(const char *minutedir);
static bool check_type_dir(const char *typedir);
static unsigned date_to_int(const char *date);
private:
void reloadSpoolDataDir(bool enableCacheLoad, bool enableCacheSave);
void updateSpoolDataDir();
void loadSpoolDataDir(cSpoolData *spoolData, sSpoolDataDirIndex index, string path, sLoadParams params);
void loadOpt();
void runCleanThread();
void termCleanThread();
static void *cleanThread(void *cleanSpool);
void cleanThread();
void cleanThreadProcess();
void updateSpoolDataForCleanThreadProcess();
bool check_exists_act_records_in_files();
bool check_exists_act_files_in_filesindex();
void reindex_all(const char *reason);
long long reindex_date(string date);
long long reindex_date_hour(string date, int h, bool readOnly = false, map<string, long long> *typeSize = NULL, bool quickCheck = false);
long long reindex_date_hour_type(string date, int h, string type, bool readOnly, bool quickCheck,
map<unsigned, bool> *fillMinutes, bool *existsDhDir);
void unlinkfileslist(eTypeSpoolFile typeSpoolFile, string fname, string callFrom);
void unlink_dirs(string datehour, int sip, int reg, int skinny, int mgcp, int ss7, int rtp, int graph, int audio, int audiograph, string callFrom);
void erase_dir(string dir, sSpoolDataDirIndex index, string callFrom);
void erase_dir_if_empty(string dir, string callFrom = "");
bool dir_is_empty(string dir, bool enableRecursion = false);
bool dir_is_empty(string dir, list<string> *exclude);
string reduk_dir(string dir, string *last_dir);
void clean_spooldir_run();
void clean_maxpoolsize(bool sip, bool rtp, bool graph, bool audio, bool audiograph);
void clean_maxpoolsize_all() {
clean_maxpoolsize(true, true, true, true, true);
}
void clean_maxpoolsize_sip() {
clean_maxpoolsize(true, false, false, false, false);
}
void clean_maxpoolsize_rtp() {
clean_maxpoolsize(false, true, false, false, false);
}
void clean_maxpoolsize_graph() {
clean_maxpoolsize(false, false, true, false, false);
}
void clean_maxpoolsize_audio() {
clean_maxpoolsize(false, false, false, true, false);
}
void clean_maxpoolsize_audiograph() {
clean_maxpoolsize(false, false, false, false, true);
}
void clean_maxpooldays(bool sip, bool rtp, bool graph, bool audio, bool audiograph);
void clean_maxpooldays_all() {
clean_maxpooldays(true, true, true, true, true);
}
void clean_maxpooldays_sip() {
clean_maxpooldays(true, false, false, false, false);
}
void clean_maxpooldays_rtp() {
clean_maxpooldays(false, true, false, false, false);
}
void clean_maxpooldays_graph() {
clean_maxpooldays(false, false, true, false, false);
}
void clean_maxpooldays_audio() {
clean_maxpooldays(false, false, false, true, false);
}
void clean_maxpooldays_audiograph() {
clean_maxpooldays(false, false, false, false, true);
}
void clean_obsolete_dirs();
void test_load(string type);
void check_spooldir_filesindex(const char *dirfilter);
void force_reindex_spool();
string print_spool(bool refresh = false);
unsigned int get_reduk_maxpoolsize(unsigned int maxpoolsize);
bool fileIsOpenTar(list<string> &listOpenTars, string &file);
void readSpoolDateDirs(list<string> *dirs);
void getSpoolDirs(list<string> *spool_dirs);
string findExistsSpoolDirFile(eTypeSpoolFile typeSpoolFile, string pathFile,
eTypeSpoolFile *rsltTypeSpoolFile = NULL);
const char *getSpoolDir(eTypeSpoolFile typeSpoolFile) {
return(::getSpoolDir(typeSpoolFile, spoolIndex));
}
string getSpoolDir_string(eTypeSpoolFile typeSpoolFile) {
return(::getSpoolDir(typeSpoolFile, spoolIndex));
}
string getSpoolIndex_string() {
return(intToString(spoolIndex));
}
string getIdSensor_string() {
extern int opt_id_sensor_cleanspool;
return(intToString(opt_id_sensor_cleanspool > 0 ? opt_id_sensor_cleanspool : 0));
}
string getCondIdSensor_cdr() {
if(is_receiver()) {
return("");
}
extern int opt_id_sensor_cleanspool;
return(opt_id_sensor_cleanspool > 0 ?
"id_sensor = " + intToString(opt_id_sensor_cleanspool) :
"id_sensor is null");
}
private:
int spoolIndex;
CleanSpoolDirs opt_dirs;
CleanSpoolOptMax opt_max;
CleanSpoolOptOther opt_other;
SqlDb *sqlDb;
unsigned int maxpoolsize_set;
bool critical_low_space;
bool do_convert_filesindex_flag;
const char *do_convert_filesindex_reason;
pthread_t clean_thread;
u_long lastCall_reindex_all;
bool suspended;
volatile int clean_spooldir_run_processing;
cSpoolData spoolData;
time_t lastRunLoadSpoolDataDir;
unsigned counterLoadSpoolDataDir;
bool force_reindex_spool_flag;
u_int32_t autoclean_log_at;
};
#endif