Commit b1fae097 authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'winh-delayed-jobs-dynamic-timer' into 'master'

Add dynamic timer to delayed jobs

See merge request gitlab-org/gitlab-ce!22382
parents cb72c3ca a6701d46
...@@ -16,6 +16,8 @@ import Log from './job_log.vue'; ...@@ -16,6 +16,8 @@ import Log from './job_log.vue';
import LogTopBar from './job_log_controllers.vue'; import LogTopBar from './job_log_controllers.vue';
import StuckBlock from './stuck_block.vue'; import StuckBlock from './stuck_block.vue';
import Sidebar from './sidebar.vue'; import Sidebar from './sidebar.vue';
import { sprintf } from '~/locale';
import delayedJobMixin from '../mixins/delayed_job_mixin';
export default { export default {
name: 'JobPageApp', name: 'JobPageApp',
...@@ -33,6 +35,7 @@ export default { ...@@ -33,6 +35,7 @@ export default {
Sidebar, Sidebar,
GlLoadingIcon, GlLoadingIcon,
}, },
mixins: [delayedJobMixin],
props: { props: {
runnerSettingsUrl: { runnerSettingsUrl: {
type: String, type: String,
...@@ -92,6 +95,17 @@ export default { ...@@ -92,6 +95,17 @@ export default {
shouldRenderContent() { shouldRenderContent() {
return !this.isLoading && !this.hasError; return !this.isLoading && !this.hasError;
}, },
emptyStateTitle() {
const { emptyStateIllustration, remainingTime } = this;
const { title } = emptyStateIllustration;
if (this.isDelayedJob) {
return sprintf(title, { remainingTime });
}
return title;
},
}, },
watch: { watch: {
// Once the job log is loaded, // Once the job log is loaded,
...@@ -272,7 +286,7 @@ export default { ...@@ -272,7 +286,7 @@ export default {
class="js-job-empty-state" class="js-job-empty-state"
:illustration-path="emptyStateIllustration.image" :illustration-path="emptyStateIllustration.image"
:illustration-size-class="emptyStateIllustration.size" :illustration-size-class="emptyStateIllustration.size"
:title="emptyStateIllustration.title" :title="emptyStateTitle"
:content="emptyStateIllustration.content" :content="emptyStateIllustration.content"
:action="emptyStateAction" :action="emptyStateAction"
/> />
......
<script> <script>
import { GlTooltipDirective, GlLink } from '@gitlab-org/gitlab-ui'; import { GlLink } from '@gitlab-org/gitlab-ui';
import tooltip from '~/vue_shared/directives/tooltip';
import CiIcon from '~/vue_shared/components/ci_icon.vue'; import CiIcon from '~/vue_shared/components/ci_icon.vue';
import Icon from '~/vue_shared/components/icon.vue'; import Icon from '~/vue_shared/components/icon.vue';
import delayedJobMixin from '~/jobs/mixins/delayed_job_mixin';
import { sprintf } from '~/locale';
export default { export default {
components: { components: {
...@@ -10,8 +13,9 @@ export default { ...@@ -10,8 +13,9 @@ export default {
GlLink, GlLink,
}, },
directives: { directives: {
GlTooltip: GlTooltipDirective, tooltip,
}, },
mixins: [delayedJobMixin],
props: { props: {
job: { job: {
type: Object, type: Object,
...@@ -24,7 +28,14 @@ export default { ...@@ -24,7 +28,14 @@ export default {
}, },
computed: { computed: {
tooltipText() { tooltipText() {
return `${this.job.name} - ${this.job.status.tooltip}`; const { name, status } = this.job;
const text = `${name} - ${status.tooltip}`;
if (this.isDelayedJob) {
return sprintf(text, { remainingTime: this.remainingTime });
}
return text;
}, },
}, },
}; };
...@@ -39,7 +50,7 @@ export default { ...@@ -39,7 +50,7 @@ export default {
}" }"
> >
<gl-link <gl-link
v-gl-tooltip v-tooltip
:href="job.status.details_path" :href="job.status.details_path"
:title="tooltipText" :title="tooltipText"
data-boundary="viewport" data-boundary="viewport"
......
import { calculateRemainingMilliseconds, formatTime } from '~/lib/utils/datetime_utility';
export default {
data() {
return {
remainingTime: formatTime(0),
remainingTimeIntervalId: null,
};
},
mounted() {
this.startRemainingTimeInterval();
},
beforeDestroy() {
if (this.remainingTimeIntervalId) {
clearInterval(this.remainingTimeIntervalId);
}
},
computed: {
isDelayedJob() {
return this.job && this.job.scheduled;
},
},
watch: {
isDelayedJob() {
this.startRemainingTimeInterval();
},
},
methods: {
startRemainingTimeInterval() {
if (this.remainingTimeIntervalId) {
clearInterval(this.remainingTimeIntervalId);
}
if (this.isDelayedJob) {
this.updateRemainingTime();
this.remainingTimeIntervalId = setInterval(() => this.updateRemainingTime(), 1000);
}
},
updateRemainingTime() {
const remainingMilliseconds = calculateRemainingMilliseconds(this.job.scheduled_at);
this.remainingTime = formatTime(remainingMilliseconds);
},
},
};
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
import ActionComponent from './action_component.vue'; import ActionComponent from './action_component.vue';
import JobNameComponent from './job_name_component.vue'; import JobNameComponent from './job_name_component.vue';
import tooltip from '../../../vue_shared/directives/tooltip'; import tooltip from '../../../vue_shared/directives/tooltip';
import { sprintf } from '~/locale';
import delayedJobMixin from '~/jobs/mixins/delayed_job_mixin';
/** /**
* Renders the badge for the pipeline graph and the job's dropdown. * Renders the badge for the pipeline graph and the job's dropdown.
...@@ -36,6 +38,7 @@ export default { ...@@ -36,6 +38,7 @@ export default {
directives: { directives: {
tooltip, tooltip,
}, },
mixins: [delayedJobMixin],
props: { props: {
job: { job: {
type: Object, type: Object,
...@@ -52,6 +55,7 @@ export default { ...@@ -52,6 +55,7 @@ export default {
default: Infinity, default: Infinity,
}, },
}, },
computed: { computed: {
status() { status() {
return this.job && this.job.status ? this.job.status : {}; return this.job && this.job.status ? this.job.status : {};
...@@ -59,17 +63,23 @@ export default { ...@@ -59,17 +63,23 @@ export default {
tooltipText() { tooltipText() {
const textBuilder = []; const textBuilder = [];
const { name: jobName } = this.job;
if (this.job.name) { if (jobName) {
textBuilder.push(this.job.name); textBuilder.push(jobName);
} }
if (this.job.name && this.status.tooltip) { const { tooltip: statusTooltip } = this.status;
if (jobName && statusTooltip) {
textBuilder.push('-'); textBuilder.push('-');
} }
if (this.status.tooltip) { if (statusTooltip) {
textBuilder.push(this.job.status.tooltip); if (this.isDelayedJob) {
textBuilder.push(sprintf(statusTooltip, { remainingTime: this.remainingTime }));
} else {
textBuilder.push(statusTooltip);
}
} }
return textBuilder.join(' '); return textBuilder.join(' ');
...@@ -88,6 +98,7 @@ export default { ...@@ -88,6 +98,7 @@ export default {
return this.job.status && this.job.status.action && this.job.status.action.path; return this.job.status && this.job.status.action && this.job.status.action.path;
}, },
}, },
methods: { methods: {
pipelineActionRequestComplete() { pipelineActionRequestComplete() {
this.$emit('pipelineActionRequestComplete'); this.$emit('pipelineActionRequestComplete');
......
---
title: Add dynamic timer to delayed jobs
merge_request: 22382
author:
type: changed
...@@ -9,7 +9,7 @@ module Gitlab ...@@ -9,7 +9,7 @@ module Gitlab
{ {
image: 'illustrations/illustrations_scheduled-job_countdown.svg', image: 'illustrations/illustrations_scheduled-job_countdown.svg',
size: 'svg-394', size: 'svg-394',
title: _("This is a delayed to run in ") + " #{execute_in}", title: _("This is a delayed job to run in %{remainingTime}"),
content: _("This job will automatically run after it's timer finishes. " \ content: _("This job will automatically run after it's timer finishes. " \
"Often they are used for incremental roll-out deploys " \ "Often they are used for incremental roll-out deploys " \
"to production environments. When unscheduled it converts " \ "to production environments. When unscheduled it converts " \
...@@ -18,21 +18,12 @@ module Gitlab ...@@ -18,21 +18,12 @@ module Gitlab
end end
def status_tooltip def status_tooltip
"delayed manual action (#{execute_in})" "delayed manual action (%{remainingTime})"
end end
def self.matches?(build, user) def self.matches?(build, user)
build.scheduled? && build.scheduled_at build.scheduled? && build.scheduled_at
end end
private
include TimeHelper
def execute_in
remaining_seconds = [0, subject.scheduled_at - Time.now].max
duration_in_numbers(remaining_seconds)
end
end end
end end
end end
......
...@@ -6197,7 +6197,7 @@ msgstr "" ...@@ -6197,7 +6197,7 @@ msgstr ""
msgid "This is a confidential issue." msgid "This is a confidential issue."
msgstr "" msgstr ""
msgid "This is a delayed to run in " msgid "This is a delayed job to run in %{remainingTime}"
msgstr "" msgstr ""
msgid "This is the author's first Merge Request to this project." msgid "This is the author's first Merge Request to this project."
......
...@@ -595,7 +595,7 @@ describe 'Jobs', :clean_gitlab_redis_shared_state do ...@@ -595,7 +595,7 @@ describe 'Jobs', :clean_gitlab_redis_shared_state do
end end
it 'shows delayed job', :js do it 'shows delayed job', :js do
expect(page).to have_content('This is a delayed to run in') expect(page).to have_content('This is a delayed job to run in')
expect(page).to have_content("This job will automatically run after it's timer finishes.") expect(page).to have_content("This job will automatically run after it's timer finishes.")
expect(page).to have_link('Unschedule job') expect(page).to have_link('Unschedule job')
end end
......
...@@ -5,16 +5,24 @@ describe Projects::JobsController, '(JavaScript fixtures)', type: :controller do ...@@ -5,16 +5,24 @@ describe Projects::JobsController, '(JavaScript fixtures)', type: :controller do
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let(:namespace) { create(:namespace, name: 'frontend-fixtures' )} let(:namespace) { create(:namespace, name: 'frontend-fixtures' )}
let(:project) { create(:project_empty_repo, namespace: namespace, path: 'builds-project') } let(:project) { create(:project, :repository, namespace: namespace, path: 'builds-project') }
let(:pipeline) { create(:ci_empty_pipeline, project: project) } let(:pipeline) { create(:ci_empty_pipeline, project: project, sha: project.commit.id) }
let!(:build_with_artifacts) { create(:ci_build, :success, :artifacts, :trace_artifact, pipeline: pipeline, stage: 'test', artifacts_expire_at: Time.now + 18.months) } let!(:build_with_artifacts) { create(:ci_build, :success, :artifacts, :trace_artifact, pipeline: pipeline, stage: 'test', artifacts_expire_at: Time.now + 18.months) }
let!(:failed_build) { create(:ci_build, :failed, pipeline: pipeline, stage: 'build') } let!(:failed_build) { create(:ci_build, :failed, pipeline: pipeline, stage: 'build') }
let!(:pending_build) { create(:ci_build, :pending, pipeline: pipeline, stage: 'deploy') } let!(:pending_build) { create(:ci_build, :pending, pipeline: pipeline, stage: 'deploy') }
let!(:delayed_job) do
create(:ci_build, :scheduled,
pipeline: pipeline,
name: 'delayed job',
stage: 'test',
commands: 'test')
end
render_views render_views
before(:all) do before(:all) do
clean_frontend_fixtures('builds/') clean_frontend_fixtures('builds/')
clean_frontend_fixtures('jobs/')
end end
before do before do
...@@ -34,4 +42,15 @@ describe Projects::JobsController, '(JavaScript fixtures)', type: :controller do ...@@ -34,4 +42,15 @@ describe Projects::JobsController, '(JavaScript fixtures)', type: :controller do
expect(response).to be_success expect(response).to be_success
store_frontend_fixture(response, example.description) store_frontend_fixture(response, example.description)
end end
it 'jobs/delayed.json' do |example|
get :show,
namespace_id: project.namespace.to_param,
project_id: project,
id: delayed_job.to_param,
format: :json
expect(response).to be_success
store_frontend_fixture(response, example.description)
end
end end
...@@ -8,6 +8,7 @@ import { resetStore } from '../store/helpers'; ...@@ -8,6 +8,7 @@ import { resetStore } from '../store/helpers';
import job from '../mock_data'; import job from '../mock_data';
describe('Job App ', () => { describe('Job App ', () => {
const delayedJobFixture = getJSONFixture('jobs/delayed.json');
const Component = Vue.extend(jobApp); const Component = Vue.extend(jobApp);
let store; let store;
let vm; let vm;
...@@ -420,6 +421,36 @@ describe('Job App ', () => { ...@@ -420,6 +421,36 @@ describe('Job App ', () => {
done(); done();
}, 0); }, 0);
}); });
it('displays remaining time for a delayed job', done => {
const oneHourInMilliseconds = 3600000;
spyOn(Date, 'now').and.callFake(
() => new Date(delayedJobFixture.scheduled_at).getTime() - oneHourInMilliseconds,
);
mock.onGet(props.endpoint).replyOnce(200, { ...delayedJobFixture });
vm = mountComponentWithStore(Component, {
props,
store,
});
store.subscribeAction(action => {
if (action.type !== 'receiveJobSuccess') {
return;
}
Vue.nextTick()
.then(() => {
expect(vm.$el.querySelector('.js-job-empty-state')).not.toBeNull();
const title = vm.$el.querySelector('.js-job-empty-state-title');
expect(title).toContainText('01:00:00');
done();
})
.catch(done.fail);
});
});
}); });
}); });
......
...@@ -4,6 +4,7 @@ import mountComponent from 'spec/helpers/vue_mount_component_helper'; ...@@ -4,6 +4,7 @@ import mountComponent from 'spec/helpers/vue_mount_component_helper';
import job from '../mock_data'; import job from '../mock_data';
describe('JobContainerItem', () => { describe('JobContainerItem', () => {
const delayedJobFixture = getJSONFixture('jobs/delayed.json');
const Component = Vue.extend(JobContainerItem); const Component = Vue.extend(JobContainerItem);
let vm; let vm;
...@@ -70,4 +71,29 @@ describe('JobContainerItem', () => { ...@@ -70,4 +71,29 @@ describe('JobContainerItem', () => {
expect(vm.$el).toHaveSpriteIcon('retry'); expect(vm.$el).toHaveSpriteIcon('retry');
}); });
}); });
describe('for delayed job', () => {
beforeEach(() => {
const remainingMilliseconds = 1337000;
spyOn(Date, 'now').and.callFake(
() => new Date(delayedJobFixture.scheduled_at).getTime() - remainingMilliseconds,
);
});
it('displays remaining time in tooltip', done => {
vm = mountComponent(Component, {
job: delayedJobFixture,
isActive: false,
});
Vue.nextTick()
.then(() => {
expect(vm.$el.querySelector('.js-job-link').getAttribute('data-original-title')).toEqual(
'delayed job - delayed manual action (00:22:17)',
);
})
.then(done)
.catch(done.fail);
});
});
}); });
import Vue from 'vue';
import delayedJobMixin from '~/jobs/mixins/delayed_job_mixin';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
describe('DelayedJobMixin', () => {
const delayedJobFixture = getJSONFixture('jobs/delayed.json');
const dummyComponent = Vue.extend({
mixins: [delayedJobMixin],
props: {
job: {
type: Object,
required: true,
},
},
template: '<div>{{ remainingTime }}</div>',
});
let vm;
beforeEach(() => {
jasmine.clock().install();
});
afterEach(() => {
vm.$destroy();
jasmine.clock().uninstall();
});
describe('if job is empty object', () => {
beforeEach(() => {
vm = mountComponent(dummyComponent, {
job: {},
});
});
it('sets remaining time to 00:00:00', () => {
expect(vm.$el.innerText).toBe('00:00:00');
});
describe('after mounting', () => {
beforeEach(done => {
Vue.nextTick()
.then(done)
.catch(done.fail);
});
it('doe not update remaining time', () => {
expect(vm.$el.innerText).toBe('00:00:00');
});
});
});
describe('if job is delayed job', () => {
let remainingTimeInMilliseconds = 42000;
beforeEach(() => {
spyOn(Date, 'now').and.callFake(
() => new Date(delayedJobFixture.scheduled_at).getTime() - remainingTimeInMilliseconds,
);
vm = mountComponent(dummyComponent, {
job: delayedJobFixture,
});
});
it('sets remaining time to 00:00:00', () => {
expect(vm.$el.innerText).toBe('00:00:00');
});
describe('after mounting', () => {
beforeEach(done => {
Vue.nextTick()
.then(done)
.catch(done.fail);
});
it('sets remaining time', () => {
expect(vm.$el.innerText).toBe('00:00:42');
});
it('updates remaining time', done => {
remainingTimeInMilliseconds = 41000;
jasmine.clock().tick(1000);
Vue.nextTick()
.then(() => {
expect(vm.$el.innerText).toBe('00:00:41');
})
.then(done)
.catch(done.fail);
});
});
});
});
...@@ -6,6 +6,7 @@ describe('pipeline graph job item', () => { ...@@ -6,6 +6,7 @@ describe('pipeline graph job item', () => {
const JobComponent = Vue.extend(JobItem); const JobComponent = Vue.extend(JobItem);
let component; let component;
const delayedJobFixture = getJSONFixture('jobs/delayed.json');
const mockJob = { const mockJob = {
id: 4256, id: 4256,
name: 'test', name: 'test',
...@@ -167,4 +168,30 @@ describe('pipeline graph job item', () => { ...@@ -167,4 +168,30 @@ describe('pipeline graph job item', () => {
expect(component.$el.querySelector(tooltipBoundary)).toBeNull(); expect(component.$el.querySelector(tooltipBoundary)).toBeNull();
}); });
}); });
describe('for delayed job', () => {
beforeEach(() => {
const fifteenMinutesInMilliseconds = 900000;
spyOn(Date, 'now').and.callFake(
() => new Date(delayedJobFixture.scheduled_at).getTime() - fifteenMinutesInMilliseconds,
);
});
it('displays remaining time in tooltip', done => {
component = mountComponent(JobComponent, {
job: delayedJobFixture,
});
Vue.nextTick()
.then(() => {
expect(
component.$el
.querySelector('.js-pipeline-graph-job-link')
.getAttribute('data-original-title'),
).toEqual('delayed job - delayed manual action (00:15:00)');
})
.then(done)
.catch(done.fail);
});
});
}); });
...@@ -13,24 +13,10 @@ describe Gitlab::Ci::Status::Build::Scheduled do ...@@ -13,24 +13,10 @@ describe Gitlab::Ci::Status::Build::Scheduled do
end end
describe '#status_tooltip' do describe '#status_tooltip' do
context 'when scheduled_at is not expired' do
let(:build) { create(:ci_build, scheduled_at: 1.minute.since, project: project) } let(:build) { create(:ci_build, scheduled_at: 1.minute.since, project: project) }
it 'shows execute_in of the scheduled job' do it 'has a placeholder for the remaining time' do
Timecop.freeze(Time.now.change(usec: 0)) do expect(subject.status_tooltip).to include('%{remainingTime}')
expect(subject.status_tooltip).to include('00:01:00')
end
end
end
context 'when scheduled_at is expired' do
let(:build) { create(:ci_build, :expired_scheduled, project: project) }
it 'shows 00:00' do
Timecop.freeze do
expect(subject.status_tooltip).to include('00:00')
end
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment