AdminScheduler.vue 18.3 KB
Newer Older
1 2 3 4
<template lang='pug'>
q-page.admin-terminal
  .row.q-pa-md.items-center
    .col-auto
5
      img.admin-icon.animated.fadeInLeft(src='/_assets/icons/fluent-bot-animated.svg')
6 7 8 9 10 11 12 13 14 15 16 17 18 19
    .col.q-pl-md
      .text-h5.text-primary.animated.fadeInLeft {{ t('admin.scheduler.title') }}
      .text-subtitle1.text-grey.animated.fadeInLeft.wait-p2s {{ t('admin.scheduler.subtitle') }}
    .col-auto.flex
      q-btn-toggle.q-mr-md(
        v-model='state.displayMode'
        push
        no-caps
        :disable='state.loading > 0'
        :toggle-color='$q.dark.isActive ? `white` : `black`'
        :toggle-text-color='$q.dark.isActive ? `black` : `white`'
        :text-color='$q.dark.isActive ? `white` : `black`'
        :color='$q.dark.isActive ? `dark-1` : `white`'
        :options=`[
20
          { label: t('admin.scheduler.schedule'), value: 'scheduled' },
21
          { label: t('admin.scheduler.upcoming'), value: 'upcoming' },
22
          { label: t('admin.scheduler.active'), value: 'active' },
23 24
          { label: t('admin.scheduler.completed'), value: 'completed' },
          { label: t('admin.scheduler.failed'), value: 'failed' },
25 26 27 28 29 30 31
        ]`
      )
      q-separator.q-mr-md(vertical)
      q-btn.q-mr-sm.acrylic-btn(
        icon='las la-question-circle'
        flat
        color='grey'
32
        :aria-label='t(`common.actions.viewDocs`)'
33
        :href='siteStore.docsBase + `/system/scheduler`'
34 35 36
        target='_blank'
        type='a'
        )
37
        q-tooltip {{ t(`common.actions.viewDocs`) }}
38 39 40 41 42
      q-btn.q-mr-sm.acrylic-btn(
        icon='las la-redo-alt'
        flat
        color='secondary'
        :loading='state.loading > 0'
43
        :aria-label='t(`common.actions.refresh`)'
44
        @click='load'
45 46
        )
        q-tooltip {{ t(`common.actions.refresh`) }}
47 48 49 50 51 52 53 54 55 56 57 58
  q-separator(inset)
  .q-pa-md.q-gutter-md
    template(v-if='state.displayMode === `scheduled`')
      q-card.rounded-borders(
        v-if='state.scheduledJobs.length < 1'
        flat
        :class='$q.dark.isActive ? `bg-dark-5 text-white` : `bg-grey-3 text-dark`'
        )
        q-card-section.items-center(horizontal)
          q-card-section.col-auto.q-pr-none
            q-icon(name='las la-info-circle', size='sm')
          q-card-section.text-caption {{ t('admin.scheduler.scheduledNone') }}
59
      q-card(v-else)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        q-table(
          :rows='state.scheduledJobs'
          :columns='scheduledJobsHeaders'
          row-key='name'
          flat
          hide-bottom
          :rows-per-page-options='[0]'
          :loading='state.loading > 0'
          )
          template(v-slot:body-cell-id='props')
            q-td(:props='props')
              q-spinner-clock.q-mr-sm(
                color='indigo'
                size='xs'
              )
75
          template(v-slot:body-cell-task='props')
76 77
            q-td(:props='props')
              strong {{props.value}}
78
              div: small.text-grey {{props.row.id}}
79 80
          template(v-slot:body-cell-cron='props')
            q-td(:props='props')
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
              q-chip(
                square
                size='md'
                color='blue'
                text-color='white'
                )
                span.font-robotomono {{ props.value }}
          template(v-slot:body-cell-type='props')
            q-td(:props='props')
              q-chip(
                square
                size='md'
                dense
                color='deep-orange'
                text-color='white'
                )
                small.text-uppercase {{ props.value }}
          template(v-slot:body-cell-created='props')
            q-td(:props='props')
              span {{props.value}}
              div: small.text-grey {{humanizeDate(props.row.createdAt)}}
          template(v-slot:body-cell-updated='props')
            q-td(:props='props')
              span {{props.value}}
              div: small.text-grey {{humanizeDate(props.row.updatedAt)}}
