forked from FreePBX/userman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Job.php
67 lines (64 loc) · 2.13 KB
/
Job.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
<?php
namespace FreePBX\modules\Userman;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
class Job implements \FreePBX\Job\TaskInterface {
public static function run(InputInterface $input, OutputInterface $output) {
$userman = \FreePBX::create()->Userman;
$directories = $userman->getAllDirectories();
foreach($directories as $directory) {
self::syncDirectory($userman, $directory,$output);
}
return true;
}
public static function syncDirectory($userman, $directory,$output) {
if(!$directory['active']) {
$output->writeln("Directory '".$directory['name']."' is not active. Skipping");
return;
}
$dir = $userman->getDirectoryObjectByID($directory['id']);
if(method_exists($dir,"sync")) {
if(!$force && empty($directory['config']['sync'])) {
$output->writeln("Directory '".$directory['name']."' sync is None. Skipping (Unless --force flag is set)");
return;
}
$timeSince = $userman->getConfig("directory-last-sync-time");
$timeSince = !empty($timeSince) ? $timeSince : 0;
$timeNow = time();
$secondsSince = 0;
switch($directory['config']['sync']) {
case "*/15 * * * *":
$secondsSince = 900;
break;
case "*/30 * * * *":
$secondsSince = 1800;
break;
case "0 * * * *":
$secondsSince = 3600;
break;
case "0 */6 * * *":
$secondsSince = 7200;
break;
case "0 0 * * *":
$secondsSince = 14400;
break;
}
if($force || ($timeNow > ($timeSince + $secondsSince))) {
$userman->setConfig("directory-last-sync-time", $timeNow);
$output->writeln("Starting Sync on directory '".$directory['name']."'...");
$userman->lockDirectory($directory['id']);
try {
$dir->sync($output);
} catch(\Exception $e) {
$output->writeln("\t<error>".$e->getMessage()."</error>");
}
$userman->unlockDirectory($directory['id']);
$output->writeln("Finished");
} else {
$output->writeln("Not syncing directory for another ".(($timeSince + $secondsSince)-$timeNow)." seconds");
}
} else {
$output->writeln("Directory '".$directory['name']."' does not support syncing");
}
}
}