Skip to content

Commit

Permalink
fix consecutive deployment issue (#62)
Browse files Browse the repository at this point in the history
* fix consecutive deployment issue

* fix styles
  • Loading branch information
saeedvaziry authored Sep 29, 2023
1 parent 1e1204f commit 38e23a1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=null
CACHE_DRIVER=redis
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=default
SESSION_DRIVER=database
Expand Down
3 changes: 1 addition & 2 deletions app/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

abstract class Job implements ShouldQueue, ShouldBeUnique
abstract class Job implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
}
44 changes: 22 additions & 22 deletions app/Jobs/Site/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Enums\DeploymentStatus;
use App\Events\Broadcast;
use App\Helpers\SSH;
use App\Jobs\Job;
use App\Models\Deployment;
use App\SSHCommands\System\RunScriptCommand;
Expand All @@ -18,42 +17,43 @@ class Deploy extends Job

protected string $script;

protected SSH $ssh;

public function __construct(Deployment $deployment, string $path, string $script)
public function __construct(Deployment $deployment, string $path)
{
$this->script = $deployment->deploymentScript->content;
$this->deployment = $deployment;
$this->path = $path;
$this->script = $script;
}

/**
* @throws Throwable
*/
public function handle(): void
{
$this->ssh = $this->deployment->site->server->ssh();
$this->ssh->exec(
new RunScriptCommand($this->path, $this->script),
'deploy',
$this->deployment->site_id
);
$this->deployment->status = DeploymentStatus::FINISHED;
$this->deployment->log_id = $this->ssh->log->id;
$this->deployment->save();
event(
new Broadcast('deploy-site-finished', [
'deployment' => $this->deployment,
])
);
$ssh = $this->deployment->site->server->ssh();
try {
$ssh->exec(
new RunScriptCommand($this->path, $this->script),
'deploy',
$this->deployment->site_id
);
$this->deployment->status = DeploymentStatus::FINISHED;
$this->deployment->log_id = $ssh->log->id;
$this->deployment->save();
event(
new Broadcast('deploy-site-finished', [
'deployment' => $this->deployment,
])
);
} catch (Throwable) {
$this->deployment->log_id = $ssh->log->id;
$this->deployment->save();
$this->failed();
}
}

public function failed(): void
{
$this->deployment->status = DeploymentStatus::FAILED;
if ($this->ssh->log) {
$this->deployment->log_id = $this->ssh->log->id;
}
$this->deployment->save();
event(
new Broadcast('deploy-site-failed', [
Expand Down
8 changes: 1 addition & 7 deletions app/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,7 @@ public function deploy(): Deployment
}
$deployment->save();

dispatch(
new Deploy(
$deployment,
$this->path,
$this->deployment_script_text
)
)->onConnection('ssh');
dispatch(new Deploy($deployment, $this->path))->onConnection('ssh');

return $deployment;
}
Expand Down
4 changes: 1 addition & 3 deletions resources/commands/system/run-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ if ! cd __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi

if ! __script__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
__script__
4 changes: 1 addition & 3 deletions tests/Feature/SSHCommands/System/RunScriptCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public function test_generate_command()
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! script; then
echo 'VITO_SSH_ERROR' && exit 1
fi
script
EOD;

$this->assertStringContainsString($expected, $command->content());
Expand Down

0 comments on commit 38e23a1

Please sign in to comment.