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

DataTransferJob::perform()   B

Complexity

Conditions 6
Paths 22

Size

Total Lines 70
Code Lines 47

Duplication

Lines 13
Ratio 18.57 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 70
rs 8.5454
cc 6
eloc 47
nc 22
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Starts a capistrano script for either retrieving data from an environment,
5
 * or pushing data into an environment. Initiated by {@link DNDataTransfer}.
6
 *
7
 * @package deploynaut
8
 * @subpackage jobs
9
 */
10
class DataTransferJob extends DeploynautJob {
11
12
	/**
13
	 * set by a resque worker
14
	 */
15
	public $args = array();
16
17
	public function setUp() {
18
		$this->updateStatus('Started');
19
		chdir(BASE_PATH);
20
	}
21
22
	public function tearDown() {
23
		$this->updateStatus('Finished');
24
		chdir(BASE_PATH);
25
	}
26
27
	public function perform() {
28
		echo "[-] DataTransferJob starting" . PHP_EOL;
29
		$log = new DeploynautLogFile($this->args['logfile']);
30
		$dataTransfer = DNDataTransfer::get()->byID($this->args['dataTransferID']);
31
		$environment = $dataTransfer->Environment();
32
		$backupDataTransfer = null;
33
34
		if(!empty($this->args['backupBeforePush']) && $dataTransfer->Direction == 'push') {
35
			$backupDataTransfer = DNDataTransfer::create();
36
			$backupDataTransfer->EnvironmentID = $environment->ID;
37
			$backupDataTransfer->Direction = 'get';
38
			$backupDataTransfer->Mode = $dataTransfer->Mode;
39
			$backupDataTransfer->DataArchiveID = null;
40
			$backupDataTransfer->ResqueToken = $dataTransfer->ResqueToken;
41
			$backupDataTransfer->AuthorID = $dataTransfer->AuthorID;
42
			$backupDataTransfer->write();
43
44
			$dataTransfer->BackupDataTransferID = $backupDataTransfer->ID;
45
			$dataTransfer->write();
46
		}
47
48
		// This is a bit icky, but there is no easy way of capturing a failed run by using the PHP Resque
49
		try {
50
			// Disallow concurrent jobs (don't rely on queuing implementation to restrict this)
51
			// Only consider data transfers started in the last 30 minutes (older jobs probably got stuck)
52
			$runningTransfers = DNDataTransfer::get()
53
				->filter(array(
54
					'EnvironmentID' => $environment->ID,
55
					'Status' => array('Queued', 'Started'),
56
					'Created:GreaterThan' => strtotime('-30 minutes')
57
				))
58
				->exclude('ID', $dataTransfer->ID);
59
60 View Code Duplication
			if($runningTransfers->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...
61
				$runningTransfer = $runningTransfers->first();
62
				$log->write(sprintf(
63
					'[-] Error: another transfer is in progress (started at %s by %s)',
64
					$runningTransfer->dbObject('Created')->Nice(),
65
					$runningTransfer->Author()->Title
66
				));
67
				throw new RuntimeException(sprintf(
68
					'Another transfer is in progress (started at %s by %s)',
69
					$runningTransfer->dbObject('Created')->Nice(),
70
					$runningTransfer->Author()->Title
71
				));
72
			}
73
74
75
			// before we push data to an environment, we'll make a backup first
76
			if($backupDataTransfer) {
77
				$log->write('Backing up existing data');
78
				$environment->Backend()->dataTransfer(
79
					$backupDataTransfer,
80
					$log
81
				);
82
			}
83
84
			$environment->Backend()->dataTransfer(
85
				$dataTransfer,
86
				$log
87
			);
88
		} catch(RuntimeException $exc) {
89
			$log->write($exc->getMessage());
90
91
			echo "[-] DataTransferJob failed" . PHP_EOL;
92
			throw $exc;
93
		}
94
95
		echo "[-] DataTransferJob finished" . PHP_EOL;
96
	}
97
98
	/**
99
	 * @param string $status
100
	 * @global array $databaseConfig
101
	 */
102
	protected function updateStatus($status) {
103
		global $databaseConfig;
104
		DB::connect($databaseConfig);
105
		$env = DNDataTransfer::get()->byID($this->args['dataTransferID']);
106
		$env->Status = $status;
107
		$env->write();
108
109
		$backup = $env->BackupDataTransfer();
110
		if($backup && $backup->exists()) {
111
			$backup->Status = $status;
112
			$backup->write();
113
		}
114
	}
115
116
	/**
117
	 * @return DNData
118
	 */
119
	protected function DNData() {
120
		return DNData::inst();
121
	}
122
}
123