মূল কনটেন্টে যান
১৪ দিনের ফ্রি ট্রায়াল, কার্ড লাগবে না৯টি ভাষা১৭৬টি ব্যবসার ধরন · ৫০টি অ্যাপ
BN· USD
লাইভ ডেমো চালিয়ে দেখুনফ্রি ট্রায়াল শুরু করুন

এই পেজটি এখনো বাংলা ভাষায় পাওয়া যায় না, তাই ইংরেজিতে দেখানো হচ্ছে।

Scheduled jobs and plugin API routes

Jobs run from the maintenance tick; API routes are published under /api/plugins/<id> and must authenticate every call.

  • about 8 min

Jobs

Declare { id, everyMinutes } in module.json → jobs and implement register().jobs[<id>]: async (ctx) => string | void with ctx = { pluginId, log, now }. The maintenance tick (every 5 minutes by default) runs each job whose interval has elapsed, so the real granularity is about 5 minutes. The returned string (≤ 500 chars) is shown as the last result; Run now is on the plugin page.

A daily job
jobs: {
  "today-digest": async (ctx) => {
    const start = new Date(ctx.now); start.setUTCHours(0, 0, 0, 0);
    const end = new Date(start); end.setUTCDate(end.getUTCDate() + 1);
    const n = await prisma.siteVisit.count({ where: { status: "PLANNED", plannedAt: { gte: start, lt: end } } });
    return n + " visit(s) today";
  },
},
Jobs must be idempotent and finish within a minute. Chunk long work with a cursor in pluginStore. For “on every event” work use a hook instead of polling.

API routes

app/api/<path>/route.ts exporting GET / POST / … is published at /api/plugins/<id>/<path>. The generated wrapper answers 404 while the plugin is disabled and 405 for methods you do not export. The proxy's api rate limit (300 burst, 5 req/s per IP) applies.

modules/site-visits/app/api/today/route.ts → GET /api/plugins/site-visits/today
import { NextResponse } from "next/server";
import { currentUser, can } from "@/lib/auth/guards";
import { prisma } from "@/lib/prisma";

export async function GET() {
  const user = await currentUser();
  if (!user || !can(user, "sitevisits.view")) return NextResponse.json({ error: "forbidden" }, { status: 403 });
  const rows = await prisma.siteVisit.findMany({ where: { status: "PLANNED" }, orderBy: { plannedAt: "asc" }, take: 50 });
  return NextResponse.json({ data: rows.map((r) => ({ id: r.id, customerName: r.customerName, plannedAt: r.plannedAt })) });
}

Authentication: use the session (currentUser + can) for calls from the admin, or a plugin-issued token stored hashed in pluginStore for machine clients. The REST API v1 key checker is not exported for plugins yet. Always validate the body with zod and never echo secrets.

Written from: docs/PLUGIN-DEVELOPER-GUIDE.md, docs/PLUGINS.md, modules/example-checklist/app/api/items/route.ts, src/lib/plugins/types.ts