调试
This commit is contained in:
parent
3d4b28a545
commit
ed2e24d4ab
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\Remote;
|
||||
use App\Jobs\HostCost;
|
||||
use App\Jobs\UserSave;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
@ -22,6 +23,9 @@ protected function schedule(Schedule $schedule)
|
||||
// dispatch HostCost job
|
||||
$schedule->job(new HostCost())->everyFiveMinutes();
|
||||
$schedule->job(new UserSave())->everyTenMinutes();
|
||||
$schedule->job(new Remote\FetchModule())->everyMinute()->onOneServer();
|
||||
$schedule->job(new Remote\PushHost())->everyMinute()->onOneServer();
|
||||
$schedule->job(new Remote\PushWorkOrder())->everyMinute()->onOneServer();
|
||||
|
||||
}
|
||||
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\ApiResponse;
|
||||
// use App\Helpers\ApiResponse;
|
||||
use App\Models\User;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
// use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
@ -19,6 +19,15 @@ class AuthController extends Controller
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
|
||||
|
||||
// test
|
||||
$handle = new \App\Jobs\Remote\PushHost();
|
||||
$handle->handle();
|
||||
|
||||
$handle = new \App\Jobs\Remote\PushWorkOrder();
|
||||
$handle->handle();
|
||||
|
||||
// if logged in
|
||||
if (Auth::check()) {
|
||||
$token_name = 'login token ' . now()->toDateString();
|
||||
|
@ -30,9 +30,10 @@ public function store(Request $request) {
|
||||
return $this->error('module_id 和 host_id 至少要填写一个');
|
||||
}
|
||||
|
||||
|
||||
$workOrder = WorkOrder::create([
|
||||
'title' => $request->title,
|
||||
'content' => $request->content,
|
||||
'content' => $request['content'],
|
||||
'module_id' => $request->module_id,
|
||||
'host_id' => $request->host_id,
|
||||
'status' => 'pending',
|
||||
|
53
app/Jobs/Remote/FetchModule.php
Normal file
53
app/Jobs/Remote/FetchModule.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Remote;
|
||||
|
||||
use App\Models\Module\Module;
|
||||
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;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class FetchModule implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
//
|
||||
Module::chunk(100, function ($modules) {
|
||||
foreach ($modules as $module) {
|
||||
$http = Http::remote($module->api_token, $module->url);
|
||||
// dd($module->url);
|
||||
$response = $http->get('remote');
|
||||
|
||||
if ($response->successful()) {
|
||||
Cache::set('module_' . $module->id, $response->status());
|
||||
// $module->update([
|
||||
// 'data' => $response->json()
|
||||
// ]);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
52
app/Jobs/Remote/PushHost.php
Normal file
52
app/Jobs/Remote/PushHost.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Remote;
|
||||
|
||||
use App\Models\Host;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
|
||||
class PushHost implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
//
|
||||
Host::whereIn('status', ['pending', 'error'])->with(['module', 'user'])->chunk(100, function ($hosts) {
|
||||
foreach ($hosts as $host) {
|
||||
$http = Http::remote($host->module->api_token, $host->module->url);
|
||||
$host->status = 'running';
|
||||
|
||||
$response = $http->post('hosts', $host->toArray());
|
||||
|
||||
if (!$response->successful()) {
|
||||
$host->status = 'error';
|
||||
}
|
||||
|
||||
$host->save();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
60
app/Jobs/Remote/PushWorkOrder.php
Normal file
60
app/Jobs/Remote/PushWorkOrder.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Remote;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
|
||||
class PushWorkOrder implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
//
|
||||
WorkOrder::whereIn('status', ['pending', 'error'])->with(['module', 'user', 'host'])->chunk(100, function ($workOrders) {
|
||||
foreach ($workOrders as $workOrder) {
|
||||
|
||||
if ($workOrder->host->status === 'pending') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$http = Http::remote($workOrder->module->api_token, $workOrder->module->url);
|
||||
$workOrder->status = 'open';
|
||||
|
||||
$response = $http->post('work-orders', $workOrder->toArray());
|
||||
|
||||
if (!$response->successful()) {
|
||||
$workOrder->status = 'error';
|
||||
}
|
||||
|
||||
$workOrder->save();
|
||||
|
||||
|
||||
dd($response);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\User;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Module\Module;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class Host extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $table = 'hosts';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'module_id',
|
||||
'user_id',
|
||||
'price',
|
||||
'configuration',
|
||||
'status',
|
||||
'managed_price',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'configuration' => 'array'
|
||||
];
|
||||
|
||||
|
||||
// user
|
||||
public function user() {
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// module
|
||||
public function module() {
|
||||
return $this->belongsTo(Module::class);
|
||||
}
|
||||
|
||||
// workorders
|
||||
public function workorders() {
|
||||
return $this->hasMany(Workorder::class);
|
||||
}
|
||||
|
||||
// module 远程一对一
|
||||
// public function module() {
|
||||
// return $this->hasOneThrough(Module::class, ProviderModule::class);
|
||||
// }
|
||||
|
||||
|
||||
// scope
|
||||
public function scopeActive($query) {
|
||||
return $query->where('status', 'running')->where('price', '!=', 0);
|
||||
}
|
||||
|
||||
// on create
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
// $model->load('module');
|
||||
// $model->module->load(['provider', 'module']);
|
||||
|
||||
// add to queue
|
||||
|
||||
});
|
||||
}
|
||||
}
|
@ -2,11 +2,12 @@
|
||||
|
||||
namespace App\Models\WorkOrder;
|
||||
|
||||
use App\Exceptions\CommonException;
|
||||
use App\Models\Host;
|
||||
use App\Models\User;
|
||||
use App\Models\Module\Module;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use App\Exceptions\CommonException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class WorkOrder extends Model
|
||||
{
|
||||
@ -23,6 +24,10 @@ class WorkOrder extends Model
|
||||
'status',
|
||||
];
|
||||
|
||||
// user
|
||||
public function user() {
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// replies
|
||||
public function replies()
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
@ -24,5 +25,12 @@ public function register()
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
|
||||
Http::macro('remote', function ($api_token, $url) {
|
||||
return Http::withHeaders([
|
||||
'X-Remote-Api-Token' => $api_token,
|
||||
])->baseUrl($url);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('modules', function (Blueprint $table) {
|
||||
//
|
||||
$table->string('url')->nullable()->after('api_token')->index();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('modules', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user