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

DeployJob::onFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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