Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notification): send notification to admin when job failed #3653

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export default function JobRunDetail() {
(stateLabel === 'Pending' || stateLabel === 'Running') &&
!currentNode?.stdout

const handleBackNavigation = () => {
if (typeof window !== 'undefined' && window.history.length <= 1) {
router.push('/jobs')
} else {
router.back()
}
}

React.useEffect(() => {
let timer: number
if (currentNode?.createdAt && !currentNode?.finishedAt) {
Expand All @@ -61,7 +69,7 @@ export default function JobRunDetail() {
{currentNode && (
<>
<div
onClick={() => router.back()}
onClick={handleBackNavigation}
className="-ml-1 flex cursor-pointer items-center transition-opacity hover:opacity-60"
>
<IconChevronLeft className="mr-1 h-6 w-6" />
Expand Down
4 changes: 4 additions & 0 deletions ee/tabby-ui/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@
.dialog-without-close-btn > button {
display: none;
}

.unread-notification::before {
@apply content-[''] float-left w-2 h-2 mr-1.5 mt-2 rounded-full bg-red-400;
}
39 changes: 17 additions & 22 deletions ee/tabby-ui/components/notification-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import { useMutation } from '@/lib/tabby/gql'
import { notificationsQuery } from '@/lib/tabby/query'
import { ArrayElementType } from '@/lib/types'
import { cn } from '@/lib/utils'

import LoadingWrapper from './loading-wrapper'
import { ListSkeleton } from './skeleton'
import { Button } from './ui/button'
import { Button } from '@/components/ui/button'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuTrigger
} from './ui/dropdown-menu'
import { IconBell, IconCheck } from './ui/icons'
import { Separator } from './ui/separator'
import { Tabs, TabsList, TabsTrigger } from './ui/tabs'
} from '@/components/ui/dropdown-menu'
import { IconBell, IconCheck } from '@/components/ui/icons'
import { Separator } from '@/components/ui/separator'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import LoadingWrapper from '@/components/loading-wrapper'
import { MemoizedReactMarkdown } from '@/components/markdown'
import { ListSkeleton } from '@/components/skeleton'

interface Props extends HTMLAttributes<HTMLDivElement> {}

Expand Down Expand Up @@ -89,7 +89,7 @@ export function NotificationBox({ className, ...rest }: Props) {
</div>
<Separator />
<Tabs
className="relative my-2 flex-1 overflow-y-auto px-4"
className="relative my-2 flex-1 overflow-y-auto px-5"
defaultValue="unread"
>
<TabsList className="sticky top-0 z-10 grid w-full grid-cols-2">
Expand Down Expand Up @@ -156,8 +156,6 @@ interface NotificationItemProps extends HTMLAttributes<HTMLDivElement> {
}

function NotificationItem({ data }: NotificationItemProps) {
const { title, content } = resolveNotification(data.content)

const markNotificationsRead = useMutation(markNotificationsReadMutation)

const onClickMarkRead = () => {
Expand All @@ -168,17 +166,14 @@ function NotificationItem({ data }: NotificationItemProps) {

return (
<div className="space-y-1.5">
<div className="space-y-1.5">
<div className="flex items-center gap-1.5 overflow-hidden text-sm font-medium">
{!data.read && (
<span className="h-2 w-2 shrink-0 rounded-full bg-red-400"></span>
)}
<span className="flex-1 truncate">{title}</span>
</div>
<div className="whitespace-pre-wrap break-words text-sm text-muted-foreground">
{content}
</div>
</div>
<MemoizedReactMarkdown
className={cn(
'prose max-w-none break-words text-sm dark:prose-invert prose-p:my-1 prose-p:leading-relaxed',
{ 'unread-notification': !data.read }
)}
>
{data.content}
</MemoizedReactMarkdown>
<div className="flex items-center justify-between text-xs text-muted-foreground">
<span className="text-muted-foreground">
{formatNotificationTime(data.createdAt)}
Expand Down
44 changes: 25 additions & 19 deletions ee/tabby-webserver/src/service/background_job/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
context: Arc<dyn ContextService>,
db: DbConn,
) -> tabby_schema::Result<()> {
let mut errors = vec![];
let mut has_error = false;

Check warning on line 23 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L23

Added line #L23 was not covered by tests

if let Err(e) = db.delete_expired_token().await {
errors.push(format!("Failed to delete expired token: {}", e));
has_error = true;
logkit::warn!("Failed to delete expired tokens: {}", e);

Check warning on line 27 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
};
if let Err(e) = db.delete_expired_password_resets().await {
errors.push(format!("Failed to delete expired password resets: {}", e));
has_error = true;
logkit::warn!("Failed to delete expired password resets: {}", e);

Check warning on line 31 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L30-L31

Added lines #L30 - L31 were not covered by tests
};
if let Err(e) = db.delete_expired_ephemeral_threads().await {
errors.push(format!("Failed to delete expired ephemeral threads: {}", e));
has_error = true;
logkit::warn!("Failed to delete expired ephemeral threads: {}", e);

Check warning on line 35 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L34-L35

Added lines #L34 - L35 were not covered by tests
};

// Read all active sources
Expand All @@ -44,54 +47,57 @@
.delete_unused_source_id_read_access_policy(&active_source_ids)
.await
{
errors.push(format!(
has_error = true;
logkit::warn!(

Check warning on line 51 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L50-L51

Added lines #L50 - L51 were not covered by tests
"Failed to delete unused source id read access policy: {}",
e
));
);
};
}
Err(e) => {
errors.push(format!("Failed to read active sources: {}", e));
has_error = true;
logkit::warn!("Failed to read active sources: {}", e);

Check warning on line 59 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L58-L59

Added lines #L58 - L59 were not covered by tests
}
}

if let Err(e) = Self::data_retention(now, &db).await {
errors.push(format!("Failed to run data retention job: {}", e));
has_error = true;
logkit::warn!("Failed to run data retention job: {}", e);

Check warning on line 65 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L64-L65

Added lines #L64 - L65 were not covered by tests
}

if errors.is_empty() {
if !has_error {

Check warning on line 68 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L68

Added line #L68 was not covered by tests
Ok(())
} else {
Err(CoreError::Other(anyhow::anyhow!(
"Failed to run db maintenance job:\n{}",
errors.join(";\n")
"Failed to run db maintenance job"

Check warning on line 72 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L72

Added line #L72 was not covered by tests
)))
}
}

async fn data_retention(now: DateTime<Utc>, db: &DbConn) -> tabby_schema::Result<()> {
let mut errors = vec![];
let mut has_error = false;

if let Err(e) = db.delete_job_run_before_three_months(now).await {
errors.push(format!(
has_error = true;
logkit::warn!(

Check warning on line 82 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L81-L82

Added lines #L81 - L82 were not covered by tests
"Failed to clean up and retain only the last 3 months of jobs: {}",
e
));
);
}

if let Err(e) = db.delete_user_events_before_three_months(now).await {
errors.push(format!(
has_error = true;
logkit::warn!(

Check warning on line 90 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L89-L90

Added lines #L89 - L90 were not covered by tests
"Failed to clean up and retain only the last 3 months of user events: {}",
e
));
);
}

if errors.is_empty() {
if !has_error {
Ok(())
} else {
Err(CoreError::Other(anyhow::anyhow!(
"Failed to run data retention job:\n{}",
errors.join(";\n")
"Failed to run data retention job"

Check warning on line 100 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L100

Added line #L100 was not covered by tests
)))
}
}
Expand Down
12 changes: 8 additions & 4 deletions ee/tabby-webserver/src/service/background_job/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@
git_repository: Arc<dyn GitRepositoryService>,
job: Arc<dyn JobService>,
) -> tabby_schema::Result<()> {
let repositories = git_repository
.repository_list()
.await
.context("Must be able to retrieve repositories for sync")?;
let repositories = match git_repository.repository_list().await {
Ok(repos) => repos,
Err(err) => {
logkit::warn!("Failed to list repositories: {}", err);
return Err(err);

Check warning on line 49 in ee/tabby-webserver/src/service/background_job/git.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/git.rs#L45-L49

Added lines #L45 - L49 were not covered by tests
}
};

let repositories: Vec<_> = repositories
.into_iter()
Expand All @@ -57,6 +60,7 @@
.trigger(BackgroundJobEvent::SchedulerGitRepository(repository).to_command())
.await;
}

Ok(())
}
}
9 changes: 8 additions & 1 deletion ee/tabby-webserver/src/service/background_job/helper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
pub use cron::CronStream;
pub use logger::JobLogger;

pub trait Job {
pub trait Job: serde::Serialize {
const NAME: &'static str;

fn name(&self) -> &'static str {
Self::NAME
}
fn to_command(&self) -> String {
serde_json::to_string(self).unwrap()
}

Check warning on line 15 in ee/tabby-webserver/src/service/background_job/helper/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/helper/mod.rs#L10-L15

Added lines #L10 - L15 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::sync::Arc;

use serde::Serialize;
use tabby_index::public::{run_index_garbage_collection, CodeIndexer};
use tabby_schema::{context::ContextService, repository::RepositoryService};

use super::helper::Job;

#[derive(Serialize)]
pub struct IndexGarbageCollection;

impl Job for IndexGarbageCollection {
Expand All @@ -18,17 +20,32 @@
context: Arc<dyn ContextService>,
) -> tabby_schema::Result<()> {
// Run garbage collection on the index
let sources = context
.read(None)
.await?
let sources = match context.read(None).await {
Ok(sources) => sources,
Err(err) => {
logkit::warn!("Failed to list sources: {}", err);
return Err(err);

Check warning on line 27 in ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs#L23-L27

Added lines #L23 - L27 were not covered by tests
}
};
let sources = sources

Check warning on line 30 in ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs#L30

Added line #L30 was not covered by tests
.sources
.into_iter()
.map(|x| x.source_id())
.collect::<Vec<_>>();
run_index_garbage_collection(sources)?;

if let Err(e) = run_index_garbage_collection(sources) {
logkit::warn!("Failed to run index garbage collection: {}", e);
return Err(e.into());
}

Check warning on line 39 in ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs#L36-L39

Added lines #L36 - L39 were not covered by tests

// Run garbage collection on the code repositories (cloned directories)
let repositories = repository.list_all_code_repository().await?;
let repositories = match repository.list_all_code_repository().await {
Ok(repos) => repos,
Err(err) => {
logkit::warn!("Failed to list repositories: {}", err);
return Err(err);

Check warning on line 46 in ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/index_garbage_collection.rs#L42-L46

Added lines #L42 - L46 were not covered by tests
}
};
let mut code = CodeIndexer::default();
code.garbage_collection(&repositories).await;

Expand Down
18 changes: 14 additions & 4 deletions ee/tabby-webserver/src/service/background_job/license_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tabby_schema::{
context::ContextService,
license::{LicenseService, LicenseType},
notification::{NotificationRecipient, NotificationService},
};
Expand All @@ -23,21 +22,32 @@
license_service: Arc<dyn LicenseService>,
notification_service: Arc<dyn NotificationService>,
) -> tabby_schema::Result<()> {
let license = license_service.read().await?;
let license = match license_service.read().await {
Ok(license) => license,
Err(err) => {
logkit::warn!("Failed to read license: {}", err);
return Err(err);

Check warning on line 29 in ee/tabby-webserver/src/service/background_job/license_check.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/license_check.rs#L25-L29

Added lines #L25 - L29 were not covered by tests
}
};
if license.r#type == LicenseType::Community {
return Ok(());
}
if let Some(expire_in_days) = license.expire_in_days() {
if expire_in_days < 7 && expire_in_days > 0 {
notification_service
if let Err(e) = notification_service

Check warning on line 37 in ee/tabby-webserver/src/service/background_job/license_check.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/license_check.rs#L37

Added line #L37 was not covered by tests
.create(
NotificationRecipient::Admin,
&make_expring_message(expire_in_days),
)
.await?;
.await

Check warning on line 42 in ee/tabby-webserver/src/service/background_job/license_check.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/license_check.rs#L42

Added line #L42 was not covered by tests
{
logkit::warn!("Failed to create notification: {}", e);
return Err(e);
}
}
}

Ok(())

Check warning on line 50 in ee/tabby-webserver/src/service/background_job/license_check.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/license_check.rs#L44-L50

Added lines #L44 - L50 were not covered by tests
}
}

Expand Down
Loading
Loading