-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #3 create init swarm cluster task
- Loading branch information
1 parent
b213970
commit f993456
Showing
40 changed files
with
1,008 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace ApiNodes\Http\Controllers; | ||
|
||
use App\Models\Node; | ||
use App\Models\NodeTask\TaskStatus; | ||
use App\Models\NodeTaskGroup; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Http\Response; | ||
|
||
class NextTaskController | ||
{ | ||
public function __invoke(Node $node) | ||
{ | ||
$taskGroup = $node->taskGroups()->inProgress()->first(); | ||
|
||
$task = $taskGroup ? $this->getNextTaskFromGroup($taskGroup) : $this->pickNextTask($node); | ||
if ($task) { | ||
return $task; | ||
} | ||
|
||
return new Response([ | ||
'message' => 'No task found.' | ||
], 404); | ||
} | ||
|
||
protected function getNextTaskFromGroup(NodeTaskGroup $taskGroup) | ||
{ | ||
if ($taskGroup->tasks()->running()->first()) { | ||
return new Response([ | ||
'error_message' => 'Another task should be already running.' | ||
], 409); | ||
} | ||
|
||
$task = $taskGroup->tasks()->pending()->first(); | ||
|
||
$task->start(); | ||
|
||
return $task; | ||
} | ||
|
||
protected function pickNextTask(Node $node) | ||
{ | ||
$taskGroup = NodeTaskGroup::where('swarm_id', $node->swarm_id)->where(function (Builder $query) use ($node) { | ||
return $query->where('node_id', $node->id)->orWhere('node_id', null); | ||
})->pending()->first(); | ||
|
||
$task = $taskGroup->tasks()->first(); | ||
|
||
$taskGroup->startTask($node, $task); | ||
|
||
return $task; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace ApiNodes\Http\Controllers; | ||
|
||
use App\Casts\TaskResultCast; | ||
use App\Models\NodeTask; | ||
use App\Models\NodeTask\ErrorResult; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class TaskController | ||
{ | ||
public function complete(NodeTask $task, Request $request) | ||
{ | ||
if ($task->is_ended) { | ||
return new Response(['error' => 'Already ended.'], 409); | ||
} | ||
|
||
if ($task->is_pending) { | ||
return new Response(['error' => "Task didn't start yet."], 409); | ||
} | ||
|
||
$result = TaskResultCast::RESULT_BY_TYPE[$task->type]::validateAndCreate($request->all()); | ||
|
||
$task->complete($result); | ||
|
||
return new Response('', 204); | ||
} | ||
|
||
public function fail(NodeTask $task, Request $request) | ||
{ | ||
if ($task->is_ended) { | ||
return new Response(['error' => 'Already ended.'], 409); | ||
} | ||
|
||
if ($task->is_pending) { | ||
return new Response(['error' => "Task didn't start yet."], 409); | ||
} | ||
|
||
$result = ErrorResult::validateAndCreate($request->all()); | ||
|
||
$task->fail($result); | ||
|
||
return new Response('', 204); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\Casts; | ||
|
||
use App\Models\NodeTask; | ||
use App\Models\NodeTask\CreateNetworkTaskPayload; | ||
use App\Models\NodeTask\InitSwarmTaskPayload; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
use InvalidArgumentException; | ||
|
||
class TaskPayloadCast implements CastsAttributes | ||
{ | ||
public const TYPE_BY_PAYLOAD = [ | ||
CreateNetworkTaskPayload::class => 0, | ||
InitSwarmTaskPayload::class => 1 | ||
]; | ||
|
||
public const PAYLOAD_BY_TYPE = [ | ||
0 => CreateNetworkTaskPayload::class, | ||
1 => InitSwarmTaskPayload::class | ||
]; | ||
|
||
/** | ||
* Cast the given value. | ||
* | ||
* @param array<string, mixed> $attributes | ||
*/ | ||
public function get(Model $model, string $key, mixed $value, array $attributes): mixed | ||
{ | ||
if (!($model instanceof NodeTask)) { | ||
throw new InvalidArgumentException('Model must be an instance of NodeTask'); | ||
} | ||
|
||
return self::PAYLOAD_BY_TYPE[$model->type]::from($value); | ||
} | ||
|
||
/** | ||
* Prepare the given value for storage. | ||
* | ||
* @param array<string, mixed> $attributes | ||
*/ | ||
public function set(Model $model, string $key, mixed $value, array $attributes): mixed | ||
{ | ||
if (!($model instanceof NodeTask)) { | ||
throw new InvalidArgumentException('Model must be an instance of NodeTask'); | ||
} | ||
|
||
return $value->toJson(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Casts; | ||
|
||
use App\Models\NodeTask; | ||
use App\Models\NodeTask\CreateNetworkTaskResult; | ||
use App\Models\NodeTask\ErrorResult; | ||
use App\Models\NodeTask\InitSwarmTaskResult; | ||
use InvalidArgumentException; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class TaskResultCast implements CastsAttributes | ||
{ | ||
|
||
public const TYPE_BY_RESULT = [ | ||
CreateNetworkTaskResult::class => 0, | ||
InitSwarmTaskResult::class => 1 | ||
]; | ||
|
||
public const RESULT_BY_TYPE = [ | ||
0 => CreateNetworkTaskResult::class, | ||
1 => InitSwarmTaskResult::class | ||
]; | ||
|
||
/** | ||
* Cast the given value. | ||
* | ||
* @param array<string, mixed> $attributes | ||
*/ | ||
public function get(Model $model, string $key, mixed $value, array $attributes): mixed | ||
{ | ||
if (!($model instanceof NodeTask)) { | ||
throw new InvalidArgumentException('Model must be an instance of NodeTask'); | ||
} | ||
|
||
if ($model->is_failed) { | ||
return ErrorResult::from($value); | ||
} | ||
|
||
if ($model->is_ended) { | ||
return self::RESULT_BY_TYPE[$model->type]::from($value); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Prepare the given value for storage. | ||
* | ||
* @param array<string, mixed> $attributes | ||
*/ | ||
public function set(Model $model, string $key, mixed $value, array $attributes): mixed | ||
{ | ||
if (!($model instanceof NodeTask)) { | ||
throw new InvalidArgumentException('Model must be an instance of NodeTask'); | ||
} | ||
|
||
return $value->toJson(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\NodeTask\InitClusterFormRequest; | ||
use App\Models\Node; | ||
use App\Models\NodeTask\CreateNetworkTaskPayload; | ||
use App\Models\NodeTask\InitSwarmTaskPayload; | ||
use App\Models\NodeTaskGroup; | ||
use App\Models\Swarm; | ||
|
||
class SwarmTaskController extends Controller | ||
{ | ||
public function initCluster(InitClusterFormRequest $request) | ||
{ | ||
$swarm = Swarm::create([ | ||
'name' => $request->name, | ||
]); | ||
|
||
Node::whereId($request->node_id)->update([ | ||
'swarm_id' => $swarm->id, | ||
]); | ||
|
||
$taskGroup = NodeTaskGroup::create([ | ||
'swarm_id' => $swarm->id, | ||
'node_id' => $request->node_id, | ||
'invoker_id' => auth()->user()->id, | ||
]); | ||
|
||
$taskGroup->tasks()->createMany([ | ||
[ | ||
'payload' => new CreateNetworkTaskPayload('ptah-net'), | ||
], | ||
[ | ||
'payload' => new InitSwarmTaskPayload($request->name, $request->force_new_cluster), | ||
] | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\NodeTask; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class InitClusterFormRequest extends FormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function rules(): array | ||
{ | ||
return [ | ||
'node_id' => ['required'], | ||
'name' => ['required', 'string', 'max:255'], | ||
'advertise_addr' => ['required', 'ipv4'], | ||
'force_new_cluster' => ['boolean'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.