106 107 108 109 110 111 112 113 114 115
    template(v-else-if='state.displayMode === `upcoming`')
      q-card.rounded-borders(
        v-if='state.upcomingJobs.length < 1'
        flat
        :class='$q.dark.isActive ? `bg-dark-5 text-white` : `bg-grey-3 text-dark`'
        )
        q-card-section.items-center(horizontal)
          q-card-section.col-auto.q-pr-none
            q-icon(name='las la-info-circle', size='sm')
          q-card-section.text-caption {{ t('admin.scheduler.upcomingNone') }}
116
      q-card(v-else)
117 118 119 120 121 122 123 124 125 126 127
        q-table(
          :rows='state.upcomingJobs'
          :columns='upcomingJobsHeaders'
          row-key='name'
          flat
          hide-bottom
          :rows-per-page-options='[0]'
          :loading='state.loading > 0'
          )
          template(v-slot:body-cell-id='props')
            q-td(:props='props')
128
              q-icon(name='las la-clock', color='primary', size='sm')
129 130 131 132 133 134 135 136 137 138
          template(v-slot:body-cell-task='props')
            q-td(:props='props')
              strong {{props.value}}
              div: small.text-grey {{props.row.id}}
          template(v-slot:body-cell-waituntil='props')
            q-td(:props='props')
              span {{ props.value }}
              div: small.text-grey {{humanizeDate(props.row.waitUntil)}}
          template(v-slot:body-cell-retries='props')
            q-td(:props='props')
139
              span #[strong {{props.value + 1}}] #[span.text-grey / {{props.row.maxRetries + 1}}]
140 141 142 143 144 145 146 147
          template(v-slot:body-cell-useworker='props')
            q-td(:props='props')
              template(v-if='props.value')
                q-icon(name='las la-microchip', color='brown', size='sm')
                small.q-ml-xs.text-brown Worker
              template(v-else)
                q-icon(name='las la-leaf', color='teal', size='sm')
                small.q-ml-xs.text-teal In-Process
148 149
          template(v-slot:body-cell-date='props')
            q-td(:props='props')
