Skip to content

Commit

Permalink
feat(core): Add optional metadata to JobInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Sep 2, 2019
1 parent 7df2b9c commit 7ddec36
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 12 deletions.
1 change: 1 addition & 0 deletions admin-ui/src/app/common/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,7 @@ export type JobInfo = {
name: Scalars['String'],
state: JobState,
progress: Scalars['Float'],
metadata?: Maybe<Scalars['JSON']>,
result?: Maybe<Scalars['JSON']>,
started?: Maybe<Scalars['DateTime']>,
ended?: Maybe<Scalars['DateTime']>,
Expand Down
22 changes: 22 additions & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,17 @@ export type PriceRange = {
max: Scalars['Int'];
};

export type PriceRangeBucket = {
__typename?: 'PriceRangeBucket';
to: Scalars['Int'];
count: Scalars['Int'];
};

export type PriceRangeInput = {
min: Scalars['Int'];
max: Scalars['Int'];
};

export type Product = Node & {
__typename?: 'Product';
id: Scalars['ID'];
Expand Down Expand Up @@ -1962,6 +1973,8 @@ export type SearchInput = {
take?: Maybe<Scalars['Int']>;
skip?: Maybe<Scalars['Int']>;
sort?: Maybe<SearchResultSortParameter>;
priceRange?: Maybe<PriceRangeInput>;
priceRangeWithTax?: Maybe<PriceRangeInput>;
};

export type SearchReindexResponse = {
Expand All @@ -1974,6 +1987,15 @@ export type SearchResponse = {
items: Array<SearchResult>;
totalItems: Scalars['Int'];
facetValues: Array<FacetValueResult>;
prices: SearchResponsePriceData;
};

export type SearchResponsePriceData = {
__typename?: 'SearchResponsePriceData';
range: PriceRange;
rangeWithTax: PriceRange;
buckets: Array<PriceRangeBucket>;
bucketsWithTax: Array<PriceRangeBucket>;
};

export type SearchResult = {
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ export type JobInfo = {
name: Scalars['String'],
state: JobState,
progress: Scalars['Float'],
metadata?: Maybe<Scalars['JSON']>,
result?: Maybe<Scalars['JSON']>,
started?: Maybe<Scalars['DateTime']>,
ended?: Maybe<Scalars['DateTime']>,
Expand Down
21 changes: 17 additions & 4 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,7 @@ export type JobInfo = {
name: Scalars['String'];
state: JobState;
progress: Scalars['Float'];
metadata?: Maybe<Scalars['JSON']>;
result?: Maybe<Scalars['JSON']>;
started?: Maybe<Scalars['DateTime']>;
ended?: Maybe<Scalars['DateTime']>;
Expand Down Expand Up @@ -4277,8 +4278,12 @@ export type GetProductsQuery = { __typename?: 'Query' } & {
products: { __typename?: 'ProductList' } & Pick<ProductList, 'totalItems'> & {
items: Array<
{ __typename?: 'Product' } & Pick<Product, 'id' | 'name' | 'slug' | 'description'> & {
featuredAsset: Maybe<{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>>;
assets: Array<{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>>;
featuredAsset: Maybe<
{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name' | 'preview' | 'source'>
>;
assets: Array<
{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name' | 'preview' | 'source'>
>;
optionGroups: Array<
{ __typename?: 'ProductOptionGroup' } & Pick<
ProductOptionGroup,
Expand All @@ -4302,9 +4307,17 @@ export type GetProductsQuery = { __typename?: 'Query' } & {
options: Array<
{ __typename?: 'ProductOption' } & Pick<ProductOption, 'id' | 'code'>
>;
assets: Array<{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>>;
assets: Array<
{ __typename?: 'Asset' } & Pick<
Asset,
'id' | 'name' | 'preview' | 'source'
>
>;
featuredAsset: Maybe<
{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>
{ __typename?: 'Asset' } & Pick<
Asset,
'id' | 'name' | 'preview' | 'source'
>
>;
facetValues: Array<
{ __typename?: 'FacetValue' } & Pick<
Expand Down
22 changes: 22 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,17 @@ export type PriceRange = {
max: Scalars['Int'];
};

export type PriceRangeBucket = {
__typename?: 'PriceRangeBucket';
to: Scalars['Int'];
count: Scalars['Int'];
};

export type PriceRangeInput = {
min: Scalars['Int'];
max: Scalars['Int'];
};

export type Product = Node & {
__typename?: 'Product';
id: Scalars['ID'];
Expand Down Expand Up @@ -1962,6 +1973,8 @@ export type SearchInput = {
take?: Maybe<Scalars['Int']>;
skip?: Maybe<Scalars['Int']>;
sort?: Maybe<SearchResultSortParameter>;
priceRange?: Maybe<PriceRangeInput>;
priceRangeWithTax?: Maybe<PriceRangeInput>;
};

export type SearchReindexResponse = {
Expand All @@ -1974,6 +1987,15 @@ export type SearchResponse = {
items: Array<SearchResult>;
totalItems: Scalars['Int'];
facetValues: Array<FacetValueResult>;
prices: SearchResponsePriceData;
};

export type SearchResponsePriceData = {
__typename?: 'SearchResponsePriceData';
range: PriceRange;
rangeWithTax: PriceRange;
buckets: Array<PriceRangeBucket>;
bucketsWithTax: Array<PriceRangeBucket>;
};

export type SearchResult = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/api/schema/admin-api/job.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type JobInfo {
name: String!
state: JobState!
progress: Float!
metadata: JSON
result: JSON
started: DateTime
ended: DateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class SearchIndexService {
updateProductOrVariant(ctx: RequestContext, updatedEntity: Product | ProductVariant) {
return this.jobService.createJob({
name: 'update-index',
metadata: {
entity: updatedEntity.constructor.name,
id: updatedEntity.id,
},
work: reporter => {
const data =
updatedEntity instanceof Product
Expand All @@ -57,7 +61,10 @@ export class SearchIndexService {

updateVariantsById(ctx: RequestContext, ids: ID[]) {
return this.jobService.createJob({
name: 'update-index',
name: 'update-variants',
metadata: {
variantIds: ids,
},
work: reporter => {
Logger.verbose(`sending reindex message`);
this.workerService
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/service/helpers/job-manager/job-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ export class JobManager {
* property of the job. If the function throws, the job will fail and the `result` property
* will be the error thrown.
*/
createJob(name: string, work: () => Promise<any>, reporter: PartialJobReporter): Job {
const job = new Job(name, work, reporter);
createJob(
name: string,
work: () => Promise<any>,
reporter: PartialJobReporter,
metadata?: Record<string, any>,
): Job {
const job = new Job(name, work, reporter, metadata);
this.jobs.set(job.id, job);
return job;
}
Expand Down Expand Up @@ -84,7 +89,7 @@ export class JobManager {
}

private toJobInfo(job: Job): JobInfo {
const info = pick(job, ['id', 'name', 'state', 'progress', 'result', 'started', 'ended']);
const info = pick(job, ['id', 'name', 'state', 'progress', 'result', 'started', 'ended', 'metadata']);
const duration = job.ended ? +job.ended - +info.started : Date.now() - +info.started;
return { ...info, duration };
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/service/helpers/job-manager/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export class Job {
id: string;
state: JobState = JobState.PENDING;
progress = 0;
result = null;
result: any = null;
started: Date;
ended: Date;

constructor(
public name: string,
public work: () => any | Promise<any>,
private reporter: PartialJobReporter,
public metadata: Record<string, any> = {},
) {
this.id = generatePublicId();
this.started = new Date();
Expand All @@ -44,5 +45,6 @@ export class Job {
this.result = e;
}
this.ended = new Date();
return this.result;
}
}
3 changes: 2 additions & 1 deletion packages/core/src/service/services/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type PartialJobReporter = Omit<JobReporter, 'setProgress'> & Partial<Pick

export interface CreateJobOptions {
name: string;
metadata?: any;
work: (reporter: JobReporter) => any | Promise<any>;
/** Limit this job to a single instance at a time */
singleInstance?: boolean;
Expand Down Expand Up @@ -60,7 +61,7 @@ export class JobService implements OnModuleInit, OnModuleDestroy {
}
});
};
return this.manager.createJob(options.name, wrappedWork, reporter);
return this.manager.createJob(options.name, wrappedWork, reporter, options.metadata);
}

getAll(input?: JobListInput): JobInfo[] {
Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 7ddec36

Please sign in to comment.