Skip to content

Commit

Permalink
job: refactor args
Browse files Browse the repository at this point in the history
* adds PredefinedArgsSchema schema class with predefined args, including
  `since`.
* changes JobType public/private interface to make it easier to define
  your own job types
* logs celery tasks error message and shows it in the UI
* removes unused code
  • Loading branch information
ntarocco authored and kpsherva committed Oct 14, 2024
1 parent 118e2de commit c3721f0
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 114 deletions.
3 changes: 2 additions & 1 deletion invenio_jobs/administration/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# details.

"""Invenio administration view module."""

from flask import current_app
from invenio_administration.views.base import (
AdminResourceCreateView,
Expand Down Expand Up @@ -96,7 +97,7 @@ class JobsDetailsView(JobsAdminMixin, AdminResourceListView):
pid_value = "<pid_value>"

item_field_list = {
"run": {"text": _("Run"), "order": 1, "width": 2},
"run": {"text": _("Run (ISO UTC)"), "order": 1, "width": 2},
"duration": {"text": _("Duration"), "order": 2, "width": 2},
"message": {"text": _("Message"), "order": 3, "width": 10},
"user": {"text": _("Started by"), "order": 4, "width": 2},
Expand Down
2 changes: 1 addition & 1 deletion invenio_jobs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class AttrDict(dict):

def __init__(self, *args, **kwargs):
"""Constructor."""
super(AttrDict, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.__dict__ = self
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const domContainer = document.getElementById("invenio-search-config");

const defaultComponents = initDefaultSearchComponents(domContainer);

const overridenComponents = {
const searchOverriddenComponents = {
...defaultComponents,
"InvenioAdministration.SearchResultItem.layout": SearchResultItemLayout,
"SearchApp.layout": JobSearchLayout,
};

createSearchAppInit(
overridenComponents,
searchOverriddenComponents,
true,
"invenio-search-config",
false,
Expand All @@ -48,7 +48,6 @@ const fields = JSON.parse(detailsConfig.dataset.fields);
const resourceName = JSON.parse(detailsConfig.dataset.resourceName);
const displayEdit = JSON.parse(detailsConfig.dataset.displayEdit);
const displayDelete = JSON.parse(detailsConfig.dataset.displayDelete);
const actions = JSON.parse(detailsConfig.dataset.actions);
const apiEndpoint = _get(detailsConfig.dataset, "apiEndpoint");
const idKeyPath = JSON.parse(_get(detailsConfig.dataset, "pidPath", "pid"));
const listUIEndpoint = detailsConfig.dataset.listEndpoint;
Expand All @@ -67,7 +66,6 @@ detailsConfig &&
<OverridableContext.Provider value={cmps}>
<AdminDetailsView
title={title}
actions={actions}
apiEndpoint={apiEndpoint}
columns={fields}
pid={pidValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import PropTypes from "prop-types";
import React, { Component } from "react";
import { UserListItemCompact, toRelativeTime } from "react-invenio-forms";
import { withState } from "react-searchkit";
import { DateTime } from "luxon";
import { Popup, Table, Button } from "semantic-ui-react";
import { Actions } from "@js/invenio_administration";
import { StatusFormatter } from "./StatusFormatter";
Expand Down Expand Up @@ -52,6 +53,15 @@ class SearchResultItemComponent extends Component {
} = this.props;

const { lastRunStatus, lastRunCreatedTime } = this.state;
const lastRunFormatted = DateTime.fromISO(lastRunCreatedTime)
.setLocale(i18next.language)
.toLocaleString(DateTime.DATETIME_FULL_WITH_SECONDS);

const nextRunFormatted = result.next_run
? DateTime.fromISO(result.next_run)
.setLocale(i18next.language)
.toLocaleString(DateTime.DATETIME_FULL_WITH_SECONDS)
: "-";

return (
<Table.Row>
Expand All @@ -77,7 +87,7 @@ class SearchResultItemComponent extends Component {
/>
<BoolFormatter
tooltip={i18next.t("Active")}
icon="check"
icon="check circle"
color="green"
value={result.active === true}
/>
Expand All @@ -92,7 +102,7 @@ class SearchResultItemComponent extends Component {
<>
<StatusFormatter status={lastRunStatus} />
<Popup
content={lastRunCreatedTime}
content={lastRunFormatted}
trigger={
<span>
{toRelativeTime(lastRunCreatedTime, i18next.language)}
Expand All @@ -101,40 +111,42 @@ class SearchResultItemComponent extends Component {
/>
</>
) : (
""
"-"
)}
</Table.Cell>
{result?.last_run?.started_by ? (
<Table.Cell
key={`job-user-${result.last_run.started_by.id}`}
data-label={i18next.t("Started by")}
collapsing
className="word-break-all"
>
<Table.Cell
key={`job-user-${result?.last_run?.started_by?.id}`}
data-label={i18next.t("Started by")}
collapsing
className="word-break-all"
>
{result?.last_run?.started_by ? (
<UserListItemCompact
user={result.last_run.started_by}
id={result.last_run.started_by.id}
/>
</Table.Cell>
) : (
<Table.Cell
key="job-user"
data-label={i18next.t("Started by")}
collapsing
className="word-break-all"
>
System
</Table.Cell>
)}
) : (
i18next.t("System")
)}
</Table.Cell>
<Table.Cell
collapsing
key={`job-next-run${result.next_run}`}
data-label={i18next.t("Next run")}
className="word-break-all"
>
{result.active === false
? "Inactive"
: toRelativeTime(result.next_run, i18next.language) ?? "−"}
{result.active === false ? (
"Inactive"
) : (
<Popup
content={nextRunFormatted}
trigger={
<span>
{toRelativeTime(result.next_run, i18next.language) ?? "-"}
</span>
}
/>
)}
</Table.Cell>
<Table.Cell collapsing>
<Button.Group size="tiny" className="relaxed">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ export class RunActionForm extends Component {
<Divider />
<Message info>
<Trans>
"Modifying the <b>Custom arguments</b> field will
replace any arguments specified above and run the task
with custom configuration.
<b>Custom args:</b> when provided, the input below will
override any arguments specified above.
</Trans>
</Message>
<TextArea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@ import { NotificationContext } from "@js/invenio_administration";
import { i18next } from "@translations/invenio_jobs/i18next";
import PropTypes from "prop-types";
import React, { Component } from "react";
import { DateTime } from "luxon";
import { UserListItemCompact } from "react-invenio-forms";
import { withState } from "react-searchkit";
import { Table } from "semantic-ui-react";
import { Button, Table } from "semantic-ui-react";
import { StatusFormatter } from "./StatusFormatter";
import { StopButton } from "./StopButton";
import { diffTimestamps } from "./utils/diffTimestamps";

const MSG_MAX_LINES = 3;

class SearchResultItemComponent extends Component {
constructor(props) {
super(props);

this.state = {
msgShowAll: false,
status: props.result.status,
};
}

toggleShowAll = () => {
this.setState((prevState) => ({
msgShowAll: !prevState.msgShowAll,
}));
};

static contextType = NotificationContext;

onError = (e) => {
Expand All @@ -40,17 +50,26 @@ class SearchResultItemComponent extends Component {

render() {
const { result } = this.props;
const { status } = this.state;
const { msgShowAll, status } = this.state;
const msgLines = result.message ? result.message.split("\n") : [];
const msgHasMoreThanMaxLines = msgLines.length > MSG_MAX_LINES;
const msgLinesShown =
msgHasMoreThanMaxLines && msgShowAll
? msgLines
: msgLines.slice(0, MSG_MAX_LINES);
const createdFormatted = DateTime.fromISO(result.created).toFormat(
"yyyy/LL/dd HH:mm:ss"
);
return (
<Table.Row>
<Table.Row verticalAlign="top">
<Table.Cell
key={`run-name-${result.started_at}`}
data-label={i18next.t("Run")}
collapsing
className="word-break-all"
>
<StatusFormatter status={status} />
<a href={result.links.self}>{result.created.slice(0, 16)}</a>
<a href={result.links.self}>{createdFormatted}</a>
</Table.Cell>
<Table.Cell
key={`run-last-run-${status}`}
Expand All @@ -77,33 +96,40 @@ class SearchResultItemComponent extends Component {
<Table.Cell
key={`run-last-run-${result.message}`}
data-label={i18next.t("Message")}
collapsing
className=""
>
{result.message}
{msgLinesShown.map((line, index) => (
<React.Fragment key={index}>
{line}
<br />
</React.Fragment>
))}
{msgHasMoreThanMaxLines && (
<React.Fragment>
{!msgShowAll && <div>...</div>}
<Button as="a" onClick={this.toggleShowAll} size="mini">
{msgShowAll ? i18next.t("Show less") : i18next.t("Show all")}
</Button>
</React.Fragment>
)}
</Table.Cell>
{result.started_by ? (
<Table.Cell
key={`job-user-${result.started_by.id}`}
data-label={i18next.t("Started by")}
collapsing
className="word-break-all"
>

<Table.Cell
key={`job-user-${result?.started_by?.id}`}
data-label={i18next.t("Started by")}
collapsing
className="word-break-all"
>
{result.started_by ? (
<UserListItemCompact
user={result.started_by}
id={result.started_by.id}
/>
</Table.Cell>
) : (
<Table.Cell
key="job-user"
data-label={i18next.t("Started by")}
collapsing
className="word-break-all"
>
System
</Table.Cell>
)}
) : (
i18next.t("System")
)}
</Table.Cell>

<Table.Cell collapsing>
{status === "RUNNING" ? (
<StopButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const domContainer = document.getElementById("invenio-search-config");

const defaultComponents = initDefaultSearchComponents(domContainer);

const overridenComponents = {
const overriddenComponents = {
...defaultComponents,
"InvenioAdministration.SearchResultItem.layout": SearchResultItemLayout,
"InvenioAdministration.ResourceActions": JobActions,
Expand All @@ -25,7 +25,7 @@ const overridenComponents = {
};

createSearchAppInit(
overridenComponents,
overriddenComponents,
true,
"invenio-search-config",
false,
Expand Down
1 change: 1 addition & 0 deletions invenio_jobs/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# under the terms of the MIT License; see LICENSE file for more details.

"""Jobs extension."""

import importlib_metadata
from celery import current_app as current_celery_app
from flask import current_app
Expand Down
Loading

0 comments on commit c3721f0

Please sign in to comment.