150 151 152 153 154
              span {{props.value}}
              div
                i18n-t.text-grey(keypath='admin.scheduler.createdBy', tag='small')
                  template(#instance)
                    strong {{props.row.createdBy}}
155 156 157 158 159 160 161 162 163
          template(v-slot:body-cell-cancel='props')
            q-td(:props='props')
              q-btn.acrylic-btn.q-px-sm(
                flat
                icon='las la-window-close'
                color='negative'
                @click='cancelJob(props.row.id)'
                )
                q-tooltip(anchor='center left', self='center right') {{ t('admin.scheduler.cancelJob') }}
164 165 166 167 168 169 170 171 172
    template(v-else)
      q-card.rounded-borders(
        v-if='state.jobs.length < 1'
        flat
        :class='$q.dark.isActive ? `bg-dark-5 text-white` : `bg-grey-3 text-dark`'
        )
        q-card-section.items-center(horizontal)
          q-card-section.col-auto.q-pr-none
            q-icon(name='las la-info-circle', size='sm')
173
          q-card-section.text-caption {{ t('admin.scheduler.' + state.displayMode + 'None') }}
174
      q-card(v-else)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        q-table(
          :rows='state.jobs'
          :columns='jobsHeaders'
          row-key='name'
          flat
          hide-bottom
          :rows-per-page-options='[0]'
          :loading='state.loading > 0'
          )
          template(v-slot:body-cell-id='props')
            q-td(:props='props')
              q-avatar(
                v-if='props.row.state === `completed`'
                icon='las la-check'
                color='positive'
                text-color='white'
                size='sm'
                rounded
                )
              q-avatar(
                v-else-if='props.row.state === `failed`'
                icon='las la-times'
                color='negative'
                text-color='white'
                size='sm'
                rounded
                )
              q-avatar(
                v-else-if='props.row.state === `interrupted`'
                icon='las la-square-full'
                color='orange'
                text-color='white'
                size='sm'
                rounded
                )
              q-circular-progress(
                v-else-if='props.row.state === `active`'
                indeterminate
                size='sm'
                :thickness='0.4'
                color='blue'
                track-color='blue-1'
                center-color='blue-2'
                )
          template(v-slot:body-cell-task='props')
            q-td(:props='props')
              strong {{props.value}}
              div: small.text-grey {{props.row.id}}
          template(v-slot:body-cell-state='props')
            q-td(:props='props')
              template(v-if='props.value === `completed`')
                i18n-t(keypath='admin.scheduler.completedIn', tag='span')
                  template(#duration)
                    strong {{humanizeDuration(props.row.startedAt, props.row.completedAt)}}
                div: small.text-grey {{ humanizeDate(props.row.completedAt) }}
              template(v-else-if='props.value === `active`')
                em.text-grey {{ t('admin.scheduler.pending') }}
              template(v-else)
                strong.text-negative {{ props.value === 'failed' ? t('admin.scheduler.error') : t('admin.scheduler.interrupted') }}
                div: small {{ props.row.lastErrorMessage }}
          template(v-slot:body-cell-attempt='props')
            q-td(:props='props')
237
              span #[strong {{props.value}}] #[span.text-grey / {{props.row.maxRetries + 1}}]
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
          template(v-slot:body-cell-useworker='props')
            q-td(:props='props')
              template(v-if='props.value')
                q-icon(name='las la-microchip', color='brown', size='sm')
                small.q-ml-xs.text-brown Worker
              template(v-else)
                q-icon(name='las la-leaf', color='teal', size='sm')
                small.q-ml-xs.text-teal In-Process
          template(v-slot:body-cell-date='props')
            q-td(:props='props')
              span {{props.value}}
              div: small.text-grey {{humanizeDate(props.row.startedAt)}}
              div
                i18n-t.text-grey(keypath='admin.scheduler.createdBy', tag='small')
                  template(#instance)
                    strong {{props.row.executedBy}}
254 255 256 257 258 259 260 261 262 263 264
          template(v-slot:body-cell-actions='props')
            q-td(:props='props')
              q-btn.acrylic-btn.q-px-sm(
                v-if='props.row.state !== `active`'
                flat
                icon='las la-undo-alt'
                color='orange'
                @click='retryJob(props.row.id)'
                :disable='props.row.state === `interrupted` || props.row.state === `failed` && props.row.attempt < props.row.maxRetries'
                )
                q-tooltip(anchor='center left', self='center right') {{ t('admin.scheduler.retryJob') }}
265 266 267 268 269 270 271 272

</template>

<script setup>
import { onMounted, reactive, watch } from 'vue'
import { useMeta, useQuasar } from 'quasar'
import { useI18n } from 'vue-i18n'
import gql from 'graphql-tag'
273
import { DateTime, Duration, Interval } from 'luxon'
274

275
import { useSiteStore } from '@/stores/site'
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

// QUASAR

const $q = useQuasar()

// STORES

const siteStore = useSiteStore()

// I18N

const { t } = useI18n()

// META

useMeta({
  title: t('admin.scheduler.title')
})

// DATA

const state = reactive({
298
  displayMode: 'upcoming',
299
  scheduledJobs: [],
300
  upcomingJobs: [],
301 302 303 304 305 306 307 308 309 310 311 312 313
  jobs: [],
  loading: 0
})

const scheduledJobsHeaders = [
  {
    align: 'center',
    field: 'id',
    name: 'id',
    sortable: false,
    style: 'width: 15px; padding-right: 0;'
  },
  {
314
    label: t('common.field.task'),
315
    align: 'left',
316 317
    field: 'task',
    name: 'task',
318 319 320 321 322 323 324
    sortable: true
  },
  {
    label: t('admin.scheduler.cron'),
    align: 'left',
    field: 'cron',
    name: 'cron',
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    sortable: true
  },
  {
    label: t('admin.scheduler.type'),
    align: 'left',
    field: 'type',
    name: 'type',
    sortable: true
  },
  {
    label: t('admin.scheduler.createdAt'),
    align: 'left',
    field: 'createdAt',
    name: 'created',
    sortable: true,
    format: v => DateTime.fromISO(v).toRelative()
  },
  {
    label: t('admin.scheduler.updatedAt'),
    align: 'left',
    field: 'updatedAt',
    name: 'updated',
    sortable: true,
    format: v => DateTime.fromISO(v).toRelative()
  }
]

const upcomingJobsHeaders = [
  {
    align: 'center',
    field: 'id',
    name: 'id',
    sortable: false,
    style: 'width: 15px; padding-right: 0;'
  },
  {
    label: t('common.field.task'),
    align: 'left',
    field: 'task',
    name: 'task',
    sortable: true
366 367
  },
  {
368
    label: t('admin.scheduler.waitUntil'),
369
    align: 'left',
370 371 372 373
    field: 'waitUntil',
    name: 'waituntil',
    sortable: true,
    format: v => DateTime.fromISO(v).toRelative()
374 375
  },
  {
376
    label: t('admin.scheduler.attempt'),
377
    align: 'left',
378 379 380 381 382 383 384 385 386 387 388 389
    field: 'retries',
    name: 'retries',
    sortable: true
  },
  {
    label: t('admin.scheduler.useWorker'),
    align: 'left',
    field: 'useWorker',
    name: 'useworker',
    sortable: true
  },
  {
390
    label: t('admin.scheduler.scheduled'),
391 392
    align: 'left',
    field: 'createdAt',
393
    name: 'date',
394 395
    sortable: true,
    format: v => DateTime.fromISO(v).toRelative()
396 397 398 399 400 401 402
  },
  {
    align: 'center',
    field: 'id',
    name: 'cancel',
    sortable: false,
    style: 'width: 15px;'
403 404 405
  }
]

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
const jobsHeaders = [
  {
    align: 'center',
    field: 'id',
    name: 'id',
    sortable: false,
    style: 'width: 15px; padding-right: 0;'
  },
  {
    label: t('common.field.task'),
    align: 'left',
    field: 'task',
    name: 'task',
    sortable: true
  },
  {
    label: t('admin.scheduler.result'),
    align: 'left',
    field: 'state',
    name: 'state',
    sortable: true
  },
  {
    label: t('admin.scheduler.attempt'),
    align: 'left',
    field: 'attempt',
    name: 'attempt',
    sortable: true
  },
  {
    label: t('admin.scheduler.useWorker'),
    align: 'left',
    field: 'useWorker',
    name: 'useworker',
    sortable: true
  },
  {
    label: t('admin.scheduler.startedAt'),
    align: 'left',
    field: 'startedAt',
    name: 'date',
    sortable: true,
    format: v => DateTime.fromISO(v).toRelative()
449 450 451 452 453 454 455
  },
  {
    align: 'center',
    field: 'id',
    name: 'actions',
    sortable: false,
    style: 'width: 15px;'
456 457 458
  }
]

459 460 461 462 463 464 465 466
// WATCHERS

watch(() => state.displayMode, (newValue) => {
  load()
})

// METHODS

467 468 469 470
function humanizeDate (val) {
  return DateTime.fromISO(val).toFormat('fff')
}

471 472 473 474 475 476 477 478 479 480 481
function humanizeDuration (start, end) {
  const dur = Interval.fromDateTimes(DateTime.fromISO(start), DateTime.fromISO(end))
    .toDuration(['hours', 'minutes', 'seconds', 'milliseconds'])
  return Duration.fromObject({
    ...dur.hours > 0 && { hours: dur.hours },
    ...dur.minutes > 0 && { minutes: dur.minutes },
    ...dur.seconds > 0 && { seconds: dur.seconds },
    ...dur.milliseconds > 0 && { milliseconds: dur.milliseconds }
  }).toHuman({ unitDisplay: 'narrow', listStyle: 'short' })
}

482 483 484 485 486 487
async function load () {
  state.loading++
  try {
    if (state.displayMode === 'scheduled') {
      const resp = await APOLLO_CLIENT.query({
        query: gql`
488 489
          query getSystemJobsScheduled {
            systemJobsScheduled {
490
              id
491
              task
492
              cron
493 494 495
              type
              createdAt
              updatedAt
496 497 498 499 500
            }
          }
        `,
        fetchPolicy: 'network-only'
      })
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
      state.scheduledJobs = resp?.data?.systemJobsScheduled
    } else if (state.displayMode === 'upcoming') {
      const resp = await APOLLO_CLIENT.query({
        query: gql`
          query getSystemJobsUpcoming {
            systemJobsUpcoming {
              id
              task
              useWorker
              retries
              maxRetries
              waitUntil
              isScheduled
              createdBy
              createdAt
              updatedAt
            }
          }
        `,
        fetchPolicy: 'network-only'
      })
      state.upcomingJobs = resp?.data?.systemJobsUpcoming
523
    } else {
524
      const states = state.displayMode === 'failed' ? ['FAILED', 'INTERRUPTED'] : [state.displayMode.toUpperCase()]
525 526 527
      const resp = await APOLLO_CLIENT.query({
        query: gql`
          query getSystemJobs (
528
            $states: [SystemJobState]
529 530
          ) {
            systemJobs (
531
              states: $states
532
              ) {
533 534 535 536 537 538 539 540 541 542 543 544
                id
                task
                state
                useWorker
                wasScheduled
                attempt
                maxRetries
                lastErrorMessage
                executedBy
                createdAt
                startedAt
                completedAt
545 546 547 548
            }
          }
        `,
        variables: {
549
          states
550 551 552
        },
        fetchPolicy: 'network-only'
      })
553
      state.jobs = resp?.data?.systemJobs?.map(j => ({ ...j, state: j.state.toLowerCase() }))
554 555 556 557 558 559 560 561 562 563 564
    }
  } catch (err) {
    $q.notify({
      type: 'negative',
      message: 'Failed to load scheduled jobs.',
      caption: err.message
    })
  }
  state.loading--
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
async function cancelJob (jobId) {
  state.loading++
  try {
    const resp = await APOLLO_CLIENT.mutate({
      mutation: gql`
        mutation cancelJob ($id: UUID!) {
          cancelJob(id: $id) {
            operation {
              succeeded
              message
            }
          }
        }
      `,
      variables: {
        id: jobId
      }
    })
    if (resp?.data?.cancelJob?.operation?.succeeded) {
584
      load()
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
      $q.notify({
        type: 'positive',
        message: t('admin.scheduler.cancelJobSuccess')
      })
    } else {
      throw new Error(resp?.data?.cancelJob?.operation?.message || 'An unexpected error occured.')
    }
  } catch (err) {
    $q.notify({
      type: 'negative',
      message: 'Failed to cancel job.',
      caption: err.message
    })
  }
  state.loading--
}

async function retryJob (jobId) {
  state.loading++
  try {
    const resp = await APOLLO_CLIENT.mutate({
      mutation: gql`
        mutation retryJob ($id: UUID!) {
          retryJob(id: $id) {
            operation {
              succeeded
              message
            }
          }
        }
      `,
      variables: {
        id: jobId
      }
    })
    if (resp?.data?.retryJob?.operation?.succeeded) {
      this.load()
      $q.notify({
        type: 'positive',
        message: t('admin.scheduler.retryJobSuccess')
      })
    } else {
      throw new Error(resp?.data?.retryJob?.operation?.message || 'An unexpected error occured.')
    }
  } catch (err) {
    $q.notify({
      type: 'negative',
      message: 'Failed to retry the job.',
      caption: err.message
    })
  }
  state.loading--
}

639 640 641 642 643 644
// MOUNTED

onMounted(() => {
  load()
})
</script>