Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
77fccd8b
Unverified
Commit
77fccd8b
authored
10 months ago
by
Ngoc Khuat
Committed by
GitHub
10 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Quartz: auto delete jobs without a class during scheduler initialization (#42383)
parent
e8dc5ec5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/task.clj
+14
-1
14 additions, 1 deletion
src/metabase/task.clj
test/metabase/task_test.clj
+35
-1
35 additions, 1 deletion
test/metabase/task_test.clj
with
49 additions
and
2 deletions
src/metabase/task.clj
+
14
−
1
View file @
77fccd8b
...
...
@@ -22,7 +22,7 @@
[
metabase.util.malli
:as
mu
]
[
metabase.util.malli.schema
:as
ms
])
(
:import
(
org.quartz
CronTrigger
JobDetail
JobKey
Scheduler
Trigger
TriggerKey
)))
(
org.quartz
CronTrigger
JobDetail
JobKey
JobPersistenceException
Scheduler
Trigger
TriggerKey
)))
(
set!
*warn-on-reflection*
true
)
...
...
@@ -141,6 +141,18 @@
(
when
(
=
(
mdb/db-type
)
:postgres
)
(
System/setProperty
"org.quartz.jobStore.driverDelegateClass"
"org.quartz.impl.jdbcjobstore.PostgreSQLDelegate"
)))
(
defn-
delete-jobs-with-no-class!
"Delete any jobs that have been scheduled but whose class is no longer available."
[]
(
when-let
[
scheduler
(
scheduler
)]
(
doseq
[
job-key
(
.getJobKeys
scheduler
nil
)]
(
try
(
qs/get-job
scheduler
job-key
)
(
catch
JobPersistenceException
e
(
when
(
instance?
ClassNotFoundException
(
.getCause
e
))
(
log/infof
"Deleting job %s due to class not found"
(
.getName
^
JobKey
job-key
))
(
qs/delete-job
scheduler
job-key
)))))))
(
defn-
init-scheduler!
"Initialize our Quartzite scheduler which allows jobs to be submitted and triggers to scheduled. Puts scheduler in
standby mode. Call [[start-scheduler!]] to begin running scheduled tasks."
...
...
@@ -153,6 +165,7 @@
(
find-and-load-task-namespaces!
)
(
qs/standby
new-scheduler
)
(
log/info
"Task scheduler initialized into standby mode."
)
(
delete-jobs-with-no-class!
)
(
init-tasks!
)))))
;;; this is a function mostly to facilitate testing.
...
...
This diff is collapsed.
Click to expand it.
test/metabase/task_test.clj
+
35
−
1
View file @
77fccd8b
...
...
@@ -5,11 +5,14 @@
[
clojurewerkz.quartzite.schedule.cron
:as
cron
]
[
clojurewerkz.quartzite.scheduler
:as
qs
]
[
clojurewerkz.quartzite.triggers
:as
triggers
]
[
metabase.db.connection
:as
mdb.connection
]
[
metabase.task
:as
task
]
[
metabase.test
:as
mt
]
[
metabase.test.fixtures
:as
fixtures
]
[
metabase.test.util
:as
tu
]
[
metabase.util.malli.schema
:as
ms
])
[
metabase.util
:as
u
]
[
metabase.util.malli.schema
:as
ms
]
[
toucan2.core
:as
t2
])
(
:import
(
org.quartz
CronTrigger
JobDetail
)))
...
...
@@ -121,3 +124,34 @@
(
mt/with-temp-env-var-value!
[
"MB_DISABLE_SCHEDULER"
"FALSE"
]
(
task/start-scheduler!
)
(
is
(
qs/started?
(
#
'task/scheduler
))))))))
(
defn-
capitalize-if-mysql
[
s
]
(
cond->
(
name
s
)
(
=
:mysql
(
mdb.connection/db-type
))
u/upper-case-en
true
keyword
))
(
deftest
start-scheduler-will-cleanup-jobs-without-class-test
;; we can't use the temp scheduler in this test because the temp scheduler use an in-memory jobstore
;; and we need update the job class in the database to trigger the cleanup
(
let
[
scheduler-initialized?
(
some?
(
#
'task/scheduler
))]
(
try
(
when-not
scheduler-initialized?
(
task/start-scheduler!
))
(
task/schedule-task!
(
job
)
(
trigger-1
))
(
testing
"make sure the job is in the database before we start the scheduler"
(
is
(
t2/exists?
(
capitalize-if-mysql
:qrtz_job_details
)
(
capitalize-if-mysql
:job_name
)
"metabase.task-test.job"
)))
;; update the job class to a non-existent class
(
t2/update!
(
capitalize-if-mysql
:qrtz_job_details
)
(
capitalize-if-mysql
:job_name
)
"metabase.task-test.job"
{(
capitalize-if-mysql
:job_class_name
)
"NOT_A_REAL_CLASS"
})
;; stop the scheduler then restart so [[task/delete-jobs-with-no-class!]] is triggered
(
task/stop-scheduler!
)
(
task/start-scheduler!
)
(
testing
"the job should be removed from the database when the scheduler starts"
(
is
(
not
(
t2/exists?
(
capitalize-if-mysql
:qrtz_job_details
)
(
capitalize-if-mysql
:job_name
)
"metabase.task-test.job"
))))
(
finally
;; restore the state of scheduler before we start the test
(
if
scheduler-initialized?
(
task/start-scheduler!
)
(
task/stop-scheduler!
))))))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment