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(job): send a notification when job fails #3713

Merged
merged 20 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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 @@ impl DbMaintainanceJob {
context: Arc<dyn ContextService>,
db: DbConn,
) -> tabby_schema::Result<()> {
let mut errors = vec![];
let mut has_error = false;

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);
};
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);
};
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);
};

// Read all active sources
Expand All @@ -44,54 +47,57 @@ impl DbMaintainanceJob {
.delete_unused_source_id_read_access_policy(&active_source_ids)
.await
{
errors.push(format!(
has_error = true;
logkit::warn!(
"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);
}
}

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);
}

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

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!(
"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!(
"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"
)))
}
}
Expand Down
15 changes: 14 additions & 1 deletion ee/tabby-webserver/src/service/background_job/hourly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,24 @@ impl HourlyJob {
repository_service: Arc<dyn RepositoryService>,
) -> tabby_schema::Result<()> {
let now = Utc::now();
let mut has_error = false;

if let Err(err) = DbMaintainanceJob::cron(now, context_service.clone(), db.clone()).await {
has_error = true;
logkit::warn!("Database maintainance failed: {:?}", err);
}

if let Err(err) =
SchedulerGitJob::cron(now, git_repository_service.clone(), job_service.clone()).await
{
has_error = true;
logkit::warn!("Scheduler job failed: {:?}", err);
}

if let Err(err) =
SyncIntegrationJob::cron(now, integration_service.clone(), job_service.clone()).await
{
has_error = true;
logkit::warn!("Sync integration job failed: {:?}", err);
}

Expand All @@ -59,15 +63,24 @@ impl HourlyJob {
)
.await
{
has_error = true;
logkit::warn!("Index issues job failed: {err:?}");
}

if let Err(err) = IndexGarbageCollection
.run(repository_service.clone(), context_service.clone())
.await
{
has_error = true;
logkit::warn!("Index garbage collection job failed: {err:?}");
}
Ok(())

if has_error {
Err(tabby_schema::CoreError::Other(anyhow::anyhow!(
"Hourly job failed"
)))
} else {
Ok(())
}
}
}
51 changes: 48 additions & 3 deletions ee/tabby-webserver/src/service/background_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ use tabby_schema::{
integration::IntegrationService,
job::JobService,
license::LicenseService,
notification::NotificationService,
notification::{NotificationRecipient, NotificationService},
repository::{GitRepositoryService, RepositoryService, ThirdPartyRepositoryService},
AsID,
};
use third_party_integration::SchedulerGithubGitlabJob;
use tracing::{debug, warn};
pub use web_crawler::WebCrawlerJob;

use self::third_party_integration::SyncIntegrationJob;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum BackgroundJobEvent {
SchedulerGitRepository(CodeRepository),
SchedulerGithubGitlabRepository(ID),
Expand Down Expand Up @@ -68,6 +69,48 @@ impl BackgroundJobEvent {
}
}

fn background_job_notification_name(event: &BackgroundJobEvent) -> &str {
match event {
BackgroundJobEvent::SchedulerGitRepository(_) => "Git repository synchronize",
BackgroundJobEvent::SchedulerGithubGitlabRepository(_) => {
"Github/Gitlab repository synchronize"
}
BackgroundJobEvent::SyncThirdPartyRepositories(_) => "Integration synchronize",
BackgroundJobEvent::WebCrawler(_) => "Web crawler",
BackgroundJobEvent::IndexGarbageCollection => "Index garbage collection",
BackgroundJobEvent::Hourly => "Hourly",
BackgroundJobEvent::Daily => "Daily",
wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved
}
}

async fn notify_job_error(
notification_service: Arc<dyn NotificationService>,
err: &str,
event: &BackgroundJobEvent,
id: i64,
) {
warn!("job {:?} failed: {:?}", event, err);
let name = background_job_notification_name(event);
if let Err(err) = notification_service
.create(
NotificationRecipient::Admin,
&format!(
r#"Background job failed

Job `{}` has failed.
wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved

Please check the log at [Jobs Detail](/jobs/detail?id={}) to identify the underlying issue.
"#,
name,
id.as_id()
),
)
.await
{
warn!("Failed to send notification: {:?}", err);
}
}

pub async fn start(
db: DbConn,
job_service: Arc<dyn JobService>,
Expand Down Expand Up @@ -108,6 +151,7 @@ pub async fn start(
continue;
};

let cloned_event = event.clone();
if let Err(err) = match event {
BackgroundJobEvent::SchedulerGitRepository(repository_config) => {
let job = SchedulerGitJob::new(repository_config);
Expand Down Expand Up @@ -148,7 +192,8 @@ pub async fn start(
).await
}
} {
logkit::info!(exit_code = 1; "Job failed {}", err);
logkit::warn!(exit_code = 1; "Job failed: {}", err);
wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved
notify_job_error(notification_service.clone(), &err.to_string(), &cloned_event, job.id).await;
} else {
logkit::info!(exit_code = 0; "Job completed successfully");
}
Expand Down
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl SyncIntegrationJob {
)
.await;
}

wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::helper::Job;

const CRAWLER_TIMEOUT_SECS: u64 = 7200;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved
pub struct WebCrawlerJob {
source_id: String,
url: String,
Expand Down
Loading