Skip to content

Commit deaf780

Browse files
committed
Adds timeout flag to most commands, adjusts default timeout to be logical for each command
1 parent 94c9446 commit deaf780

File tree

9 files changed

+57
-8
lines changed

9 files changed

+57
-8
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Usage involves first reading the rest of this README, then running one of the co
1717

1818
/usr/local/bin/macupdate list --recommended --restart
1919

20-
This will display a list of all pending updates that are recommend and require a restart.
20+
This will display a list of all pending updates that are recommended and require a restart.
2121

2222
## Important Difference
2323
It is important to note the one way in which MacUpdate differs from using the `softwareupdate` binary directly, which involves the usage of the `--restart` and `--shutdown` flags.
@@ -46,6 +46,7 @@ The most useful command is `summary`, which will output a count of total, recomm
4646
|--|--|
4747
| json | Output results in JSON format. |
4848
| no-scan | Do not scan for new updates, used cached results. |
49+
| timeout | The maximum time in seconds that the softwareupdate process should be allowed to run. |
4950

5051
### Wait Command
5152
This command is only useful when used in other scripting. It will wait for the given number of seconds, or when conditions are met, based on the flags given at runtime. The command can wait for the following conditions to clear:
@@ -84,7 +85,8 @@ An example of where this command is useful is when users can only access the Sof
8485
|Flags | Purpose |
8586
|--|--|
8687
| json | Output results in JSON format. |
87-
| no-scan | Do not scan for new updates, used cached results. |
88+
| no-scan | Do not scan for new updates, used cached results. |
89+
| timeout | The maximum time in seconds that the softwareupdate process should be allowed to download. |
8890
| recommend | Only include recommended updates. |
8991
| restart | Only include updates requiring a restart. |
9092
| shutdown | Only include updates requiring a shutdown. |
@@ -104,6 +106,7 @@ Several conditions are checked to verify that it is safe to install updates:
104106
|--|--|
105107
| json | Output results in JSON format. |
106108
| no-scan | Do not scan for new updates, used cached results. |
109+
| timeout | The maximum time in seconds that the softwareupdate process should be allowed to run. |
107110
| force | Install even if on battery power or user is logged in. |
108111
| recommend | Only include recommended updates. |
109112
| restart | Only include updates requiring a restart. |
@@ -116,6 +119,7 @@ Several conditions are checked to verify that it is safe to install updates:
116119
|--|--|
117120
| json | Output results in JSON format. |
118121
| no-scan | Do not scan for new updates, used cached results. |
122+
| timeout | The maximum time in seconds that the softwareupdate process should be allowed to run. |
119123
| quiet | Displays only the name of the updates. |
120124
| recommend | Only include recommended updates. |
121125
| restart | Only include updates requiring a restart. |
@@ -129,6 +133,7 @@ This command is mostly redundant; the `summary` command is more useful, and can
129133
|Flags | Purpose |
130134
|--|--|
131135
| no-scan | Do not scan for new updates, used cached results. |
136+
| timeout | The maximum time in seconds that the softwareupdate process should be allowed to run. |
132137
| quiet | Display no results, just a return code. |
133138
| recommend | Only include recommended updates. |
134139
| restart | Only include updates requiring a restart. |

bin/console

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ $command[] = new DevCoding\Mac\Update\Command\DownloadCommand();
1414
$command[] = new DevCoding\Mac\Update\Command\InstallCommand();
1515
$command[] = new DevCoding\Mac\Update\Command\SummaryCommand();
1616
$command[] = new DevCoding\Mac\Update\Command\WaitCommand();
17-
$app = new Application('MacUpdate', 'v1.6.4');
17+
$app = new Application('MacUpdate', 'v1.7');
1818
$app->addCommands($command);
1919
$app->run();

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"DevCoding\\Mac\\Update\\": "src"
1515
}
1616
},
17-
"version": "1.6.4",
17+
"version": "1.7",
1818
"require": {
1919
"php": "^7.0",
2020
"ext-json": "*",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/macupdate.phar

1.27 KB
Binary file not shown.

src/Command/AbstractUpdateConsole.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected function configure()
3232
->addOption('restart', null, InputOption::VALUE_NONE, 'Only include restart updates')
3333
->addOption('shutdown', null, InputOption::VALUE_NONE, 'Only include shutdown updates')
3434
->addOption('no-scan', null, InputOption::VALUE_NONE, 'Do not scan for new updates')
35+
->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'Software Update timeout in seconds.', $this->getDefaultTimeout())
3536
;
3637
}
3738

