Completed
Pull Request — master (#518)
by Michael
03:38
created

DeployJob::onFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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