# Task_2

`class` in `plugin-tasks` · v0.1.0-next.1

<https://seedcord.org/docs/packages/plugin-tasks/0.1.0-next.1/classes/task-2>

Base class for a task. A field doesn't keep its value between runs since each run gets a fresh
instance.

Import it from `@seedcord/plugin-tasks/gateway` or `/http` to type `this.core` as your bot's `Core`.

```ts
abstract class Task_2<
    TData = void,
    TCore extends CoreBase = CoreBase
>
```

## Examples

```ts
\@RegisterTask()
export class Unban extends Task<{ guildId: string; userId: string }> {
    public async execute(): Promise<void> {
        const { guildId, userId } = this.data;
        await this.core.rest.delete(Routes.guildBan(guildId, userId));
    }
}
```

## Constructors

### constructor

```ts
Task_2(context: TaskContext<TData>)
```

Constructs a new instance of the `Task` class

## Properties

### core

```ts
protected readonly core: TCore
```

### data

```ts
protected readonly data: TaskData<TData>
```

The scheduled payload after its trip through the store.

### key

```ts
public readonly key: string
```

The task's key, from `@RegisterTask` or the class name.

### logger

```ts
protected readonly logger: Logger
```

Logs under the task's key, on the channel of the plugin's attach key.

### signal

```ts
protected readonly signal: AbortSignal
```

Aborts when the bot shuts down. Pass it to anything slow.

### startedAt

```ts
protected readonly startedAt: Date
```

### trigger

```ts
protected readonly trigger: TaskTrigger
```

## Methods

### execute

```ts
public abstract execute(): Promise<void>
```

The task's work. The plugin logs a throw, publishes it as a failed `taskRan`, and passes it to `onError()`.

### onError

```ts
public async onError?(error: Error): Promise<void>
```

Runs after `execute()` throws and the plugin has logged the error. If `execute()` throws after shutdown aborted `signal`, the plugin skips this.

### onOverrun

```ts
public async onOverrun?(): Promise<void>
```

Runs in place of `execute()` when a `protect`ed task gets a cron or manual run before its previous run has finished.
