Skip to content

Commit 1b12612

Browse files
authored
Fix double job status pooling in audit log (baserow#4735)
1 parent e4b0823 commit 1b12612

File tree

1 file changed

+36
-67
lines changed

1 file changed

+36
-67
lines changed

enterprise/web-frontend/modules/baserow_enterprise/components/admin/modals/AuditLogExportModal.vue

Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@
5757
</template>
5858

5959
<script>
60-
import { mapState } from 'vuex'
61-
6260
import modal from '@baserow/modules/core/mixins/modal'
6361
import error from '@baserow/modules/core/mixins/error'
62+
import job from '@baserow/modules/core/mixins/job'
6463
import moment from '@baserow/modules/core/moment'
6564
import { getHumanPeriodAgoCount } from '@baserow/modules/core/utils/date'
6665
import ExportLoadingBar from '@baserow/modules/database/components/export/ExportLoadingBar'
6766
import AuditLogExportForm from '@baserow_enterprise/components/admin/forms/AuditLogExportForm'
6867
import AuditLogAdminService from '@baserow_enterprise/services/auditLog'
68+
import { AuditLogExportJobType } from '@baserow_enterprise/jobTypes'
6969
7070
const MAX_EXPORT_FILES = 4
7171
7272
export default {
7373
name: 'AuditLogExportModal',
7474
components: { AuditLogExportForm, ExportLoadingBar },
75-
mixins: [modal, error],
75+
mixins: [modal, error, job],
7676
props: {
7777
filters: {
7878
type: Object,
@@ -86,9 +86,6 @@ export default {
8686
data() {
8787
return {
8888
loading: false,
89-
timeoutId: null,
90-
timeNextPoll: 1000,
91-
job: null,
9289
lastFinishedJobs: [],
9390
}
9491
},
@@ -103,35 +100,30 @@ export default {
103100
this.lastFinishedJobs = filteredJobs.filter(
104101
(job) => job.state === 'finished'
105102
)
106-
const runningJob = filteredJobs.find(
107-
(job) => !['failed', 'cancelled', 'finished'].includes(job.state)
108-
)
109-
this.job = runningJob || null
110-
if (this.job) {
111-
this.scheduleNextPoll()
112-
} else {
103+
this.loadRunningJob()
104+
if (!this.jobIsRunning) {
113105
this.loading = false
114106
}
115107
},
116-
// the poll timeout can only be scheduled on the client
117108
fetchOnServer: false,
118-
computed: {
119-
jobHasFailed() {
120-
return ['failed', 'cancelled'].includes(this.job.state)
121-
},
122-
jobIsRunning() {
123-
return (
124-
this.job !== null && this.job.state !== 'finished' && !this.jobHasFailed
109+
methods: {
110+
loadRunningJob() {
111+
const runningJob = this.$store.getters['job/getUnfinishedJobs'].find(
112+
(job) => {
113+
if (job.type !== AuditLogExportJobType.getType()) {
114+
return false
115+
}
116+
if (this.workspaceId) {
117+
return job.filter_workspace_id === this.workspaceId
118+
}
119+
return true
120+
}
125121
)
122+
if (runningJob) {
123+
this.job = runningJob
124+
this.loading = true
125+
}
126126
},
127-
...mapState({
128-
selectedTableViews: (state) => state.view.items,
129-
}),
130-
},
131-
beforeUnmount() {
132-
this.stopPollIfRunning()
133-
},
134-
methods: {
135127
getExportedFilename(job) {
136128
return job ? `audit_log_${job.created_on}.csv` : ''
137129
},
@@ -152,7 +144,6 @@ export default {
152144
return this.$t(`datetime.${period}Ago`, { count }, count)
153145
},
154146
hidden() {
155-
this.stopPollIfRunning()
156147
if (this.job && !this.jobIsRunning) {
157148
this.lastFinishedJobs = [this.job, ...this.lastFinishedJobs]
158149
this.job = null
@@ -165,7 +156,6 @@ export default {
165156
166157
this.loading = true
167158
this.hideError()
168-
this.stopPollIfRunning()
169159
const filters = Object.fromEntries(
170160
Object.entries(this.filters).map(([key, value]) => [
171161
`filter_${key}`,
@@ -188,49 +178,28 @@ export default {
188178
0,
189179
MAX_EXPORT_FILES - 1
190180
)
191-
this.job = data
192-
this.scheduleNextPoll()
181+
await this.createAndMonitorJob(data)
193182
} catch (error) {
194183
this.loading = false
195184
this.handleError(error, 'export')
196185
}
197186
},
198-
async getJobInfo() {
199-
try {
200-
const { data } = await AuditLogAdminService(
201-
this.$client
202-
).getExportJobInfo(this.job.id)
203-
this.job = data
204-
205-
if (this.jobIsRunning) {
206-
this.scheduleNextPoll()
207-
return
208-
}
209-
210-
this.loading = false
211-
if (this.jobHasFailed) {
212-
let title, message
213-
if (this.job.status === 'failed') {
214-
title = this.$t('auditLogExportModal.failedTitle')
215-
message = this.$t('auditLogExportModal.failedDescription')
216-
} else {
217-
// cancelled
218-
title = this.$t('auditLogExportModal.cancelledTitle')
219-
message = this.$t('auditLogExportModal.cancelledDescription')
220-
}
221-
this.showError(title, message)
222-
}
223-
} catch (error) {
224-
this.handleError(error, 'export')
225-
}
187+
onJobFinished() {
188+
this.loading = false
226189
},
227-
scheduleNextPoll() {
228-
this.timeNextPoll = Math.min(this.timeNextPoll * 1.1, 5000)
229-
this.timeoutId = setTimeout(this.getJobInfo, this.timeNextPoll)
190+
onJobFailed() {
191+
this.loading = false
192+
this.showError(
193+
this.$t('auditLogExportModal.failedTitle'),
194+
this.$t('auditLogExportModal.failedDescription')
195+
)
230196
},
231-
stopPollIfRunning() {
232-
clearTimeout(this.timeoutId)
233-
this.timeoutId = null
197+
onJobCancelled() {
198+
this.loading = false
199+
this.showError(
200+
this.$t('auditLogExportModal.cancelledTitle'),
201+
this.$t('auditLogExportModal.cancelledDescription')
202+
)
234203
},
235204
valuesChanged() {
236205
this.isValid = this.$refs.form.isFormValid()

0 commit comments

Comments
 (0)