Skip to content

Commit 3905a4f

Browse files
committed
Implement registration.
Signed-off-by: Alexander Trauzzi <[email protected]>
1 parent f878330 commit 3905a4f

File tree

5 files changed

+83
-10
lines changed

5 files changed

+83
-10
lines changed

src/implementation/Client/HTTPClient/jobs.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,58 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import IClientJobs from "../../../interfaces/Client/IClientJobs";
14+
import IClientJobs, { JobSchedule } from "../../../interfaces/Client/IClientJobs";
1515
import HTTPClient from "./HTTPClient";
16+
import { Logger } from "../../../logger/Logger";
17+
import { THTTPExecuteParams } from "../../../types/http/THTTPExecuteParams.type";
1618

1719
export default class HTTPClientJobs implements IClientJobs {
1820

21+
private readonly logger: Logger;
1922
private readonly httpClient: HTTPClient;
2023

2124
constructor(httpClient: HTTPClient) {
25+
this.logger = new Logger("HTTPClient", "Jobs", httpClient.options.logger);
2226
this.httpClient = httpClient;
2327
}
2428

25-
schedule(): Promise<unknown> {
26-
throw new Error("Not yet!");
29+
async schedule(
30+
jobName: string,
31+
data: object | string,
32+
schedule: JobSchedule | null = null,
33+
dueTime: string | Date | null = null,
34+
repeats: number | null = null,
35+
ttl: string | null = null
36+
): Promise<void> {
37+
38+
try {
39+
await this.httpClient.executeWithApiVersion(
40+
"v1.0-alpha1",
41+
`/jobs/${jobName}`,
42+
{
43+
method: "POST",
44+
body: {
45+
data,
46+
schedule,
47+
dueTime,
48+
repeats,
49+
ttl,
50+
},
51+
headers: {
52+
"content-type": "application/json",
53+
},
54+
} as THTTPExecuteParams
55+
);
56+
}
57+
catch (e: any) {
58+
this.logger.error(e);
59+
}
2760
}
61+
2862
get(): Promise<unknown> {
2963
throw new Error("Not yet!");
3064
}
65+
3166
delete(): Promise<unknown> {
3267
throw new Error("Not yet!");
3368
}

src/implementation/Server/HTTPServer/jobs.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
import IServerJobs from "../../../interfaces/Server/IServerJobs";
1515
import HTTPServer from "./HTTPServer";
1616
import { Logger } from "../../../logger/Logger";
17+
import { TypeDaprJobsCallback } from "../../../types/DaprJobsCallback.type";
1718

1819
export default class HTTPServerJobs implements IServerJobs {
1920

@@ -25,9 +26,7 @@ export default class HTTPServerJobs implements IServerJobs {
2526
this.httpServer = httpServer;
2627
}
2728

28-
listen(): Promise<unknown> {
29-
30-
// this.httpServer.server.post();
31-
29+
listen(jobName: string, callback: TypeDaprJobsCallback): void {
30+
this.httpServer.getServer().post(`/job/${jobName}`, callback);
3231
}
3332
}

src/interfaces/Client/IClientJobs.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,33 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14+
export enum FixedPeriod {
15+
Yearly = "@yearly",
16+
Monthly = "@monthly",
17+
Weekly = "@weekly",
18+
Daily = "@daily",
19+
Hourly = "@hourly"
20+
}
21+
22+
// note: This can get pretty crazy, more than TS can really handle.
23+
type SystemDCronExpression = `${string} ${string} ${string} ${string} ${string} ${string}`;
24+
25+
type EveryPeriod = `@every ${string}`;
26+
27+
type Schedule = FixedPeriod | EveryPeriod | SystemDCronExpression;
28+
29+
export type JobSchedule = Schedule;
30+
1431
export default interface IClientJobs {
1532

16-
schedule(): Promise<unknown>;
33+
schedule(
34+
jobName: string,
35+
data: object | string,
36+
schedule: JobSchedule | null,
37+
dueTime: string | Date | null,
38+
repeats: number | null,
39+
ttl: string | null
40+
): Promise<void>;
1741
get(): Promise<unknown>;
1842
delete(): Promise<unknown>;
19-
2043
}

src/interfaces/Server/IServerJobs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14+
import { TypeDaprJobsCallback } from "../../types/DaprJobsCallback.type";
15+
1416
export default interface IServerJobs {
1517

16-
listen(): Promise<unknown>;
18+
listen(jobName: string, callback: TypeDaprJobsCallback): void;
1719

1820
}

src/types/DaprJobsCallback.type.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Copyright 2025 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
export type TypeDaprJobsCallback = (data: any, headers: object) => Promise<any | void>;

0 commit comments

Comments
 (0)