(new): support Bot API 10.3
Golang lint / lint (push) Failing after 1m37s

(fix): finalize v2 contracts
(tests): cover v2 migration
(doc): prepare release guidance
This commit is contained in:
2026-09-08 23:21:38 +03:00
parent 24040fe164
commit d78526242b
88 changed files with 2321 additions and 918 deletions
+14 -34
View File
@@ -6,16 +6,8 @@ import (
"time"
)
// RunnerFn is the legacy function type for a runner. It receives a pointer to
// the Bot and returns an error if execution fails. New blocking or I/O work
// should use ContextRunnerFn so runtime cancellation can stop the callback.
//
// Subject to change in v2: runner callbacks may require context.Context.
type RunnerFn[T AppData] func(*Bot[T]) error
// ContextRunnerFn is a cancelable runner function.
// The runtime context is canceled when polling or webhook execution stops.
type ContextRunnerFn[T AppData] func(context.Context, *Bot[T]) error
// RunnerFn is a cancelable task executed by a Bot runtime.
type RunnerFn[T AppData] func(context.Context, *Bot[T]) error
// Runner represents a configurable background or one-time task to be
// executed by a Bot.
@@ -33,22 +25,6 @@ type Runner[T AppData] struct {
async bool // If true, runs in a goroutine; else, runs synchronously
every time.Duration // Interval between periodic executions; zero means one-shot
fn RunnerFn[T] // The function to execute
ctxFn ContextRunnerFn[T]
}
func executeRunnerWithContext[T AppData](ctx context.Context, runner Runner[T], bot *Bot[T]) (err error) {
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("runner %q panicked: %v", runner.name, recovered)
}
}()
if runner.ctxFn != nil {
return runner.ctxFn(ctx, bot)
}
if runner.fn == nil {
return fmt.Errorf("runner %q has no function", runner.name)
}
return runner.fn(bot)
}
// NewRunner creates a new Runner with the given name and function.
@@ -57,18 +33,10 @@ func executeRunnerWithContext[T AppData](ctx context.Context, runner Runner[T],
// goroutine that fires once when the bot runtime starts. Use Async and Every
// to customize this. Do not call builder methods concurrently or after the
// bot runtime has begun executing runners.
//
// Subject to change in v2: NewContextRunner may become the primary constructor.
func NewRunner[T AppData](name string, fn RunnerFn[T]) Runner[T] {
return Runner[T]{name: name, fn: fn, async: true, every: 0}
}
// NewContextRunner creates a runner whose callback observes runtime cancellation.
// It is the preferred constructor for I/O, blocking, and periodic work.
func NewContextRunner[T AppData](name string, fn ContextRunnerFn[T]) Runner[T] {
return Runner[T]{name: name, ctxFn: fn, async: true, every: 0}
}
// Async sets whether the runner executes synchronously or asynchronously.
// If true, the runner runs in a goroutine (non-blocking).
// If false, the runner blocks the caller during execution.
@@ -198,3 +166,15 @@ func (bot *Bot[T]) ExecRunners(ctx context.Context) {
}
}
}
func executeRunnerWithContext[T AppData](ctx context.Context, runner Runner[T], bot *Bot[T]) (err error) {
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("runner %q panicked: %v", runner.name, recovered)
}
}()
if runner.fn == nil {
return fmt.Errorf("runner %q has no function", runner.name)
}
return runner.fn(ctx, bot)
}