-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: using cron-parser package parsing job schedules
- Loading branch information
Johan
committed
Oct 25, 2019
1 parent
9e49245
commit ae32baa
Showing
11 changed files
with
181 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,43 @@ | ||
import cron from "cron-parser"; | ||
import { table } from "table"; | ||
import { isEmpty } from "../../arrayHelper"; | ||
import { ScheduledItem } from "../../../types"; | ||
import { parse } from "../../scheduledItemParser"; | ||
import ssh from "../../ssh"; | ||
import { create } from "../../../models/command"; | ||
|
||
const description = "Lists existing cron jobs"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const asTabular = (data: ScheduledItem[]): any[] => { | ||
if (isEmpty(data)) return []; | ||
|
||
const tableValues = data.map(x => { | ||
const values = Object.values(x); | ||
const expression = cron.parseExpression(x.cronExpression); | ||
values.push(expression.hasNext() ? expression.next() : ""); | ||
return values; | ||
}); | ||
|
||
const headings = Object.getOwnPropertyNames(data[0]); | ||
headings.push("next run"); | ||
|
||
tableValues.unshift(headings); | ||
|
||
return tableValues; | ||
}; | ||
|
||
const run = () => { | ||
ssh.execute("cru l"); | ||
const result = ssh.execute("cru l", { silent: true }); | ||
|
||
const parsed = result.stdout | ||
.split("\n") | ||
.map(x => parse(x)) | ||
.filter(x => x.id); | ||
|
||
const tabularData = asTabular(parsed); | ||
console.log(parsed); | ||
console.log(table(tabularData)); | ||
}; | ||
|
||
export default create({ description, run }); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ScheduledItem } from "../types"; | ||
import { takeWhile, takeWhileAll } from "./arrayHelper"; | ||
|
||
const idMarker = "#"; | ||
|
||
const parseCronExpression = (row: string[]): [string, string[]] => { | ||
const result = takeWhileAll(row, x => x.filter(z => z === " ").length < 5); | ||
const remainder = row.slice(result.length + 1); | ||
|
||
return [result.join("").trim(), remainder]; | ||
}; | ||
|
||
const parseCommand = (row: string[]): [string, string[]] => { | ||
const result = takeWhile(row, x => x !== idMarker); | ||
const remainder = row.slice(result.length + 1); | ||
|
||
return [result.join("").trim(), remainder]; | ||
}; | ||
|
||
const parseId = (row: string[]): string => { | ||
return row | ||
.join("") | ||
.replace(new RegExp(`${idMarker}`, "g"), "") | ||
.trim(); | ||
}; | ||
|
||
export const parse = (row: string): ScheduledItem => { | ||
const [cronExpression, cronRemainder] = parseCronExpression([...row]); | ||
const [command, commandRemainder] = parseCommand(cronRemainder); | ||
const id = parseId(commandRemainder); | ||
|
||
const item: ScheduledItem = { | ||
id, | ||
command, | ||
cronExpression | ||
}; | ||
|
||
return item; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ScheduledItem } from "../types"; | ||
|
||
export const empty: ScheduledItem = { | ||
id: "empty", | ||
cronExpression: "* * * * *", | ||
command: "" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ScheduledItem { | ||
id: string; | ||
cronExpression: string; | ||
command: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.