exfriend/laravel-overseer

Laravel 任务管理器

1.0.3 2016-08-25 22:07 UTC

This package is auto-updated.

Last update: 2024-09-21 20:07:50 UTC


README

logo

允许从 Web/API 控制artisan命令。

概述

此包主要设计用于控制应用程序管理后台的各种后台任务。

安装后,Laravel Overseer 将提供灵活的 JSON API,以便实时检查、启动、停止和监控您的命令。

当与overseer-bootstrap一起使用时,它还将提供由 VueJS 和 Twitter Bootstrap 3 编写的完整功能的仪表板页面。

如果您使用不同的前端或想要自定义任何部分,您可以发布资源并/或使用它们作为参考,在 Overseer API 的基础上编写自己的前端。

您所需要做的就是从 Exfriend\Overseer\Command 扩展您的 Artisan 命令,而不是从 Illuminate\Console\Command 扩展。

安装

composer require exfriend/laravel-overseer

或者

composer require exfriend/overseer-bootstrap

用于带有前端的自定义版本。

然后,将包的服务提供者添加到您的 config/app.php 文件中

// ...
Exfriend\Overseer\OverseerServiceProvider::class,
Exfriend\OverseerBootstrap\OverseerBootstrapServiceProvider::class, // <- only for overseer-bootstrap

创建一个目录以存储日志

cd /path/to/your/project
mkdir ./storage/logs/tasks
chmod -r 666 ./storage/logs/tasks

示例命令

现在您已准备好创建您的第一个 Overseer 命令。基本上它与 Laravel 的控制台命令非常相似,但有一些区别。

<?php

namespace App\Robots\HackerNews;

class Command extends \Exfriend\Robots\Console\Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'scrape:hackernews';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Gets the latest 10 news titles from hackernews';
    
    // this is new thing
    protected $title = 'HackerNews Scraper';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->line( 'Beginning scrape' );
        for ( $i = 1; $i < 10; $i++ )
        {
            sleep(2);
            
            /**
             place this where you need to check 
             if there is a pending "stop" command from API
             to terminate properly
            */
            $this->checkpoint();
            
            // you can set progress% 0..100
            $this->setProgress( $i*10 );
            $this->line( 'Scraping news #'.$i.' of 10');
        }
        $this->line( 'Bye!' );
    }

}

上面的命令描述了您的实际命令应该如何看起来。

使用 API

GET http://your_project/overseer/api/commands

{
    status: "ok",
    data: [
    {
        command: "App\Robots\Rozetka\Command",
        running: false,
        title: "Rozetka Scraper",
        progress: 0,
        last_run: "30.08.2016 23:33:19",
        description: ""
    }
    ]
}

GET http://your_project/overseer/api/command?command=App\Robots\Rozetka\Command

{
status: "ok",
data: {
        command: "App\Robots\Rozetka\Command",
        running: false,
        title: "Rozetka Scraper",
        progress: 0,
        last_run: "30.08.2016 23:33:19",
        description: "",
        short_log: "",
        logs: [
        "scrape:rozetka__2016_08_30_23_33_19.log",
        "scrape:rozetka__2016_08_26_08_20_48.log",
        "scrape:rozetka__2016_08_26_07_24_49.log",
        "scrape:rozetka__2016_08_26_03_25_14.log"
        ]
    }
}

GET http://your_project/overseer/api/current_log?command=App\Robots\Rozetka\Command

{
status: "ok",
data: {
    "[2016-08-31 00:45:26] local.INFO: Beginning scrape [] [] ",
    "[2016-08-31 00:45:26] local.INFO: Scraping category 1 [] [] ",
    "[2016-08-31 00:45:27] local.INFO: Parsing product 1 [] [] ",
    "[2016-08-31 00:45:29] local.INFO: Parsing product 2 [] [] ",
    "[2016-08-31 00:45:31] local.INFO: Parsing product 3 [] [] ",
    }
}

GET http://your_project/overseer/api/run?command=App\Robots\Rozetka\Command

{
status: "ok",
}

GET http://your_project/overseer/api/stop?command=App\Robots\Rozetka\Command

{
status: "ok",
}

GET http://your_project/overseer/api/unlock?command=App\Robots\Rozetka\Command

{
status: "ok",
}

互斥锁

Overseer 使用称为互斥排他机制来防止任务重叠。有时使用“解锁”命令删除互斥文件很有用。这可以用于例如,如果您的任务因未捕获的异常而死亡并且无法正确删除锁文件。在这种情况下,您的系统将认为此任务正在运行,直到您手动删除锁文件或使用解锁 API 方法。

控制台改进

Overseer 为您从控制台运行的每个命令添加了 2 个选项

--force (-f) - ignore mutex lock
--unlock (-u) - delete the mutex lock

图形界面

如果您正在寻找此包的基于网页的用户界面,请务必查看overseer-bootstrap

贡献

此包仍在开发中。欢迎提交拉取请求。前方还有大量工作要做!