Declare task

Before using a task, you must define a task, and defining the task is simple. Set a task as follows:

 /**
 * Class TestTask
 *
 * @since 2.0
 *
 * @Task(name="testTask")
 */
class TestTask
{
    /**
     * @TaskMapping(name="list")
     *
     * @param int    $id
     * @param string $default
     *
     * @return array
     */
    public function getList(int $id, string $default = 'def'): array
    {
        return [
            'list'    => [1, 3, 3],
            'id'      => $id,
            'default' => $default
        ];
    }

    /**
     * @TaskMapping()
     *
     * @param int $id
     *
     * @return bool
     */
    public function delete(int $id): bool
    {
        if ($id > 10) {
            return true;
        }

        return false;
    }
}     

@Task

Tag class is a task

  • Name specifies the task name, the default full path class name

@TaskMapping

Map name

  • Name name mapping, default to the method name of the class
note
  1. Each method that is marked by @Task is a task. If the method does not use the @TaskMapping annotation, it will not be resolved into a task.
  2. Before the Task is delivered, it will be json_encode via the Swoft\Task\Packet::pack() method. Therefore, when the entity is delivered, the task gets an array of entities. At the same time, the delivery of parameters that cannot be json_encode will result in an error (if it is an asynchronous task and no additional logs are enabled, the effect may be that the task ends directly without error message).
/docs/2.x/en/task/statement.html
progress-bar