6e0519a232
- Implemented a new BrandAssetCleanupWorker to handle the cleanup of brand-related assets after a brand is deleted. - Added SQL queries for cleaning up articles, keywords, questions, competitors, and monitoring data associated with a brand. - Introduced a new API endpoint to delete publish records. - Updated the router to include the new delete publish record endpoint. - Added tests for the BrandAssetCleanupWorker to ensure proper functionality. - Created migration scripts to support soft deletion of publish records and to add the brand asset cleanup scheduler job.
988 lines
27 KiB
Go
988 lines
27 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: desktop_task.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const cancelDesktopTaskByClient = `-- name: CancelDesktopTaskByClient :one
|
|
UPDATE desktop_tasks
|
|
SET status = 'aborted',
|
|
error = $1,
|
|
active_attempt_id = NULL,
|
|
lease_token_hash = NULL,
|
|
lease_expires_at = NULL,
|
|
updated_at = now()
|
|
WHERE desktop_id = $2
|
|
AND target_client_id = $3
|
|
AND status = 'queued'
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type CancelDesktopTaskByClientParams struct {
|
|
Error []byte `json:"error"`
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
ClientID pgtype.UUID `json:"client_id"`
|
|
}
|
|
|
|
func (q *Queries) CancelDesktopTaskByClient(ctx context.Context, arg CancelDesktopTaskByClientParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, cancelDesktopTaskByClient, arg.Error, arg.DesktopID, arg.ClientID)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const cancelDesktopTaskByLease = `-- name: CancelDesktopTaskByLease :one
|
|
UPDATE desktop_tasks
|
|
SET status = 'aborted',
|
|
error = $1,
|
|
active_attempt_id = NULL,
|
|
lease_token_hash = NULL,
|
|
lease_expires_at = NULL,
|
|
updated_at = now()
|
|
WHERE desktop_id = $2
|
|
AND lease_token_hash = $3
|
|
AND status = 'in_progress'
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type CancelDesktopTaskByLeaseParams struct {
|
|
Error []byte `json:"error"`
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
LeaseTokenHash []byte `json:"lease_token_hash"`
|
|
}
|
|
|
|
func (q *Queries) CancelDesktopTaskByLease(ctx context.Context, arg CancelDesktopTaskByLeaseParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, cancelDesktopTaskByLease, arg.Error, arg.DesktopID, arg.LeaseTokenHash)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const cancelQueuedPublishDesktopTaskByOwner = `-- name: CancelQueuedPublishDesktopTaskByOwner :one
|
|
UPDATE desktop_tasks AS t
|
|
SET status = 'aborted',
|
|
error = $1,
|
|
active_attempt_id = NULL,
|
|
lease_token_hash = NULL,
|
|
lease_expires_at = NULL,
|
|
updated_at = now()
|
|
FROM desktop_publish_jobs AS j
|
|
WHERE t.desktop_id = $2
|
|
AND t.tenant_id = $3
|
|
AND t.workspace_id = $4
|
|
AND t.kind = 'publish'
|
|
AND t.status = 'queued'
|
|
AND j.desktop_id = t.job_id
|
|
AND j.tenant_id = t.tenant_id
|
|
AND j.workspace_id = t.workspace_id
|
|
AND j.created_by_user_id = $5
|
|
RETURNING t.id, t.desktop_id, t.job_id, t.tenant_id, t.workspace_id, t.target_account_id, t.target_client_id, t.platform_id, t.kind, t.payload, t.status, t.dedup_key, t.active_attempt_id, t.lease_token_hash, t.lease_expires_at, t.attempts, t.result, t.error, t.created_at, t.updated_at, t.priority, t.lane, t.lane_weight, t.source, t.scheduler_group_key, t.monitor_task_id, t.supersedes_task_id, t.control_flags, t.interrupt_generation, t.started_at, t.interrupted_at, t.interrupt_reason, t.enqueued_at, t.publish_submit_started_at
|
|
`
|
|
|
|
type CancelQueuedPublishDesktopTaskByOwnerParams struct {
|
|
Error []byte `json:"error"`
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
WorkspaceID int64 `json:"workspace_id"`
|
|
UserID int64 `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) CancelQueuedPublishDesktopTaskByOwner(ctx context.Context, arg CancelQueuedPublishDesktopTaskByOwnerParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, cancelQueuedPublishDesktopTaskByOwner,
|
|
arg.Error,
|
|
arg.DesktopID,
|
|
arg.TenantID,
|
|
arg.WorkspaceID,
|
|
arg.UserID,
|
|
)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const completeDesktopTask = `-- name: CompleteDesktopTask :one
|
|
UPDATE desktop_tasks
|
|
SET status = $1,
|
|
result = $2,
|
|
error = $3,
|
|
active_attempt_id = NULL,
|
|
lease_token_hash = NULL,
|
|
lease_expires_at = NULL,
|
|
updated_at = now()
|
|
WHERE desktop_id = $4
|
|
AND lease_token_hash = $5
|
|
AND status = 'in_progress'
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type CompleteDesktopTaskParams struct {
|
|
Status string `json:"status"`
|
|
Result []byte `json:"result"`
|
|
Error []byte `json:"error"`
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
LeaseTokenHash []byte `json:"lease_token_hash"`
|
|
}
|
|
|
|
func (q *Queries) CompleteDesktopTask(ctx context.Context, arg CompleteDesktopTaskParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, completeDesktopTask,
|
|
arg.Status,
|
|
arg.Result,
|
|
arg.Error,
|
|
arg.DesktopID,
|
|
arg.LeaseTokenHash,
|
|
)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createDesktopPublishJob = `-- name: CreateDesktopPublishJob :one
|
|
INSERT INTO desktop_publish_jobs (
|
|
desktop_id,
|
|
tenant_id,
|
|
workspace_id,
|
|
created_by_user_id,
|
|
title,
|
|
content_ref,
|
|
scheduled_at,
|
|
article_id,
|
|
article_version_id
|
|
)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9
|
|
)
|
|
RETURNING id, desktop_id, tenant_id, workspace_id, created_by_user_id, title, content_ref, scheduled_at, created_at, updated_at, article_id, article_version_id, status, compliance_blocked_record_id, compliance_blocked_at, compliance_blocked_reason
|
|
`
|
|
|
|
type CreateDesktopPublishJobParams struct {
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
WorkspaceID int64 `json:"workspace_id"`
|
|
CreatedByUserID int64 `json:"created_by_user_id"`
|
|
Title string `json:"title"`
|
|
ContentRef []byte `json:"content_ref"`
|
|
ScheduledAt pgtype.Timestamptz `json:"scheduled_at"`
|
|
ArticleID pgtype.Int8 `json:"article_id"`
|
|
ArticleVersionID pgtype.Int8 `json:"article_version_id"`
|
|
}
|
|
|
|
func (q *Queries) CreateDesktopPublishJob(ctx context.Context, arg CreateDesktopPublishJobParams) (DesktopPublishJob, error) {
|
|
row := q.db.QueryRow(ctx, createDesktopPublishJob,
|
|
arg.DesktopID,
|
|
arg.TenantID,
|
|
arg.WorkspaceID,
|
|
arg.CreatedByUserID,
|
|
arg.Title,
|
|
arg.ContentRef,
|
|
arg.ScheduledAt,
|
|
arg.ArticleID,
|
|
arg.ArticleVersionID,
|
|
)
|
|
var i DesktopPublishJob
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.CreatedByUserID,
|
|
&i.Title,
|
|
&i.ContentRef,
|
|
&i.ScheduledAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ArticleID,
|
|
&i.ArticleVersionID,
|
|
&i.Status,
|
|
&i.ComplianceBlockedRecordID,
|
|
&i.ComplianceBlockedAt,
|
|
&i.ComplianceBlockedReason,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createDesktopTask = `-- name: CreateDesktopTask :one
|
|
INSERT INTO desktop_tasks (
|
|
desktop_id,
|
|
job_id,
|
|
tenant_id,
|
|
workspace_id,
|
|
target_account_id,
|
|
target_client_id,
|
|
platform_id,
|
|
kind,
|
|
payload,
|
|
status,
|
|
dedup_key
|
|
)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10,
|
|
$11
|
|
)
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type CreateDesktopTaskParams struct {
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
JobID pgtype.UUID `json:"job_id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
WorkspaceID int64 `json:"workspace_id"`
|
|
TargetAccountID pgtype.UUID `json:"target_account_id"`
|
|
TargetClientID pgtype.UUID `json:"target_client_id"`
|
|
PlatformID string `json:"platform_id"`
|
|
Kind string `json:"kind"`
|
|
Payload []byte `json:"payload"`
|
|
Status string `json:"status"`
|
|
DedupKey pgtype.Text `json:"dedup_key"`
|
|
}
|
|
|
|
func (q *Queries) CreateDesktopTask(ctx context.Context, arg CreateDesktopTaskParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, createDesktopTask,
|
|
arg.DesktopID,
|
|
arg.JobID,
|
|
arg.TenantID,
|
|
arg.WorkspaceID,
|
|
arg.TargetAccountID,
|
|
arg.TargetClientID,
|
|
arg.PlatformID,
|
|
arg.Kind,
|
|
arg.Payload,
|
|
arg.Status,
|
|
arg.DedupKey,
|
|
)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createDesktopTaskAttempt = `-- name: CreateDesktopTaskAttempt :one
|
|
INSERT INTO desktop_task_attempts (
|
|
desktop_id,
|
|
task_id,
|
|
client_id,
|
|
lease_token_hash
|
|
)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4
|
|
)
|
|
RETURNING id, desktop_id, task_id, client_id, lease_token_hash, started_at, ended_at, final_status, error, created_at
|
|
`
|
|
|
|
type CreateDesktopTaskAttemptParams struct {
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
TaskID pgtype.UUID `json:"task_id"`
|
|
ClientID pgtype.UUID `json:"client_id"`
|
|
LeaseTokenHash []byte `json:"lease_token_hash"`
|
|
}
|
|
|
|
func (q *Queries) CreateDesktopTaskAttempt(ctx context.Context, arg CreateDesktopTaskAttemptParams) (DesktopTaskAttempt, error) {
|
|
row := q.db.QueryRow(ctx, createDesktopTaskAttempt,
|
|
arg.DesktopID,
|
|
arg.TaskID,
|
|
arg.ClientID,
|
|
arg.LeaseTokenHash,
|
|
)
|
|
var i DesktopTaskAttempt
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.TaskID,
|
|
&i.ClientID,
|
|
&i.LeaseTokenHash,
|
|
&i.StartedAt,
|
|
&i.EndedAt,
|
|
&i.FinalStatus,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const extendDesktopTaskLease = `-- name: ExtendDesktopTaskLease :one
|
|
UPDATE desktop_tasks
|
|
SET lease_expires_at = now() + CASE WHEN kind = 'publish' THEN interval '3 minutes' ELSE interval '10 minutes' END,
|
|
updated_at = now()
|
|
WHERE desktop_id = $1
|
|
AND lease_token_hash = $2
|
|
AND status = 'in_progress'
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type ExtendDesktopTaskLeaseParams struct {
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
LeaseTokenHash []byte `json:"lease_token_hash"`
|
|
}
|
|
|
|
func (q *Queries) ExtendDesktopTaskLease(ctx context.Context, arg ExtendDesktopTaskLeaseParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, extendDesktopTaskLease, arg.DesktopID, arg.LeaseTokenHash)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const finishDesktopTaskAttempt = `-- name: FinishDesktopTaskAttempt :exec
|
|
UPDATE desktop_task_attempts
|
|
SET ended_at = now(),
|
|
final_status = $1,
|
|
error = $2
|
|
WHERE task_id = $3
|
|
AND desktop_id = $4
|
|
`
|
|
|
|
type FinishDesktopTaskAttemptParams struct {
|
|
FinalStatus pgtype.Text `json:"final_status"`
|
|
Error []byte `json:"error"`
|
|
TaskID pgtype.UUID `json:"task_id"`
|
|
AttemptID pgtype.UUID `json:"attempt_id"`
|
|
}
|
|
|
|
func (q *Queries) FinishDesktopTaskAttempt(ctx context.Context, arg FinishDesktopTaskAttemptParams) error {
|
|
_, err := q.db.Exec(ctx, finishDesktopTaskAttempt,
|
|
arg.FinalStatus,
|
|
arg.Error,
|
|
arg.TaskID,
|
|
arg.AttemptID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const getDesktopTaskByDesktopID = `-- name: GetDesktopTaskByDesktopID :one
|
|
SELECT id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
FROM desktop_tasks
|
|
WHERE desktop_id = $1
|
|
AND workspace_id = $2
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetDesktopTaskByDesktopIDParams struct {
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
WorkspaceID int64 `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) GetDesktopTaskByDesktopID(ctx context.Context, arg GetDesktopTaskByDesktopIDParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, getDesktopTaskByDesktopID, arg.DesktopID, arg.WorkspaceID)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const leaseNextQueuedDesktopTask = `-- name: LeaseNextQueuedDesktopTask :one
|
|
WITH candidate AS (
|
|
SELECT dt.desktop_id
|
|
FROM desktop_tasks AS dt
|
|
WHERE dt.target_client_id = $3
|
|
AND dt.status = 'queued'
|
|
AND (
|
|
$4::text IS NULL
|
|
OR dt.kind = $4::text
|
|
)
|
|
AND (
|
|
dt.kind <> 'publish'
|
|
OR dt.attempts < 3
|
|
)
|
|
AND (
|
|
dt.kind <> 'publish'
|
|
OR NOT EXISTS (
|
|
SELECT 1
|
|
FROM desktop_publish_jobs AS j
|
|
WHERE j.desktop_id = dt.job_id
|
|
AND j.status <> 'queued'
|
|
)
|
|
)
|
|
ORDER BY dt.lane_weight DESC,
|
|
dt.priority DESC,
|
|
COALESCE(dt.enqueued_at, dt.created_at) ASC,
|
|
dt.created_at ASC,
|
|
dt.desktop_id ASC
|
|
LIMIT 1
|
|
FOR UPDATE OF dt SKIP LOCKED
|
|
)
|
|
UPDATE desktop_tasks AS t
|
|
SET active_attempt_id = $1,
|
|
lease_token_hash = $2,
|
|
lease_expires_at = now() + CASE WHEN t.kind = 'publish' THEN interval '3 minutes' ELSE interval '10 minutes' END,
|
|
status = 'in_progress',
|
|
attempts = t.attempts + 1,
|
|
started_at = COALESCE(t.started_at, now()),
|
|
updated_at = now()
|
|
FROM candidate
|
|
WHERE t.desktop_id = candidate.desktop_id
|
|
RETURNING t.id, t.desktop_id, t.job_id, t.tenant_id, t.workspace_id, t.target_account_id, t.target_client_id, t.platform_id, t.kind, t.payload, t.status, t.dedup_key, t.active_attempt_id, t.lease_token_hash, t.lease_expires_at, t.attempts, t.result, t.error, t.created_at, t.updated_at, t.priority, t.lane, t.lane_weight, t.source, t.scheduler_group_key, t.monitor_task_id, t.supersedes_task_id, t.control_flags, t.interrupt_generation, t.started_at, t.interrupted_at, t.interrupt_reason, t.enqueued_at, t.publish_submit_started_at
|
|
`
|
|
|
|
type LeaseNextQueuedDesktopTaskParams struct {
|
|
AttemptID pgtype.UUID `json:"attempt_id"`
|
|
LeaseTokenHash []byte `json:"lease_token_hash"`
|
|
ClientID pgtype.UUID `json:"client_id"`
|
|
Kind pgtype.Text `json:"kind"`
|
|
}
|
|
|
|
func (q *Queries) LeaseNextQueuedDesktopTask(ctx context.Context, arg LeaseNextQueuedDesktopTaskParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, leaseNextQueuedDesktopTask,
|
|
arg.AttemptID,
|
|
arg.LeaseTokenHash,
|
|
arg.ClientID,
|
|
arg.Kind,
|
|
)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const leaseQueuedDesktopTaskByDesktopID = `-- name: LeaseQueuedDesktopTaskByDesktopID :one
|
|
UPDATE desktop_tasks
|
|
SET active_attempt_id = $1,
|
|
lease_token_hash = $2,
|
|
lease_expires_at = now() + CASE WHEN kind = 'publish' THEN interval '3 minutes' ELSE interval '10 minutes' END,
|
|
status = 'in_progress',
|
|
attempts = attempts + 1,
|
|
started_at = COALESCE(started_at, now()),
|
|
updated_at = now()
|
|
WHERE desktop_id = $3
|
|
AND target_client_id = $4
|
|
AND status = 'queued'
|
|
AND (
|
|
kind <> 'publish'
|
|
OR attempts < 3
|
|
)
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type LeaseQueuedDesktopTaskByDesktopIDParams struct {
|
|
AttemptID pgtype.UUID `json:"attempt_id"`
|
|
LeaseTokenHash []byte `json:"lease_token_hash"`
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
ClientID pgtype.UUID `json:"client_id"`
|
|
}
|
|
|
|
func (q *Queries) LeaseQueuedDesktopTaskByDesktopID(ctx context.Context, arg LeaseQueuedDesktopTaskByDesktopIDParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, leaseQueuedDesktopTaskByDesktopID,
|
|
arg.AttemptID,
|
|
arg.LeaseTokenHash,
|
|
arg.DesktopID,
|
|
arg.ClientID,
|
|
)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const markDesktopTasksUnknownByClient = `-- name: MarkDesktopTasksUnknownByClient :execrows
|
|
UPDATE desktop_tasks
|
|
SET status = 'unknown',
|
|
error = $1,
|
|
active_attempt_id = NULL,
|
|
lease_token_hash = NULL,
|
|
lease_expires_at = NULL,
|
|
updated_at = now()
|
|
WHERE target_client_id = $2
|
|
AND workspace_id = $3
|
|
AND status = 'in_progress'
|
|
`
|
|
|
|
type MarkDesktopTasksUnknownByClientParams struct {
|
|
Error []byte `json:"error"`
|
|
ClientID pgtype.UUID `json:"client_id"`
|
|
WorkspaceID int64 `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) MarkDesktopTasksUnknownByClient(ctx context.Context, arg MarkDesktopTasksUnknownByClientParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, markDesktopTasksUnknownByClient, arg.Error, arg.ClientID, arg.WorkspaceID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const reconcileDesktopTask = `-- name: ReconcileDesktopTask :one
|
|
UPDATE desktop_tasks
|
|
SET status = CASE
|
|
WHEN $1::text = 'retry' THEN 'queued'
|
|
ELSE $1::text
|
|
END,
|
|
result = CASE
|
|
WHEN $1::text = 'retry' THEN NULL
|
|
ELSE $2
|
|
END,
|
|
error = $3,
|
|
active_attempt_id = NULL,
|
|
lease_token_hash = NULL,
|
|
lease_expires_at = NULL,
|
|
attempts = CASE WHEN $1::text = 'retry' THEN 0 ELSE attempts END,
|
|
updated_at = now()
|
|
WHERE desktop_id = $4
|
|
AND workspace_id = $5
|
|
AND status = 'unknown'
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type ReconcileDesktopTaskParams struct {
|
|
Status string `json:"status"`
|
|
Result []byte `json:"result"`
|
|
Error []byte `json:"error"`
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
WorkspaceID int64 `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) ReconcileDesktopTask(ctx context.Context, arg ReconcileDesktopTaskParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, reconcileDesktopTask,
|
|
arg.Status,
|
|
arg.Result,
|
|
arg.Error,
|
|
arg.DesktopID,
|
|
arg.WorkspaceID,
|
|
)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const tenantCancelDesktopTask = `-- name: TenantCancelDesktopTask :one
|
|
UPDATE desktop_tasks
|
|
SET status = 'aborted',
|
|
error = $1,
|
|
active_attempt_id = NULL,
|
|
lease_token_hash = NULL,
|
|
lease_expires_at = NULL,
|
|
updated_at = now()
|
|
WHERE desktop_id = $2
|
|
AND tenant_id = $3
|
|
AND workspace_id = $4
|
|
AND status = 'queued'
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM desktop_publish_jobs AS j
|
|
WHERE j.desktop_id = desktop_tasks.job_id
|
|
AND j.tenant_id = desktop_tasks.tenant_id
|
|
AND j.workspace_id = desktop_tasks.workspace_id
|
|
AND j.created_by_user_id = $5
|
|
)
|
|
RETURNING id, desktop_id, job_id, tenant_id, workspace_id, target_account_id, target_client_id, platform_id, kind, payload, status, dedup_key, active_attempt_id, lease_token_hash, lease_expires_at, attempts, result, error, created_at, updated_at, priority, lane, lane_weight, source, scheduler_group_key, monitor_task_id, supersedes_task_id, control_flags, interrupt_generation, started_at, interrupted_at, interrupt_reason, enqueued_at, publish_submit_started_at
|
|
`
|
|
|
|
type TenantCancelDesktopTaskParams struct {
|
|
Error []byte `json:"error"`
|
|
DesktopID pgtype.UUID `json:"desktop_id"`
|
|
TenantID int64 `json:"tenant_id"`
|
|
WorkspaceID int64 `json:"workspace_id"`
|
|
UserID int64 `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) TenantCancelDesktopTask(ctx context.Context, arg TenantCancelDesktopTaskParams) (DesktopTask, error) {
|
|
row := q.db.QueryRow(ctx, tenantCancelDesktopTask, arg.Error, arg.DesktopID, arg.TenantID, arg.WorkspaceID, arg.UserID)
|
|
var i DesktopTask
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DesktopID,
|
|
&i.JobID,
|
|
&i.TenantID,
|
|
&i.WorkspaceID,
|
|
&i.TargetAccountID,
|
|
&i.TargetClientID,
|
|
&i.PlatformID,
|
|
&i.Kind,
|
|
&i.Payload,
|
|
&i.Status,
|
|
&i.DedupKey,
|
|
&i.ActiveAttemptID,
|
|
&i.LeaseTokenHash,
|
|
&i.LeaseExpiresAt,
|
|
&i.Attempts,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Priority,
|
|
&i.Lane,
|
|
&i.LaneWeight,
|
|
&i.Source,
|
|
&i.SchedulerGroupKey,
|
|
&i.MonitorTaskID,
|
|
&i.SupersedesTaskID,
|
|
&i.ControlFlags,
|
|
&i.InterruptGeneration,
|
|
&i.StartedAt,
|
|
&i.InterruptedAt,
|
|
&i.InterruptReason,
|
|
&i.EnqueuedAt,
|
|
&i.PublishSubmitStartedAt,
|
|
)
|
|
return i, err
|
|
}
|