Completed
Pull Request — master (#489)
by Helpful
03:18
created

DeployJob::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

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