پرش به مطلب اصلی

توابع کمکی کارهای پس‌زمینه (background-job.ts)

توابع کمکی مربوط به اجرای کارهای پس‌زمینه و پردازش غیرهمگام.

addBackgroundJob

افزودن کار پس‌زمینه (deprecated - استفاده از safeAddBackgroundJob توصیه می‌شود).

function addBackgroundJob<T = any>(profile: AddBackgroundJobProfile<T>): string

پارامترها:

  • profile.data: داده‌های ورودی
  • profile.execute: تابع اجرایی
  • profile.parentClass: کلاس والد
  • profile.statusResponse: تابع پاسخ وضعیت

بازگشتی: ID کار پس‌زمینه

safeAddBackgroundJob

افزودن کار پس‌زمینه امن (پشتیبانی از حالت چندپردازشی).

function safeAddBackgroundJob<T = any>(
profile: AddSafeBackgroundJobProfile<T>
): string

پارامترها:

  • profile.data: داده‌های ورودی
  • profile.functionName: نام تابع API
  • profile.apiClassName: نام کلاس API
  • profile.appName: نام برنامه

بازگشتی: ID کار پس‌زمینه

مثال:

const jobId = safeAddBackgroundJob({
appName: 'myapp',
apiClassName: 'EmailService',
functionName: 'sendWelcomeEmail',
data: { userId: 123, email: 'user@example.com' }
});

getBackgroundJobStatus

دریافت وضعیت کار پس‌زمینه.

function getBackgroundJobStatus(id: string): any

پارامترها:

  • id: ID کار پس‌زمینه

بازگشتی: شیء وضعیت شامل id، status و error

getSafeBackgroundJobStatus

دریافت وضعیت کار پس‌زمینه امن (Promise-based).

function getSafeBackgroundJobStatus(id: string): Promise<safeBackgroundJobStatusResponse>

پارامترها:

  • id: ID کار پس‌زمینه

بازگشتی: Promise که شیء وضعیت را برمی‌گرداند

مثال:

const status = await getSafeBackgroundJobStatus(jobId);
console.log(status.status); // 'running', 'completed', 'failed'
if (status.error) {
console.error('Job failed:', status.error);
}