Skip to content

Commit ffd638e

Browse files
committed
Implement the rest of the jobs utility methods.
Signed-off-by: Alexander Trauzzi <[email protected]>
1 parent 3905a4f commit ffd638e

File tree

4 files changed

+97
-53
lines changed

4 files changed

+97
-53
lines changed

src/implementation/Client/HTTPClient/jobs.ts

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

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

1920
export default class HTTPClientJobs implements IClientJobs {
2021

21-
private readonly logger: Logger;
22-
private readonly httpClient: HTTPClient;
23-
24-
constructor(httpClient: HTTPClient) {
25-
this.logger = new Logger("HTTPClient", "Jobs", httpClient.options.logger);
26-
this.httpClient = httpClient;
27-
}
22+
constructor(private readonly httpClient: HTTPClient) {}
2823

2924
async schedule(
3025
jobName: string,
@@ -35,35 +30,47 @@ export default class HTTPClientJobs implements IClientJobs {
3530
ttl: string | null = null
3631
): Promise<void> {
3732

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-
}
33+
await this.httpClient.executeWithApiVersion(
34+
"v1.0-alpha1",
35+
`/jobs/${jobName}`,
36+
{
37+
method: "POST",
38+
body: {
39+
data,
40+
schedule,
41+
dueTime,
42+
repeats,
43+
ttl,
44+
},
45+
headers: {
46+
"content-type": "application/json",
47+
},
48+
} as THTTPExecuteParams
49+
);
6050
}
6151

62-
get(): Promise<unknown> {
63-
throw new Error("Not yet!");
52+
async get(jobName: string): Promise<Job>;
53+
async get<DataType>(jobName: string): Promise<Job<DataType>> {
54+
55+
const result = await this.httpClient.executeWithApiVersion(
56+
"v1.0-alpha1",
57+
`/jobs/${jobName}`,
58+
{
59+
method: "GET",
60+
}
61+
);
62+
63+
return result as Job<DataType>;
6464
}
6565

66-
delete(): Promise<unknown> {
67-
throw new Error("Not yet!");
66+
async delete(jobName: string): Promise<void> {
67+
68+
await this.httpClient.executeWithApiVersion(
69+
"v1.0-alpha1",
70+
`/jobs/${jobName}`,
71+
{
72+
method: "DELETE"
73+
}
74+
);
6875
}
6976
}

src/interfaces/Client/IClientJobs.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,8 @@ 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;
14+
import { JobSchedule } from "../../types/jobs/JobSchedule.type";
15+
import { Job } from "../../types/jobs/Job.type";
3016

3117
export default interface IClientJobs {
3218

@@ -38,6 +24,6 @@ export default interface IClientJobs {
3824
repeats: number | null,
3925
ttl: string | null
4026
): Promise<void>;
41-
get(): Promise<unknown>;
42-
delete(): Promise<unknown>;
27+
get(jobName: string): Promise<Job>;
28+
delete(jobName: string): Promise<void>;
4329
}

src/types/jobs/Job.type.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
import { JobSchedule } from "./JobSchedule.type";
15+
16+
export interface Job<DataType = object | string> {
17+
name: string;
18+
schedule: JobSchedule;
19+
data: {
20+
value: DataType,
21+
};
22+
}

src/types/jobs/JobSchedule.type.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 enum FixedPeriod {
15+
Yearly = "@yearly",
16+
Monthly = "@monthly",
17+
Weekly = "@weekly",
18+
Daily = "@daily",
19+
Hourly = "@hourly"
20+
}
21+
22+
type EveryPeriod = `@every ${string}`;
23+
24+
// note: This can get crazy, more than TS can really handle.
25+
type SystemDCronExpression = `${string} ${string} ${string} ${string} ${string} ${string}`;
26+
27+
type Schedule = FixedPeriod | EveryPeriod | SystemDCronExpression;
28+
29+
export type JobSchedule = Schedule;

0 commit comments

Comments
 (0)