Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code] Index job timeout should show repository index error #33140

Merged
merged 3 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions x-pack/plugins/code/public/reducers/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,36 @@ export const status = handleActions(
}),
[String(updateCloneProgress)]: (state: StatusState, action: any) =>
produce<StatusState>(state, draft => {
const progress = action.payload.progress;
let s = RepoState.CLONING;
if (
progress === WorkerReservedProgress.ERROR ||
progress === WorkerReservedProgress.TIMEOUT
) {
s = RepoState.CLONE_ERROR;
} else if (progress === WorkerReservedProgress.COMPLETED) {
s = RepoState.READY;
}
draft.status[action.payload.repoUri] = {
...action.payload,
state: RepoState.CLONING,
state: s,
};
}),
[String(updateIndexProgress)]: (state: StatusState, action: any) =>
produce<StatusState>(state, draft => {
const progress = action.payload.progress;
let s = RepoState.INDEXING;
if (
progress === WorkerReservedProgress.ERROR ||
progress === WorkerReservedProgress.TIMEOUT
) {
s = RepoState.INDEX_ERROR;
} else if (progress === WorkerReservedProgress.COMPLETED) {
s = RepoState.READY;
}
draft.status[action.payload.repoUri] = {
...action.payload,
state: progress < WorkerReservedProgress.COMPLETED ? RepoState.INDEXING : RepoState.READY,
state: s,
};
}),
[String(updateDeleteProgress)]: (state: StatusState, action: any) =>
Expand All @@ -145,9 +164,17 @@ export const status = handleActions(
if (progress === WorkerReservedProgress.COMPLETED) {
delete draft.status[action.payload.repoUri];
} else {
let s = RepoState.DELETING;
if (
progress === WorkerReservedProgress.ERROR ||
progress === WorkerReservedProgress.TIMEOUT
) {
s = RepoState.DELETE_ERROR;
}

draft.status[action.payload.repoUri] = {
...action.payload,
state: RepoState.DELETING,
state: s,
};
}
}),
Expand Down
16 changes: 14 additions & 2 deletions x-pack/plugins/code/server/__tests__/clone_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,27 @@ describe('clone_worker_tests', () => {
(repoServiceFactory as any) as RepositoryServiceFactory
);

const result = await cloneWorker.executeJob({
const result1 = await cloneWorker.executeJob({
payload: {
url: 'file:///foo/bar.git',
},
options: {},
timestamp: 0,
});

assert.ok(result.repo === null);
assert.ok(result1.repo === null);
assert.ok(newInstanceSpy.notCalled);
assert.ok(cloneSpy.notCalled);

const result2 = await cloneWorker.executeJob({
payload: {
url: '/foo/bar.git',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
options: {},
timestamp: 0,
});

assert.ok(result2.repo === null);
assert.ok(newInstanceSpy.notCalled);
assert.ok(cloneSpy.notCalled);
});
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/code/server/indexer/abstract_indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ export abstract class AbstractIndexer implements Indexer {
failCount += 1;
}

if (progressReporter) {
// Double check if the the indexer is cancelled or not, because the
// processRequest process could take fairly long and during this time
// the index job might have been cancelled already. In this case,
// we shall not update the progress.
if (!this.isCancelled() && progressReporter) {
this.log.debug(`Update progress for ${this.type} indexer.`);
// Update progress if progress reporter has been provided.
const progress: IndexProgress = {
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/code/server/queue/abstract_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export abstract class AbstractWorker implements Worker {
// Assemble jobs, for now most of the job object construction should be the same.
public createJob(payload: any, options: any): Job {
const timestamp = moment().valueOf();
if (options.timeout !== undefined || options.timeout !== null) {
if (options.timeout !== undefined && options.timeout !== null) {
// If the job explicitly specify the timeout, then honor it.
return {
payload,
options,
timestamp,
};
} else {
// Otherwise, use a default timeout.
return {
payload,
options: {
Expand All @@ -55,7 +57,7 @@ export abstract class AbstractWorker implements Worker {
public async enqueueJob(payload: any, options: any) {
const job: Job = this.createJob(payload, options);
return new Promise((resolve, reject) => {
const jobInternal: JobInternal<Job> = this.queue.addJob(this.id, job, {});
const jobInternal: JobInternal<Job> = this.queue.addJob(this.id, job, job.options);
jobInternal.on(esqueueEvents.EVENT_JOB_CREATED, async (createdJob: JobInternal<Job>) => {
if (createdJob.id === jobInternal.id) {
await this.onJobEnqueued(job);
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/code/server/queue/index_worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import sinon from 'sinon';

import { WorkerReservedProgress } from '../../model';
import { IndexerFactory } from '../indexer';
import { AnyObject, CancellationToken, EsClient, Esqueue } from '../lib/esqueue';
import { Logger } from '../log';
Expand Down Expand Up @@ -187,8 +186,6 @@ test('On index job completed.', async () => {
},
{
uri: 'github.com/elastic/kibana',
progress: WorkerReservedProgress.COMPLETED,
timestamp: new Date(),
}
);

Expand Down
25 changes: 12 additions & 13 deletions x-pack/plugins/code/server/queue/index_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

import moment from 'moment';

import {
IndexStats,
IndexWorkerResult,
RepositoryUri,
WorkerProgress,
WorkerReservedProgress,
} from '../../model';
import { IndexStats, IndexWorkerResult, RepositoryUri, WorkerProgress } from '../../model';
import { IndexerFactory, IndexProgress } from '../indexer';
import { EsClient, Esqueue } from '../lib/esqueue';
import { Logger } from '../log';
Expand Down Expand Up @@ -79,13 +73,18 @@ export class IndexWorker extends AbstractWorker {
return await this.objectClient.setRepositoryLspIndexStatus(uri, progress);
}

public async onJobCompleted(job: Job, res: WorkerProgress) {
const { uri } = job.payload;
await this.objectClient.updateRepositoryLspIndexStatus(uri, {
progress: WorkerReservedProgress.COMPLETED,
public async updateProgress(uri: RepositoryUri, progress: number) {
const p: WorkerProgress = {
uri,
progress,
timestamp: new Date(),
});
return await super.onJobCompleted(job, res);
};
try {
return await this.objectClient.updateRepositoryLspIndexStatus(uri, p);
} catch (error) {
this.log.error(`Update index progress error.`);
this.log.error(error);
}
}

protected getTimeoutMs(_: any) {
Expand Down