Completed
Pull Request — master (#594)
by Mateusz
03:09
created

DeployJob::alarmHandler()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 39
rs 6.7272
cc 7
eloc 24
nc 4
nop 0
1
<?php
2
3
/**
4
 * Runs a deployment via the most appropriate backend
5
 */
6
class DeployJob extends DeploynautJob {
7
8
	public function setUp() {
9
		parent::setUp();
10
		$this->updateStatus(DNDeployment::TR_DEPLOY);
11
	}
12
13
	public function perform() {
14
		echo "[-] DeployJob starting" . PHP_EOL;
15
		$log = new DeploynautLogFile($this->args['logfile']);
16
17
		$deployment = DNDeployment::get()->byID($this->args['deploymentID']);
18
		$environment = $deployment->Environment();
19
		$project = $environment->Project();
20
		// This is a bit icky, but there is no easy way of capturing a failed deploy by using the PHP Resque
21
		try {
22
			// Disallow concurrent deployments (don't rely on queuing implementation to restrict this)
23
			// Only consider deployments started in the last 30 minutes (older jobs probably got stuck)
24
			$runningDeployments = $environment->runningDeployments()->exclude('ID', $this->args['deploymentID']);
25 View Code Duplication
			if($runningDeployments->count()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
				$runningDeployment = $runningDeployments->first();
27
				$log->write(sprintf(
28
					'[-] Error: another deployment is in progress (started at %s by %s)',
29
					$runningDeployment->dbObject('Created')->Nice(),
30
					$runningDeployment->Deployer()->Title
31
				));
32
				throw new RuntimeException(sprintf(
33
					'Another deployment is in progress (started at %s by %s)',
34
					$runningDeployment->dbObject('Created')->Nice(),
35
					$runningDeployment->Deployer()->Title
36
				));
37
			}
38
39
			$environment->Backend($this)->deploy(
40
				$environment,
41
				$log,
42
				$project,
43
				// Pass all args to give the backend full visibility. These args also contain
44
				// all options from the DeploymentStrategy merged in, including sha.
45
				$this->args
46
			);
47
		} catch(Exception $e) {
48
			// DeploynautJob will automatically trigger onFailure.
49
			echo "[-] DeployJob failed" . PHP_EOL;
50
			throw $e;
51
		}
52
		$this->updateStatus(DNDeployment::TR_COMPLETE);
53
		echo "[-] DeployJob finished" . PHP_EOL;
54
	}
55
56
	public function onFailure(Exception $exception) {
57
		$this->updateStatus(DNDeployment::TR_FAIL);
58
	}
59
60
	/**
61
	 * @param string $status Transition
62
	 * @global array $databaseConfig
63
	 */
64
	protected function updateStatus($status) {
65
		global $databaseConfig;
66
		DB::connect($databaseConfig);
67
		$dnDeployment = DNDeployment::get()->byID($this->args['deploymentID']);
68
		$dnDeployment->getMachine()->apply($status);
69
	}
70
71
	/**
72
	 * @return DNData
73
	 */
74
	protected function DNData() {
75
		return DNData::inst();
76
	}
77
}
78