Skip to content

Commit

Permalink
refactor: start using constants instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Jun 2, 2021
1 parent 19160e5 commit 15e3082
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
10 changes: 9 additions & 1 deletion packages/api/src/handlers/queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Status,
ValidMetrics,
} from '../../typings/app';
import { STATUSES } from '../constants/statuses';

type MetricName = keyof ValidMetrics;

Expand Down Expand Up @@ -62,7 +63,14 @@ const formatJob = (job: QueueJob, queue: BaseAdapter): AppJob => {
};
};

const allStatuses: JobStatus[] = ['active', 'completed', 'delayed', 'failed', 'paused', 'waiting'];
const allStatuses: JobStatus[] = [
STATUSES.active,
STATUSES.completed,
STATUSES.delayed,
STATUSES.failed,
STATUSES.paused,
STATUSES.waiting,
];
const JOB_PER_PAGE = 10;

function getPagination(statuses: JobStatus[], counts: JobCounts, currentPage: number): Pagination {
Expand Down
8 changes: 3 additions & 5 deletions packages/api/src/handlers/retryAll.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {
BullBoardRequest,
ControllerHandlerReturnType,
} from '../../typings/app';
import { BullBoardRequest, ControllerHandlerReturnType } from '../../typings/app';
import { BaseAdapter } from '../queueAdapters/base';
import { queueProvider } from '../providers/queue';
import { STATUSES } from '../constants/statuses';

async function retryAll(
_req: BullBoardRequest,
queue: BaseAdapter
): Promise<ControllerHandlerReturnType> {
const jobs = await queue.getJobs(['failed']);
const jobs = await queue.getJobs([STATUSES.failed]);
await Promise.all(jobs.map((job) => job.retry()));

return { status: 200, body: {} };
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/components/JobCard/Progress/Progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import s from './Progress.module.css';
import cn from 'clsx';
import { Status } from '@bull-board/api/typings/app';
import { STATUSES } from '@bull-board/api/src/constants/statuses';

export const Progress = ({
percentage,
Expand Down Expand Up @@ -29,7 +30,7 @@ export const Progress = ({
cy="70"
r="70"
fill="none"
stroke={status === 'failed' ? '#F56565' : '#48BB78'}
stroke={status === STATUSES.failed ? '#F56565' : '#48BB78'}
strokeWidth="8"
strokeLinecap="round"
strokeDasharray="600"
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/src/components/JobCard/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { format, formatDistance, getYear, isToday } from 'date-fns';
import React from 'react';
import s from './Timeline.module.css';
import { AppJob, Status } from '@bull-board/api/typings/app';
import { STATUSES } from '@bull-board/api/src/constants/statuses';

type TimeStamp = number | Date;

Expand All @@ -23,7 +24,7 @@ export const Timeline = function Timeline({ job, status }: { job: AppJob; status
<small>Added at</small>
<time>{formatDate(job.timestamp || 0)}</time>
</li>
{!!job.delay && job.delay > 0 && status === 'delayed' && (
{!!job.delay && job.delay > 0 && status === STATUSES.delayed && (
<li>
<small>Delayed for</small>
<time>
Expand Down Expand Up @@ -52,7 +53,7 @@ export const Timeline = function Timeline({ job, status }: { job: AppJob; status
includeSeconds: true,
})}
</small>
<small>{status === 'failed' ? 'Failed' : 'Finished'} at</small>
<small>{status === STATUSES.failed ? 'Failed' : 'Finished'} at</small>
<time>{formatDate(job.finishedOn)}</time>
</li>
)}
Expand Down
11 changes: 6 additions & 5 deletions packages/ui/src/components/QueueActions/QueueActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import { TrashIcon } from '../Icons/Trash';
import { Button } from '../JobCard/Button/Button';
import s from './QueueActions.module.css';
import { AppQueue, Status } from '@bull-board/api/typings/app';
import { STATUSES } from '@bull-board/api/src/constants/statuses';

interface QueueActionProps {
queue: AppQueue;
actions: Store['actions'];
status: Status;
}

const ACTIONABLE_STATUSES = ['failed', 'delayed', 'completed'];
const ACTIONABLE_STATUSES = [STATUSES.failed, STATUSES.delayed, STATUSES.completed] as const;

const isStatusActionable = (status: Status): boolean => ACTIONABLE_STATUSES.includes(status);
const isStatusActionable = (status: Status): boolean => ACTIONABLE_STATUSES.includes(status as any);

const CleanAllButton = ({ onClick }: any) => (
<Button onClick={onClick} className={s.button}>
Expand All @@ -30,7 +31,7 @@ export const QueueActions = ({ status, actions, queue }: QueueActionProps) => {

return (
<ul className={s.queueActions}>
{status === 'failed' && (
{status === STATUSES.failed && (
<>
<li>
<Button onClick={actions.retryAll(queue.name)} className={s.button}>
Expand All @@ -43,12 +44,12 @@ export const QueueActions = ({ status, actions, queue }: QueueActionProps) => {
</li>
</>
)}
{status === 'delayed' && (
{status === STATUSES.delayed && (
<li>
<CleanAllButton onClick={actions.cleanAllDelayed(queue.name)} />
</li>
)}
{status === 'completed' && (
{status === STATUSES.completed && (
<li>
<CleanAllButton onClick={actions.cleanAllCompleted(queue.name)} />
</li>
Expand Down

0 comments on commit 15e3082

Please sign in to comment.