How do I stop a query from running if the row is locked?
you can use GET_LOCK() to avoid the query to run if other query is running on the same lock name.

you will basically do:

SELECT IS_FREE_LOCK('maintenance');
#IF RESULT IS 1
SELECT GET_LOCK('maintenance', '10');#it will get a lock named maintenance
#PERFORM ALL YOUR MAINTENANCE
SELECT RELEASE_LOCK('maintenance'); #it will release a lock named maintence


what will happen is that the thread 1 will get a lock named maintenance, while this thread don't release this lock, the command SELECT IS_FREE_LOCK('maintenance'); won't return 1.