@@ -40,7 +41,34 @@ protected function isAllowUserOption()
4041
return false;
4142
}
4243

43-
// endregion ///////////////////////////////////////////// End Symfony Copmmand Methods
44+
/**
45+
* Returns the default number of seconds that softwareupdate should be allowed to run before determining that
46+
* it has stalled. This can be overridden with the --timeout option.
47+
*
48+
* @return int
49+
*/
50+
protected function getDefaultTimeout()
51+
{
52+
return 7200; # 2 Hours
53+
}
54+
55+
/**
56+
* Returns the default number of seconds that softwareupdate should be allowed to run before determining that
57+
* it has stalled. Set with the --timeout option, which provides a default based on the getDefaultTimeout method.
58+
*
59+
* @throws \Exception If for some reason, the value of the timeout option is falsy.
60+
*/
61+
private function getTimeout()
62+
{
63+
if (!$timeout = $this->io()->getOption('timeout'))
64+
{
65+
throw new \Exception('A default timeout for softwareupdate was not provided.');
66+
}
67+
68+
return (is_numeric($timeout) && $timeout > 0) ? $timeout : null;
69+
}
70+
71+
// endregion ///////////////////////////////////////////// End Symfony Command Methods
4472

4573
// region //////////////////////////////////////////////// Software Update Methods
4674

@@ -162,10 +190,11 @@ protected function getUpdateList()
162190
if ($suBin = $this->getSoftwareUpdate())
163191
{
164192
// Create Process
193+
$timeout = $this->getTimeout();
165194
$noScan = $this->isNoScan() ? '--no-scan' : null;
166195
$Process = Process::fromShellCommandline(trim(sprintf('%s -l %s', $suBin, $noScan)));
167196
// Eliminate Timeouts
168-
$Process->setIdleTimeout(null)->setTimeout(null);
197+
$Process->setIdleTimeout($timeout)->setTimeout($timeout);
169198
// Run the Process
170199
$Process->run();
171200

@@ -300,7 +329,8 @@ protected function isNoScan()
300329
*/
301330
protected function runSoftwareUpdate($cmd, &$errors = [])
302331
{
303-
$P = Process::fromShellCommandline($cmd)->setTimeout(86400)->setIdleTimeout(86400);
332+
$t = $this->getTimeout();
333+
$P = Process::fromShellCommandline($cmd)->setTimeout($t)->setIdleTimeout($t);
304334
$P->run();
305335

306336
if (!$P->isSuccessful())

src/Command/DownloadCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ protected function configure()
1414
parent::configure();
1515
}
1616

17+
protected function getDefaultTimeout()
18+
{
19+
return 14400; # 4 Hours
20+
}
21+
1722
protected function execute(InputInterface $input, OutputInterface $output)
1823
{
1924
$this->io()->msg('Checking For Updates', 50);

src/Command/InstallCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ protected function isForced()
2222
return $this->io()->getOption('force') ? true : false;
2323
}
2424

25+
/**
26+
* @return int
27+
*/
28+
protected function getDefaultTimeout()
29+
{
30+
return 21600; #6 Hours
31+
}
32+
2533
protected function execute(InputInterface $input, OutputInterface $output)
2634
{
2735
$errors = [];

src/Command/SummaryCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected function configure()
1414
$this->setName('summary');
1515
$this->addOption('json', null, InputOption::VALUE_NONE, 'Output results in JSON');
1616
$this->addOption('no-scan', null, InputOption::VALUE_NONE, 'Do not scan for new updates.');
17+
$this->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'Software Update timeout in seconds.', $this->getDefaultTimeout());
1718
}
1819

1920
protected function execute(InputInterface $input, OutputInterface $output)

0 commit comments

Comments
 (0)