Completed
Pull Request — master (#500)
by Michael
04:43
created

DeployJob   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 78
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 4 1
B perform() 0 41 3
A updateStatus() 0 7 1
A DNData() 0 3 1
1
<?php
2
3
/**
4
 * Runs a deployment via the most appropriate backend
5
 */
6
class DeployJob {
7
8
	/**
9
	 * @var array
10
	 */
11
	public $args;
12
13
	public function setUp() {
14
		$this->updateStatus('Started');
15
		chdir(BASE_PATH);
16
	}
17
18
	public function tearDown() {
19
		$this->updateStatus('Finished');
20
		chdir(BASE_PATH);
21
	}
22
23
	public function perform() {
24
		echo "[-] DeployJob starting" . PHP_EOL;
25
		$log = new DeploynautLogFile($this->args['logfile']);
26
27
		$deployment = DNDeployment::get()->byID($this->args['deploymentID']);
28
		$environment = $deployment->Environment();
29
		$project = $environment->Project();
30
		// This is a bit icky, but there is no easy way of capturing a failed deploy by using the PHP Resque
31
		try {
32
			// Disallow concurrent deployments (don't rely on queuing implementation to restrict this)
33
			// Only consider deployments started in the last 30 minutes (older jobs probably got stuck)
34
			$runningDeployments = $environment->runningDeployments()->exclude('ID', $this->args['deploymentID']);
35
			if($runningDeployments->count()) {
36
				$runningDeployment = $runningDeployments->first();
37
				$log->write(sprintf(
38
					'[-] Error: another deployment is in progress (started at %s by %s)',
39
					$runningDeployment->dbObject('Created')->Nice(),
40
					$runningDeployment->Deployer()->Title
41
				));
42
				throw new RuntimeException(sprintf(
43
					'Another deployment is in progress (started at %s by %s)',
44
					$runningDeployment->dbObject('Created')->Nice(),
45
					$runningDeployment->Deployer()->Title
46
				));
47
			}
48
49
			$environment->Backend()->deploy(
50
				$environment,
51
				$log,
52
				$project,
53
				// Pass all args to give the backend full visibility. These args also contain
54
				// all options from the DeploymentStrategy merged in, including sha.
55
				$this->args
56
			);
57
		} catch(Exception $e) {
58
			$this->updateStatus('Failed');
59
			echo "[-] DeployJob failed" . PHP_EOL;
60
			throw $e;
61
		}
62
		echo "[-] DeployJob finished" . PHP_EOL;
63
	}
64
65
	/**
66
	 * @param string $status
67
	 * @global array $databaseConfig
68
	 */
69
	protected function updateStatus($status) {
70
		global $databaseConfig;
71
		DB::connect($databaseConfig);
72
		$dnDeployment = DNDeployment::get()->byID($this->args['deploymentID']);
73
		$dnDeployment->Status = $status;
74
		$dnDeployment->write();
75
	}
76
77
	/**
78
	 * @return DNData
79
	 */
80
	protected function DNData() {
81
		return DNData::inst();
82
	}
83
}
84