DataTransferJob::DNData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 perform() {
23
		echo "[-] DataTransferJob starting" . PHP_EOL;
24
		$log = new DeploynautLogFile($this->args['logfile']);
25
		$dataTransfer = DNDataTransfer::get()->byID($this->args['dataTransferID']);
26
		$environment = $dataTransfer->Environment();
27
		$backupDataTransfer = null;
28
29 View Code Duplication
		if(
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...
30
			!empty($this->args['backupBeforePush'])
31
			&& $this->args['backupBeforePush'] !== 'false'
32
			&& $dataTransfer->Direction == 'push'
33
		) {
34
			$backupDataTransfer = DNDataTransfer::create();
35
			$backupDataTransfer->EnvironmentID = $environment->ID;
36
			$backupDataTransfer->Direction = 'get';
37
			$backupDataTransfer->Mode = $dataTransfer->Mode;
38
			$backupDataTransfer->ResqueToken = $dataTransfer->ResqueToken;
39
			$backupDataTransfer->AuthorID = $dataTransfer->AuthorID;
40
			$backupDataTransfer->write();
41
42
			$dataTransfer->BackupDataTransferID = $backupDataTransfer->ID;
43
			$dataTransfer->write();
44
		}
45
46
		try {
47
			// Disallow concurrent jobs (don't rely on queuing implementation to restrict this)
48
			// Only consider data transfers started in the last 30 minutes (older jobs probably got stuck)
49
			$runningTransfers = DNDataTransfer::get()
50
				->filter(array(
51
					'EnvironmentID' => $environment->ID,
52
					'Status' => array('Queued', 'Started'),
53
					'Created:GreaterThan' => strtotime('-30 minutes')
54
				))
55
				->exclude('ID', $dataTransfer->ID);
56
			if($runningTransfers->count()) {
57
				$runningTransfer = $runningTransfers->first();
58
				$message = sprintf(
59
					'Error: another transfer is in progress (started at %s by %s)',
60
					$runningTransfer->dbObject('Created')->Nice(),
61
					$runningTransfer->Author()->Title
62
				);
63
				$log->write($message);
64
				throw new \RuntimeException($message);
65
			}
66
67
			$this->performBackup($backupDataTransfer, $log);
68
			$environment->Backend()->dataTransfer($dataTransfer, $log);
69
		} catch(Exception $e) {
70
			echo "[-] DataTransferJob failed" . PHP_EOL;
71
			throw $e;
72
		}
73
74
		$this->updateStatus('Finished');
75
		echo "[-] DataTransferJob finished" . PHP_EOL;
76
	}
77
78 View Code Duplication
	protected function performBackup($backupDataTransfer, \DeploynautLogFile $log) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
79
		if (!$backupDataTransfer) {
80
			return false;
81
		}
82
83
		$log->write('Backing up existing data');
84
		try {
85
			$backupDataTransfer->Environment()->Backend()->dataTransfer($backupDataTransfer, $log);
86
			global $databaseConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
87
			DB::connect($databaseConfig);
88
			$backupDataTransfer->Status = 'Finished';
89
			$backupDataTransfer->write();
90
		} catch(Exception $e) {
91
			global $databaseConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
92
			DB::connect($databaseConfig);
93
			$backupDataTransfer->Status = 'Failed';
94
			$backupDataTransfer->write();
95
			throw $e;
96
		}
97
	}
98
99
	/**
100
	 * @param string $status
101
	 * @global array $databaseConfig
102
	 */
103
	protected function updateStatus($status) {
104
		global $databaseConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
105
		DB::connect($databaseConfig);
106
		$transfer = DNDataTransfer::get()->byID($this->args['dataTransferID']);
107
		$transfer->Status = $status;
108
		$transfer->write();
109
	}
110
111
	/**
112
	 * @return DNData
113
	 */
114
	protected function DNData() {
115
		return DNData::inst();
116
	}
117
}
118