@appconda/nextjs 1.0.161 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/actionClient.d.ts +8 -2
- package/dist/actions/actionClient.js +21 -15
- package/dist/actions/auth.d.ts +1 -0
- package/dist/actions/auth.js +7 -0
- package/dist/actions/authOptions.d.ts +5 -0
- package/dist/actions/authOptions.js +235 -0
- package/dist/actions/index.d.ts +3 -0
- package/dist/actions/index.js +4 -0
- package/dist/actions/nextAuthHandler.d.ts +1 -0
- package/dist/actions/nextAuthHandler.js +6 -0
- package/dist/client.d.ts +1 -1
- package/dist/client.js +1 -0
- package/dist/decorators/Cache.js +1 -0
- package/dist/decorators/CacheKey.js +1 -0
- package/dist/decorators/Invalidate.js +1 -0
- package/dist/enums/api-service.js +1 -0
- package/dist/enums/api.js +1 -0
- package/dist/enums/auth-method.js +1 -0
- package/dist/enums/browser.js +1 -0
- package/dist/enums/compression.js +1 -0
- package/dist/enums/credit-card.js +1 -0
- package/dist/enums/database-usage-range.js +1 -0
- package/dist/enums/email-template-locale.js +1 -0
- package/dist/enums/email-template-type.js +1 -0
- package/dist/enums/entities/EntityLimitType.js +1 -0
- package/dist/enums/entities/PropertyAttributeName.js +1 -0
- package/dist/enums/entities/PropertyCondition.js +1 -0
- package/dist/enums/entities/PropertyType.js +1 -0
- package/dist/enums/entities/PropertyValueType.js +1 -0
- package/dist/enums/entities/RowAccess.js +1 -0
- package/dist/enums/entities/ViewFilterCondition.js +1 -0
- package/dist/enums/execution-method.js +1 -0
- package/dist/enums/flag.js +1 -0
- package/dist/enums/function-usage-range.js +1 -0
- package/dist/enums/image-format.js +1 -0
- package/dist/enums/image-gravity.js +1 -0
- package/dist/enums/index-type.js +1 -0
- package/dist/enums/messaging-provider-type.js +1 -0
- package/dist/enums/name.js +1 -0
- package/dist/enums/o-auth-provider copy.js +1 -0
- package/dist/enums/password-hash.js +1 -0
- package/dist/enums/platform-type.js +1 -0
- package/dist/enums/project-usage-range.js +1 -0
- package/dist/enums/region.js +1 -0
- package/dist/enums/relation-mutate.js +1 -0
- package/dist/enums/relationship-type.js +1 -0
- package/dist/enums/resource-type.js +1 -0
- package/dist/enums/runtime.js +1 -0
- package/dist/enums/s-m-t-p-secure.js +1 -0
- package/dist/enums/shared/ApplicationLayout.js +1 -0
- package/dist/enums/shared/Colors.js +1 -0
- package/dist/enums/shared/InputType.js +1 -0
- package/dist/enums/shared/Periodicity.js +1 -0
- package/dist/enums/shared/SvgIcon.js +1 -0
- package/dist/enums/shared/Theme.js +1 -0
- package/dist/enums/sms-template-locale.js +1 -0
- package/dist/enums/sms-template-type.js +1 -0
- package/dist/enums/smtp-encryption.js +1 -0
- package/dist/enums/storage-usage-range.js +1 -0
- package/dist/enums/subscriptions/PricingModel.js +1 -0
- package/dist/enums/subscriptions/SubscriptionBillingPeriod.js +1 -0
- package/dist/enums/subscriptions/SubscriptionFeatureLimitType.js +1 -0
- package/dist/enums/subscriptions/SubscriptionPriceType.js +1 -0
- package/dist/enums/tenants/LinkedAccountStatus.js +1 -0
- package/dist/enums/tenants/TenantUserJoined.js +1 -0
- package/dist/enums/tenants/TenantUserStatus.js +1 -0
- package/dist/enums/tenants/TenantUserType.js +1 -0
- package/dist/enums/user-usage-range.js +1 -0
- package/dist/getAppcondaClient.js +5 -3
- package/dist/getSDKForCurrentUser.d.ts +8 -2
- package/dist/getSDKForCurrentUser.js +13 -5
- package/dist/getSDKForService.js +3 -1
- package/dist/getSDKForTenant.d.ts +20 -0
- package/dist/getSDKForTenant.js +44 -0
- package/dist/id.js +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +5 -1
- package/dist/inputFile.js +1 -0
- package/dist/lib/Cache/Adapter.js +1 -0
- package/dist/lib/Cache/Adapters/Filesystem.js +1 -0
- package/dist/lib/Cache/Adapters/Memory.js +1 -0
- package/dist/lib/Cache/Adapters/None.js +1 -0
- package/dist/lib/Cache/Adapters/Sharding.js +1 -0
- package/dist/lib/Cache/Cache.js +1 -0
- package/dist/lib/Cache/index.js +1 -0
- package/dist/lib/Cache/test.js +1 -0
- package/dist/lib/Registry/Registry.js +1 -0
- package/dist/lib/Registry/index.js +1 -0
- package/dist/lib/Services.js +1 -0
- package/dist/lib/crypto.d.ts +23 -0
- package/dist/lib/crypto.js +78 -0
- package/dist/lib/env.d.ts +1 -0
- package/dist/lib/env.js +137 -0
- package/dist/lib/errors.d.ts +70 -0
- package/dist/lib/errors.js +76 -0
- package/dist/lib/index.d.ts +2 -0
- package/dist/lib/index.js +3 -0
- package/dist/lib/jwt.d.ts +12 -0
- package/dist/lib/jwt.js +103 -0
- package/dist/models.js +1 -0
- package/dist/modules/account/actions.d.ts +20 -14
- package/dist/modules/account/actions.js +1 -0
- package/dist/modules/account/enums/authentication-factor.js +1 -0
- package/dist/modules/account/enums/authenticator-type.js +1 -0
- package/dist/modules/account/enums/o-auth-provider.js +1 -0
- package/dist/modules/account/index.js +1 -0
- package/dist/modules/account/schema.d.ts +8 -8
- package/dist/modules/account/schema.js +1 -0
- package/dist/modules/account/service.js +1 -0
- package/dist/modules/account/types.js +1 -0
- package/dist/modules/acl/service.d.ts +26 -0
- package/dist/modules/acl/service.js +27 -0
- package/dist/modules/agent/action.d.ts +27 -23
- package/dist/modules/agent/action.js +1 -0
- package/dist/modules/agent/index.js +1 -0
- package/dist/modules/agent/schema.d.ts +18 -18
- package/dist/modules/agent/schema.js +1 -0
- package/dist/modules/agent/service.js +1 -0
- package/dist/modules/agent/types.js +1 -0
- package/dist/modules/ai/index.js +1 -0
- package/dist/modules/ai/node/actions.d.ts +1 -1
- package/dist/modules/ai/node/actions.js +1 -0
- package/dist/modules/ai/node/index.js +1 -0
- package/dist/modules/ai/node/service.js +1 -0
- package/dist/modules/hooks/index.d.ts +2 -0
- package/dist/modules/hooks/index.js +3 -0
- package/dist/modules/hooks/lib/Hooks.d.ts +32 -0
- package/dist/modules/hooks/lib/Hooks.js +64 -0
- package/dist/modules/hooks/lib/handler.d.ts +12 -0
- package/dist/modules/hooks/lib/handler.js +23 -0
- package/dist/modules/hooks/lib/jobs/index.d.ts +2 -0
- package/dist/modules/hooks/lib/jobs/index.js +3 -0
- package/dist/modules/hooks/lib/jobs/job1.d.ts +1 -0
- package/dist/modules/hooks/lib/jobs/job1.js +31 -0
- package/dist/modules/hooks/lib/jobs/log-job.d.ts +1 -0
- package/dist/modules/hooks/lib/jobs/log-job.js +9 -0
- package/dist/modules/hooks/lib/wait.d.ts +8 -0
- package/dist/modules/hooks/lib/wait.js +13 -0
- package/dist/modules/index.d.ts +4 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/scheduled-job/action.d.ts +97 -0
- package/dist/modules/scheduled-job/action.js +77 -0
- package/dist/modules/scheduled-job/index.d.ts +7 -0
- package/dist/modules/scheduled-job/index.js +8 -0
- package/dist/modules/scheduled-job/lib/Schedules.d.ts +32 -0
- package/dist/modules/scheduled-job/lib/Schedules.js +64 -0
- package/dist/modules/scheduled-job/lib/handler.d.ts +12 -0
- package/dist/modules/scheduled-job/lib/handler.js +23 -0
- package/dist/modules/scheduled-job/lib/jobs/index.d.ts +2 -0
- package/dist/modules/scheduled-job/lib/jobs/index.js +3 -0
- package/dist/modules/scheduled-job/lib/jobs/job1.d.ts +1 -0
- package/dist/modules/scheduled-job/lib/jobs/job1.js +30 -0
- package/dist/modules/scheduled-job/lib/jobs/log-job.d.ts +1 -0
- package/dist/modules/scheduled-job/lib/jobs/log-job.js +9 -0
- package/dist/modules/scheduled-job/lib/wait.d.ts +8 -0
- package/dist/modules/scheduled-job/lib/wait.js +13 -0
- package/dist/modules/scheduled-job/schema.d.ts +52 -0
- package/dist/modules/scheduled-job/schema.js +23 -0
- package/dist/modules/scheduled-job/service.d.ts +13 -0
- package/dist/modules/scheduled-job/service.js +25 -0
- package/dist/modules/scheduled-job/types.d.ts +29 -0
- package/dist/modules/scheduled-job/types.js +2 -0
- package/dist/modules/task/action.d.ts +201 -0
- package/dist/modules/task/action.js +147 -0
- package/dist/modules/task/index.d.ts +4 -0
- package/dist/modules/task/index.js +5 -0
- package/dist/modules/task/schema.d.ts +107 -0
- package/dist/modules/task/schema.js +44 -0
- package/dist/modules/task/service.d.ts +19 -0
- package/dist/modules/task/service.js +43 -0
- package/dist/modules/task/types.d.ts +84 -0
- package/dist/modules/task/types.js +2 -0
- package/dist/modules/tenant/actions.d.ts +49 -0
- package/dist/modules/tenant/actions.js +38 -0
- package/dist/modules/tenant/index.d.ts +3 -0
- package/dist/modules/tenant/index.js +4 -0
- package/dist/{services → modules/tenant}/tenant.d.ts +3 -3
- package/dist/modules/tenant/tenant.js +125 -0
- package/dist/modules/tenant/types.d.ts +11 -0
- package/dist/modules/tenant/types.js +2 -0
- package/dist/modules/waitlist/action.d.ts +31 -29
- package/dist/modules/waitlist/action.js +1 -0
- package/dist/modules/waitlist/index.js +1 -0
- package/dist/modules/waitlist/schema.d.ts +12 -12
- package/dist/modules/waitlist/schema.js +1 -0
- package/dist/modules/waitlist/service.js +1 -0
- package/dist/modules/waitlist/types.js +1 -0
- package/dist/permission.js +1 -0
- package/dist/query.js +1 -0
- package/dist/role.js +1 -0
- package/dist/schemas/nodes.js +1 -0
- package/dist/service-client.js +1 -0
- package/dist/service.js +1 -0
- package/dist/services/applets.js +1 -0
- package/dist/services/avatars.js +1 -0
- package/dist/services/chat-flow.js +1 -0
- package/dist/services/community.js +1 -0
- package/dist/services/configuration.js +1 -0
- package/dist/services/databases.js +1 -0
- package/dist/services/functions.js +1 -0
- package/dist/services/graphql.js +1 -0
- package/dist/services/health.js +1 -0
- package/dist/services/locale.js +1 -0
- package/dist/services/messaging.js +1 -0
- package/dist/services/permissions.js +1 -0
- package/dist/services/pricing.js +1 -0
- package/dist/services/projects.js +1 -0
- package/dist/services/roles.js +1 -0
- package/dist/services/schema.js +1 -0
- package/dist/services/storage.js +1 -0
- package/dist/services/subscription.js +1 -0
- package/dist/services/teams.js +1 -0
- package/dist/services/tenant-subscription.js +1 -0
- package/dist/services/users.js +1 -0
- package/package.json +4 -1
- package/src/actions/actionClient.ts +13 -6
- package/src/actions/auth.ts +7 -0
- package/src/actions/authOptions.ts +255 -0
- package/src/actions/index.ts +3 -0
- package/src/actions/nextAuthHandler.ts +9 -0
- package/src/client.ts +1 -1
- package/src/getAppcondaClient.ts +4 -3
- package/src/getSDKForCurrentUser.ts +12 -5
- package/src/getSDKForService.ts +2 -1
- package/src/getSDKForTenant.ts +52 -0
- package/src/index.ts +4 -2
- package/src/lib/crypto.ts +105 -0
- package/src/lib/env.ts +145 -0
- package/src/lib/errors.ts +138 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/jwt.ts +113 -0
- package/src/modules/account/actions.ts +1 -1
- package/src/modules/acl/service.ts +55 -0
- package/src/modules/agent/action.ts +2 -0
- package/src/modules/ai/node/actions.ts +0 -1
- package/src/modules/hooks/index.ts +4 -0
- package/src/modules/hooks/lib/Hooks.ts +103 -0
- package/src/modules/hooks/lib/handler.ts +27 -0
- package/src/modules/hooks/lib/jobs/index.ts +2 -0
- package/src/modules/hooks/lib/jobs/job1.ts +35 -0
- package/src/modules/hooks/lib/jobs/log-job.ts +9 -0
- package/src/modules/hooks/lib/wait.ts +17 -0
- package/src/modules/index.ts +5 -1
- package/src/modules/scheduled-job/action.ts +94 -0
- package/src/modules/scheduled-job/index.ts +8 -0
- package/src/modules/scheduled-job/lib/Schedules.ts +103 -0
- package/src/modules/scheduled-job/lib/handler.ts +27 -0
- package/src/modules/scheduled-job/lib/jobs/index.ts +2 -0
- package/src/modules/scheduled-job/lib/jobs/job1.ts +34 -0
- package/src/modules/scheduled-job/lib/jobs/log-job.ts +9 -0
- package/src/modules/scheduled-job/lib/wait.ts +17 -0
- package/src/modules/scheduled-job/schema.ts +31 -0
- package/src/modules/scheduled-job/service.ts +37 -0
- package/src/modules/scheduled-job/types.ts +35 -0
- package/src/modules/task/action.ts +175 -0
- package/src/modules/task/index.ts +4 -0
- package/src/modules/task/schema.ts +53 -0
- package/src/modules/task/service.ts +62 -0
- package/src/modules/task/types.ts +89 -0
- package/src/modules/tenant/actions.ts +46 -0
- package/src/modules/tenant/index.ts +3 -0
- package/src/{services → modules/tenant}/tenant.ts +3 -4
- package/src/modules/tenant/types.ts +12 -0
- package/tsconfig.json +4 -1
- package/dist/actions/nodes.d.ts +0 -4
- package/dist/actions/nodes.js +0 -15
- package/dist/enums/authentication-factor.d.ts +0 -6
- package/dist/enums/authentication-factor.js +0 -7
- package/dist/enums/authenticator-type.d.ts +0 -3
- package/dist/enums/authenticator-type.js +0 -4
- package/dist/enums/o-auth-provider.d.ts +0 -41
- package/dist/enums/o-auth-provider.js +0 -42
- package/dist/services/account.d.ts +0 -530
- package/dist/services/account.js +0 -1259
- package/dist/services/node.d.ts +0 -5
- package/dist/services/node.js +0 -11
- package/dist/services/tenant.js +0 -124
@@ -10,7 +10,7 @@ export const CreateUser = actionClient
|
|
10
10
|
.schema(CreateUserSchema)
|
11
11
|
.action(async ({ parsedInput }): Promise<User> => {
|
12
12
|
try {
|
13
|
-
const { userId, email, password, name } = parsedInput;
|
13
|
+
const { userId, email, password, name } = parsedInput as any;
|
14
14
|
const { accounts } = await getSDKForCurrentUser();
|
15
15
|
return await accounts.create(userId, email, password, name);
|
16
16
|
|
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
import { Payload } from "../../client";
|
3
|
+
import { ServiceClient } from "../../service-client";
|
4
|
+
|
5
|
+
type Blacklist = {
|
6
|
+
id: string
|
7
|
+
createdAt: Date
|
8
|
+
type: string
|
9
|
+
value: string
|
10
|
+
active: boolean
|
11
|
+
registerAttempts: number
|
12
|
+
}
|
13
|
+
|
14
|
+
export class Acl extends ServiceClient {
|
15
|
+
|
16
|
+
protected getServiceName(): string {
|
17
|
+
return 'com.appconda.service.acl';
|
18
|
+
}
|
19
|
+
|
20
|
+
public async getBlacklist(
|
21
|
+
filters?: any,
|
22
|
+
pagination?: { page: number; pageSize: number }
|
23
|
+
): Promise<{ items: Blacklist[]; pagination: any }> {
|
24
|
+
|
25
|
+
const payload: Payload = {};
|
26
|
+
|
27
|
+
return await this.actionCall('GetBlackList', payload);
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
public async addToBlackList(type: string, value: string ): Promise<Blacklist> {
|
32
|
+
|
33
|
+
const payload: Payload = {};
|
34
|
+
payload['type'] = type;
|
35
|
+
payload['value'] = value;
|
36
|
+
return await this.actionCall('AddToBlackList', payload);
|
37
|
+
|
38
|
+
}
|
39
|
+
|
40
|
+
public async deleteFromBlackList(type: string, value: string ): Promise<boolean> {
|
41
|
+
|
42
|
+
const payload: Payload = {};
|
43
|
+
payload['type'] = type;
|
44
|
+
payload['value'] = value;
|
45
|
+
return await this.actionCall('DeleteFromBlackList', payload);
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
public async getAllTenantIpAddresses(): Promise<{ items: any[]; pagination: any }> {
|
50
|
+
|
51
|
+
const payload: Payload = {};
|
52
|
+
return await this.actionCall('GetAllTenantIpAddresses', payload);
|
53
|
+
|
54
|
+
}
|
55
|
+
}
|
@@ -0,0 +1,103 @@
|
|
1
|
+
|
2
|
+
type TaskPayload = {
|
3
|
+
timestamp: Date;
|
4
|
+
lastTimestamp?: Date;
|
5
|
+
timezone: string;
|
6
|
+
};
|
7
|
+
|
8
|
+
type TaskContext = {
|
9
|
+
retriesLeft: number;
|
10
|
+
};
|
11
|
+
|
12
|
+
type TriggerArgs = {
|
13
|
+
id: string;
|
14
|
+
events: string[];
|
15
|
+
maxDuration?: number; // saniye
|
16
|
+
retries?: number;
|
17
|
+
run: (
|
18
|
+
payload: TaskPayload,
|
19
|
+
tools: { ctx: TaskContext }
|
20
|
+
) => Promise<void> | void;
|
21
|
+
};
|
22
|
+
|
23
|
+
export class Hooks {
|
24
|
+
private lastRunTimestamps: Record<string, Date> = {};
|
25
|
+
private triggers: TriggerArgs[] = [];
|
26
|
+
|
27
|
+
public trigger(args: TriggerArgs) {
|
28
|
+
this.triggers.push(args);
|
29
|
+
}
|
30
|
+
|
31
|
+
public async run(hookId: string) {
|
32
|
+
const now = new Date();
|
33
|
+
const lastTimestamp = this.lastRunTimestamps[hookId];
|
34
|
+
|
35
|
+
const payload: TaskPayload = {
|
36
|
+
timestamp: now,
|
37
|
+
lastTimestamp,
|
38
|
+
timezone: "UTC",
|
39
|
+
};
|
40
|
+
|
41
|
+
this.lastRunTimestamps[hookId] = now;
|
42
|
+
|
43
|
+
const context: TaskContext = {
|
44
|
+
retriesLeft: this.triggers.find(hook => hook.id === hookId)?.retries ?? 0,
|
45
|
+
};
|
46
|
+
|
47
|
+
await this.runWithRetry(this.triggers.find(hook => hook.id === hookId)!, payload, context);
|
48
|
+
|
49
|
+
|
50
|
+
}
|
51
|
+
|
52
|
+
private async runWithRetry(
|
53
|
+
trigger: TriggerArgs,
|
54
|
+
payload: TaskPayload,
|
55
|
+
ctx: TaskContext
|
56
|
+
) {
|
57
|
+
const start = Date.now();
|
58
|
+
|
59
|
+
try {
|
60
|
+
await Promise.race([
|
61
|
+
trigger.run(payload, { ctx }),
|
62
|
+
timeout(trigger.maxDuration ?? 60),
|
63
|
+
]);
|
64
|
+
log("success", { taskId: trigger.id, time: new Date().toISOString() });
|
65
|
+
} catch (err) {
|
66
|
+
//@ts-ignore
|
67
|
+
log("error", { taskId: trigger.id, error: err });
|
68
|
+
|
69
|
+
if (ctx.retriesLeft > 0) {
|
70
|
+
ctx.retriesLeft -= 1;
|
71
|
+
//@ts-ignore
|
72
|
+
log("warn", { taskId: trigger.id, msg: "Retrying..." });
|
73
|
+
await this.runWithRetry(trigger, payload, ctx);
|
74
|
+
}
|
75
|
+
} finally {
|
76
|
+
const duration = Date.now() - start;
|
77
|
+
//@ts-ignore
|
78
|
+
log("info", { taskId: trigger.id, durationMs: duration });
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
public toJSON() {
|
83
|
+
return this.triggers.map(trigger => ({
|
84
|
+
id: trigger.id,
|
85
|
+
event: trigger.events,
|
86
|
+
maxDuration: trigger.maxDuration,
|
87
|
+
retries: trigger.retries,
|
88
|
+
}));
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
function log(arg0: string, arg1: { taskId: string; time: string; }) {
|
93
|
+
// console.log(arg0, arg1);
|
94
|
+
}
|
95
|
+
|
96
|
+
function timeout(seconds: number) {
|
97
|
+
return new Promise((_, reject) =>
|
98
|
+
setTimeout(() => reject(new Error("Timeout reached")), seconds * 1000)
|
99
|
+
);
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
export const hooks = new Hooks();
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
2
|
+
|
3
|
+
import './jobs';
|
4
|
+
import { hooks } from './Hooks';
|
5
|
+
|
6
|
+
/* export async function GET() {
|
7
|
+
return NextResponse.json({ schedules: schedules.toJSON() },
|
8
|
+
{ status: 200 });
|
9
|
+
}
|
10
|
+
|
11
|
+
export async function POST(request: Request) {
|
12
|
+
|
13
|
+
} */
|
14
|
+
|
15
|
+
export async function hookHandler(request: NextRequest, res: NextResponse) {
|
16
|
+
if (request.method === 'POST') {
|
17
|
+
const body = await request.json();
|
18
|
+
const hookId = body.hookId;
|
19
|
+
hooks.run(hookId);
|
20
|
+
return NextResponse.json({ message: 'Callback triggered' },
|
21
|
+
{ status: 200 });
|
22
|
+
} else {
|
23
|
+
return NextResponse.json({ hooks: hooks.toJSON() },
|
24
|
+
{ status: 200 });
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import { schedules } from "../../../scheduled-job";
|
2
|
+
import { hooks } from "../Hooks";
|
3
|
+
|
4
|
+
hooks.trigger({
|
5
|
+
id: "job1",
|
6
|
+
events: ["event1"],
|
7
|
+
run: async () => {
|
8
|
+
console.log("hello from job 1");
|
9
|
+
},
|
10
|
+
});
|
11
|
+
|
12
|
+
|
13
|
+
hooks.trigger({
|
14
|
+
id: "job2",
|
15
|
+
events: ["event2"],
|
16
|
+
run: async () => {
|
17
|
+
console.log("hello from job 2");
|
18
|
+
},
|
19
|
+
});
|
20
|
+
|
21
|
+
schedules.task({
|
22
|
+
id: "job3",
|
23
|
+
cron: "*/5 * * * *",
|
24
|
+
run: async () => {
|
25
|
+
console.log("hello from job 3");
|
26
|
+
},
|
27
|
+
});
|
28
|
+
|
29
|
+
schedules.task({
|
30
|
+
id: "job4",
|
31
|
+
cron: "*/5 * * * *",
|
32
|
+
run: async () => {
|
33
|
+
console.log("hello from job 4");
|
34
|
+
},
|
35
|
+
});
|
@@ -0,0 +1,17 @@
|
|
1
|
+
export const wait = {
|
2
|
+
for: ({ seconds }: { seconds: number }) =>
|
3
|
+
new Promise((resolve) => setTimeout(resolve, seconds * 1000)),
|
4
|
+
|
5
|
+
until: ({ date }: { date: Date }) => {
|
6
|
+
const now = new Date();
|
7
|
+
const diff = date.getTime() - now.getTime();
|
8
|
+
|
9
|
+
if (diff <= 0) {
|
10
|
+
// Hedef tarih zaten geçmişse hiç bekleme
|
11
|
+
return Promise.resolve();
|
12
|
+
}
|
13
|
+
|
14
|
+
return new Promise((resolve) => setTimeout(resolve, diff));
|
15
|
+
},
|
16
|
+
};
|
17
|
+
|
package/src/modules/index.ts
CHANGED
@@ -0,0 +1,94 @@
|
|
1
|
+
'use server';
|
2
|
+
|
3
|
+
|
4
|
+
import { actionClient } from '../../actions/actionClient';
|
5
|
+
import { getSDKForCurrentUser } from '../../getSDKForCurrentUser';
|
6
|
+
import { CreateScheduledJobSchema, GetScheduledJobSchema, ListAvgExecutionsSchema, ListExecutionsSchema, ListScheduledJobsSchema, UpdateJobSchema } from './schema';
|
7
|
+
import { AvgExecution, Execution, ScheduledJob } from './types';
|
8
|
+
|
9
|
+
export const CreateScheduledJob = actionClient
|
10
|
+
.schema(CreateScheduledJobSchema)
|
11
|
+
.action(async ({ parsedInput }): Promise<ScheduledJob> => {
|
12
|
+
try {
|
13
|
+
|
14
|
+
const { scheduledJob } = await getSDKForCurrentUser();
|
15
|
+
return await scheduledJob.CreateScheduledJob(parsedInput);
|
16
|
+
|
17
|
+
} catch (error) {
|
18
|
+
console.error('Error in CreateScheduledJob:', error);
|
19
|
+
throw new Error('Failed to fetch CreateScheduledJob');
|
20
|
+
}
|
21
|
+
});
|
22
|
+
|
23
|
+
export const ListScheduledJobs = actionClient
|
24
|
+
.schema(ListScheduledJobsSchema)
|
25
|
+
.action(async ({ parsedInput }): Promise<ScheduledJob[]> => {
|
26
|
+
try {
|
27
|
+
|
28
|
+
const { scheduledJob } = await getSDKForCurrentUser();
|
29
|
+
return await scheduledJob.ListScheduledJobs(parsedInput);
|
30
|
+
|
31
|
+
} catch (error) {
|
32
|
+
console.error('Error in ListScheduledJobs:', error);
|
33
|
+
throw new Error('Failed to fetch ListScheduledJobs');
|
34
|
+
}
|
35
|
+
});
|
36
|
+
|
37
|
+
export const GetScheduledJob = actionClient
|
38
|
+
.schema(GetScheduledJobSchema)
|
39
|
+
.action(async ({ parsedInput }): Promise<ScheduledJob> => {
|
40
|
+
try {
|
41
|
+
|
42
|
+
const { scheduledJob } = await getSDKForCurrentUser();
|
43
|
+
return await scheduledJob.GetScheduledJob(parsedInput);
|
44
|
+
|
45
|
+
} catch (error) {
|
46
|
+
console.error('Error in GetScheduledJob:', error);
|
47
|
+
throw new Error('Failed to fetch GetScheduledJob');
|
48
|
+
}
|
49
|
+
});
|
50
|
+
|
51
|
+
export const ListExecutions = actionClient
|
52
|
+
.schema(ListExecutionsSchema)
|
53
|
+
.action(async ({ parsedInput }): Promise<Execution[]> => {
|
54
|
+
try {
|
55
|
+
|
56
|
+
const { scheduledJob } = await getSDKForCurrentUser();
|
57
|
+
return await scheduledJob.ListExecutions(parsedInput);
|
58
|
+
|
59
|
+
} catch (error) {
|
60
|
+
console.error('Error in ListExecutions:', error);
|
61
|
+
throw new Error('Failed to fetch ListExecutions');
|
62
|
+
}
|
63
|
+
});
|
64
|
+
|
65
|
+
export const ListAvgExecutions = actionClient
|
66
|
+
.schema(ListAvgExecutionsSchema)
|
67
|
+
.action(async ({ parsedInput }): Promise<AvgExecution[]> => {
|
68
|
+
try {
|
69
|
+
|
70
|
+
const { scheduledJob } = await getSDKForCurrentUser();
|
71
|
+
return await scheduledJob.ListAvgExecutions(parsedInput);
|
72
|
+
|
73
|
+
} catch (error) {
|
74
|
+
console.error('Error in ListAvgExecutions:', error);
|
75
|
+
throw new Error('Failed to fetch ListAvgExecutions');
|
76
|
+
}
|
77
|
+
});
|
78
|
+
|
79
|
+
export const UpdateJob = actionClient
|
80
|
+
.schema(UpdateJobSchema)
|
81
|
+
.action(async ({ parsedInput }): Promise<ScheduledJob> => {
|
82
|
+
try {
|
83
|
+
|
84
|
+
const { scheduledJob } = await getSDKForCurrentUser();
|
85
|
+
return await scheduledJob.UpdateJob(parsedInput);
|
86
|
+
|
87
|
+
} catch (error) {
|
88
|
+
console.error('Error in UpdateJob:', error);
|
89
|
+
throw new Error('Failed to fetch UpdateJob');
|
90
|
+
}
|
91
|
+
});
|
92
|
+
|
93
|
+
|
94
|
+
|
@@ -0,0 +1,103 @@
|
|
1
|
+
|
2
|
+
type TaskPayload = {
|
3
|
+
timestamp: Date;
|
4
|
+
lastTimestamp?: Date;
|
5
|
+
timezone: string;
|
6
|
+
};
|
7
|
+
|
8
|
+
type TaskContext = {
|
9
|
+
retriesLeft: number;
|
10
|
+
};
|
11
|
+
|
12
|
+
type TaskArgs = {
|
13
|
+
id: string;
|
14
|
+
cron: string;
|
15
|
+
maxDuration?: number; // saniye
|
16
|
+
retries?: number;
|
17
|
+
run: (
|
18
|
+
payload: TaskPayload,
|
19
|
+
tools: { ctx: TaskContext }
|
20
|
+
) => Promise<void> | void;
|
21
|
+
};
|
22
|
+
|
23
|
+
export class Schedules {
|
24
|
+
private lastRunTimestamps: Record<string, Date> = {};
|
25
|
+
private Tasks: TaskArgs[] = [];
|
26
|
+
|
27
|
+
public task(args: TaskArgs) {
|
28
|
+
this.Tasks.push(args);
|
29
|
+
}
|
30
|
+
|
31
|
+
public async run(taskId: string) {
|
32
|
+
const now = new Date();
|
33
|
+
const lastTimestamp = this.lastRunTimestamps[taskId];
|
34
|
+
|
35
|
+
const payload: TaskPayload = {
|
36
|
+
timestamp: now,
|
37
|
+
lastTimestamp,
|
38
|
+
timezone: "UTC",
|
39
|
+
};
|
40
|
+
|
41
|
+
this.lastRunTimestamps[taskId] = now;
|
42
|
+
|
43
|
+
const context: TaskContext = {
|
44
|
+
retriesLeft: this.Tasks.find(task => task.id === taskId)?.retries ?? 0,
|
45
|
+
};
|
46
|
+
|
47
|
+
await this.runWithRetry(this.Tasks.find(task => task.id === taskId)!, payload, context);
|
48
|
+
|
49
|
+
|
50
|
+
}
|
51
|
+
|
52
|
+
private async runWithRetry(
|
53
|
+
task: TaskArgs,
|
54
|
+
payload: TaskPayload,
|
55
|
+
ctx: TaskContext
|
56
|
+
) {
|
57
|
+
const start = Date.now();
|
58
|
+
|
59
|
+
try {
|
60
|
+
await Promise.race([
|
61
|
+
task.run(payload, { ctx }),
|
62
|
+
timeout(task.maxDuration ?? 60),
|
63
|
+
]);
|
64
|
+
log("success", { taskId: task.id, time: new Date().toISOString() });
|
65
|
+
} catch (err) {
|
66
|
+
//@ts-ignore
|
67
|
+
log("error", { taskId: task.id, error: err });
|
68
|
+
|
69
|
+
if (ctx.retriesLeft > 0) {
|
70
|
+
ctx.retriesLeft -= 1;
|
71
|
+
//@ts-ignore
|
72
|
+
log("warn", { taskId: task.id, msg: "Retrying..." });
|
73
|
+
await this.runWithRetry(task, payload, ctx);
|
74
|
+
}
|
75
|
+
} finally {
|
76
|
+
const duration = Date.now() - start;
|
77
|
+
//@ts-ignore
|
78
|
+
log("info", { taskId: task.id, durationMs: duration });
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
public toJSON() {
|
83
|
+
return this.Tasks.map(task => ({
|
84
|
+
id: task.id,
|
85
|
+
cron: task.cron,
|
86
|
+
maxDuration: task.maxDuration,
|
87
|
+
retries: task.retries,
|
88
|
+
}));
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
function log(arg0: string, arg1: { taskId: string; time: string; }) {
|
93
|
+
// console.log(arg0, arg1);
|
94
|
+
}
|
95
|
+
|
96
|
+
function timeout(seconds: number) {
|
97
|
+
return new Promise((_, reject) =>
|
98
|
+
setTimeout(() => reject(new Error("Timeout reached")), seconds * 1000)
|
99
|
+
);
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
export const schedules = new Schedules();
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
2
|
+
|
3
|
+
import './jobs';
|
4
|
+
import { schedules } from './Schedules';
|
5
|
+
|
6
|
+
/* export async function GET() {
|
7
|
+
return NextResponse.json({ schedules: schedules.toJSON() },
|
8
|
+
{ status: 200 });
|
9
|
+
}
|
10
|
+
|
11
|
+
export async function POST(request: Request) {
|
12
|
+
|
13
|
+
} */
|
14
|
+
|
15
|
+
export async function scheduledJobHandler(request: NextRequest, res: NextResponse) {
|
16
|
+
if (request.method === 'POST') {
|
17
|
+
const body = await request.json();
|
18
|
+
const taskId = body.taskId;
|
19
|
+
schedules.run(taskId);
|
20
|
+
return NextResponse.json({ message: 'Callback triggered' },
|
21
|
+
{ status: 200 });
|
22
|
+
} else {
|
23
|
+
return NextResponse.json({ schedules: schedules.toJSON() },
|
24
|
+
{ status: 200 });
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { schedules } from "../Schedules";
|
2
|
+
|
3
|
+
schedules.task({
|
4
|
+
id: "job1",
|
5
|
+
cron: "*/5 * * * *",
|
6
|
+
run: async () => {
|
7
|
+
console.log("hello from job 1");
|
8
|
+
},
|
9
|
+
});
|
10
|
+
|
11
|
+
|
12
|
+
schedules.task({
|
13
|
+
id: "job2",
|
14
|
+
cron: "*/5 * * * *",
|
15
|
+
run: async () => {
|
16
|
+
console.log("hello from job 2");
|
17
|
+
},
|
18
|
+
});
|
19
|
+
|
20
|
+
schedules.task({
|
21
|
+
id: "job3",
|
22
|
+
cron: "*/5 * * * *",
|
23
|
+
run: async () => {
|
24
|
+
console.log("hello from job 3");
|
25
|
+
},
|
26
|
+
});
|
27
|
+
|
28
|
+
schedules.task({
|
29
|
+
id: "job4",
|
30
|
+
cron: "*/5 * * * *",
|
31
|
+
run: async () => {
|
32
|
+
console.log("hello from job 4");
|
33
|
+
},
|
34
|
+
});
|
@@ -0,0 +1,17 @@
|
|
1
|
+
export const wait = {
|
2
|
+
for: ({ seconds }: { seconds: number }) =>
|
3
|
+
new Promise((resolve) => setTimeout(resolve, seconds * 1000)),
|
4
|
+
|
5
|
+
until: ({ date }: { date: Date }) => {
|
6
|
+
const now = new Date();
|
7
|
+
const diff = date.getTime() - now.getTime();
|
8
|
+
|
9
|
+
if (diff <= 0) {
|
10
|
+
// Hedef tarih zaten geçmişse hiç bekleme
|
11
|
+
return Promise.resolve();
|
12
|
+
}
|
13
|
+
|
14
|
+
return new Promise((resolve) => setTimeout(resolve, diff));
|
15
|
+
},
|
16
|
+
};
|
17
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
|
3
|
+
export const CreateScheduledJobSchema = z.object({
|
4
|
+
id: z.string().optional(),
|
5
|
+
name: z.string(),
|
6
|
+
cron: z.string(),
|
7
|
+
enabled: z.boolean().optional().default(true),
|
8
|
+
});
|
9
|
+
|
10
|
+
export const ListScheduledJobsSchema = z.object({
|
11
|
+
|
12
|
+
});
|
13
|
+
|
14
|
+
export const GetScheduledJobSchema = z.object({
|
15
|
+
id: z.string()
|
16
|
+
});
|
17
|
+
|
18
|
+
export const ListExecutionsSchema = z.object({
|
19
|
+
scheduledJobId: z.string(),
|
20
|
+
status: z.string().optional()
|
21
|
+
});
|
22
|
+
|
23
|
+
export const ListAvgExecutionsSchema = z.object({
|
24
|
+
});
|
25
|
+
|
26
|
+
export const UpdateJobSchema = z.object({
|
27
|
+
id: z.string(),
|
28
|
+
name: z.string().optional(),
|
29
|
+
cron: z.string().optional(),
|
30
|
+
enabled: z.boolean().optional(),
|
31
|
+
});
|
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
import z from "zod";
|
3
|
+
import { ServiceClient } from "../../service-client";
|
4
|
+
import { CreateScheduledJobSchema, ListScheduledJobsSchema, GetScheduledJobSchema, ListExecutionsSchema, ListAvgExecutionsSchema, UpdateJobSchema } from "./schema";
|
5
|
+
import { AvgExecution, Execution, ScheduledJob } from "./types";
|
6
|
+
|
7
|
+
export class ScheduledJobService extends ServiceClient {
|
8
|
+
protected getServiceName(): string {
|
9
|
+
return 'com.appconda.service.scheduled-jobs';
|
10
|
+
}
|
11
|
+
|
12
|
+
public async CreateScheduledJob(payload: z.infer<typeof CreateScheduledJobSchema>): Promise<ScheduledJob> {
|
13
|
+
return await this.actionCall('CreateScheduledJob', payload);
|
14
|
+
}
|
15
|
+
|
16
|
+
public async ListScheduledJobs(payload: z.infer<typeof ListScheduledJobsSchema>): Promise<ScheduledJob[]> {
|
17
|
+
return await this.actionCall('ListScheduledJobs', payload);
|
18
|
+
}
|
19
|
+
|
20
|
+
public async GetScheduledJob(payload: z.infer<typeof GetScheduledJobSchema>): Promise<ScheduledJob> {
|
21
|
+
return await this.actionCall('GetScheduledJob', payload);
|
22
|
+
}
|
23
|
+
|
24
|
+
public async ListExecutions(payload: z.infer<typeof ListExecutionsSchema>): Promise<Execution[]> {
|
25
|
+
return await this.actionCall('ListExecutions', payload);
|
26
|
+
}
|
27
|
+
|
28
|
+
public async ListAvgExecutions(payload: z.infer<typeof ListAvgExecutionsSchema>): Promise<AvgExecution[]> {
|
29
|
+
return await this.actionCall('ListAvgExecutions', payload);
|
30
|
+
}
|
31
|
+
|
32
|
+
public async UpdateJob(payload: z.infer<typeof UpdateJobSchema>): Promise<ScheduledJob> {
|
33
|
+
return await this.actionCall('UpdateJob', payload);
|
34
|
+
}
|
35
|
+
|
36
|
+
|
37
|
+
}
|