Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added once feature #1314

Merged
merged 6 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
### Added
- Added possibility to use callable when setting 'default_stage'
- Added console init template for TYPO3 CMS [#1300]
- Added possibility to run a task only once [#1311]
- Added `git_recursive` option
- Added `shallow` task option

### Removed
- Removed `terminate_message` option


## v5.1.3
[v5.1.2...v5.1.3](https://github.com/deployphp/deployer/compare/v5.1.2...v5.1.3)

Expand Down Expand Up @@ -270,7 +272,7 @@

## v4.0.0
🙄

[#1311]: https://github.com/deployphp/deployer/pull/1311
[#1300]: https://github.com/deployphp/deployer/pull/1300
[#1290]: https://github.com/deployphp/deployer/pull/1290
[#1283]: https://github.com/deployphp/deployer/pull/1283
Expand Down
34 changes: 34 additions & 0 deletions src/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ class Task
*/
private $private = false;

/**
* Mark task to run only once, of the first node from the pool
*
* @var bool
*/
private $once = false;

/**
* Mark if the task has run at least once
*
* @var bool
*/
private $hasRun = false;

/**
* Shallow task will not print execution message/finish massage.
* Useful for success messages and info printing.
Expand Down Expand Up @@ -89,6 +103,10 @@ public function run(Context $context)
// Call task
call_user_func($this->callback);

if ($this->once) {
$this->hasRun = true;
}

// Clear working_path
if ($context->getConfig() !== null) {
$context->getConfig()->set('working_path', false);
Expand Down Expand Up @@ -142,6 +160,17 @@ public function isLocal()
return $this->local;
}

public function once()
{
$this->once = true;
return $this;
}

public function isOnce()
{
return $this->once;
}

/**
* @param array $hosts
* @return $this
Expand Down Expand Up @@ -180,6 +209,11 @@ public function onStage(...$stages)
*/
public function shouldBePerformed(...$hosts)
{
// don't allow to run again it the task has been marked to run only once
if ($this->once && $this->hasRun) {
return false;
}

foreach ($hosts as $host) {
$onHost = empty($this->on['hosts']) || in_array($host->getHostname(), $this->on['hosts'], true);

Expand Down
35 changes: 35 additions & 0 deletions test/src/Task/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function testTask()

$task->setPrivate();
self::assertTrue($task->isPrivate());

$task->once();
self::assertTrue($task->isOnce());
}

public function testShouldBePerformed()
Expand Down Expand Up @@ -126,6 +129,38 @@ public function testInit()
$task3->run($context);
self::assertEquals(1, StubTask::$runned);
}

public function testOnce()
{
$a = (new Host('a'))->stage('prod')->roles('app');
$b = (new Host('b'))->stage('prod')->roles('app');

$context = self::getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();

// Test create task with [$object, 'method']
$mock = self::getMockBuilder('stdClass')
->setMethods(['callback'])
->getMock();

$task1 = new Task('only:once', [$mock, 'callback']);
$task1
->onHosts('a', 'b')
->once();
self::assertTrue($task1->shouldBePerformed($a));
self::assertEquals([true, true], array_map([$task1, 'shouldBePerformed'], [$a, $b]));
$task1->run($context);
self::assertFalse($task1->shouldBePerformed($b));
self::assertEquals([false, false], array_map([$task1, 'shouldBePerformed'], [$a, $b]));

$task2 = new Task('multiple:runs', [$mock, 'callback']);
$task2
->onHosts('a', 'b');
self::assertTrue($task2->shouldBePerformed($a));
self::assertEquals([true, true], array_map([$task2, 'shouldBePerformed'], [$a, $b]));
$task2->run($context);
self::assertTrue($task2->shouldBePerformed($b));
self::assertEquals([true, true], array_map([$task2, 'shouldBePerformed'], [$a, $b]));
}
}

/**
Expand Down