Repository: quarkusio/quarkus
Issue: Quarkus 2.9.0.CR1: Async Scheduled methods have unsafe sub-context

### Describe the bug

When using an async `@Scheduled` method returning a void or using Kotlin coroutines, the provided sub-context seems to be unsafe and throws an error when e.g. using hibernate reactive with it. 

E.g. using a code like

```
class TestScheduler(private val testDao: TestDao) {

    @Scheduled(every = "5s")
    fun every5s(): Uni<Void> {
        return testDao.getEntity(UUID.randomUUID()).replaceWithVoid()
    }
}

@ApplicationScoped
class TestDao(private val sessionFactory: Mutiny.SessionFactory) {
    fun getEntity(id: UUID): Uni<TestEntity?> = sessionFactory.withTransaction { s, _ ->
        s.find(TestEntity::class.java, id)
    }
}

```

ends in a crash like

```
2022-04-29 15:32:05,016 ERROR [io.qua.sch.com.run.StatusEmitterInvoker] (vert.x-eventloop-thread-0) Error occured while executing task for trigger IntervalTrigger [id=1_org.acme.TestScheduler_ScheduledInvoker_every5s_c56c24f01b03a432de6e9b18de0f5a7dfbc05db8, interval=5000]: java.lang.IllegalStateException: The current operation requires a safe (isolated) Vert.x sub-context, but the current context hasn't been flagged as such. You can still use Hibernate Reactive, you just need to avoid using the methods which implicitly require accessing the stateful context, such as MutinySessionFactory#withTransaction and #withSession.
	at io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.checkIsSafe(VertxContextSafetyToggle.java:80)
	at io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.validateContextIfExists(VertxContextSafetyToggle.java:63)
	at io.quarkus.hibernate.reactive.runtime.customized.CheckingVertxContext.get(CheckingVertxContext.java:46)
	at org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl.withSession(MutinySessionFactoryImpl.java:189)
	at org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl.withTransaction(MutinySessionFactoryImpl.java:261)
	at org.acme.db.TestDao.getEntity(TestDao.kt:12)
	at org.acme.db.TestDao_Subclass.getEntity$$superforward1(Unknown Source)
	at org.acme.db.TestDao_Subclass$$function$$1.apply(Unknown Source)
	at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
	at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.proceed(InvocationInterceptor.java:62)
```


### Expected behavior

@Scheduled should have a safe context that can be used with hibernate reactive.

### Actual behavior

It crashes

### How to Reproduce?

1. Download reproducer project and unzip:
[2022-04-29_scheduler-unsafe.zip](https://github.com/quarkusio/quarkus/files/8591125/2022-04-29_scheduler-unsafe.zip)
2. run the included `docker-compose.yml` to set up a local postgres container. 
3. run with `./gradlew quarkusDev`
4. check the log for the crash

### Output of `uname -a` or `ver`

_No response_

### Output of `java -version`

_No response_

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

2.9.0.CR1

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

gradle

### Additional information

_No response_

Changed files:
extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java
extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java

diff --git a/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java b/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java
index d04ae8ab00a..ec68f58ae00 100644
--- a/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java
+++ b/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java
@@ -73,7 +73,9 @@
 import io.quarkus.scheduler.common.runtime.StatusEmitterInvoker;
 import io.quarkus.scheduler.common.runtime.util.SchedulerUtils;
 import io.quarkus.scheduler.runtime.SchedulerRuntimeConfig;
+import io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle;
 import io.smallrye.common.vertx.VertxContext;
+import io.vertx.core.Context;
 import io.vertx.core.Handler;
 import io.vertx.core.Vertx;
 
@@ -555,20 +557,22 @@ static class InvokerJob implements Job {
         }
 
         @Override
-        public void execute(JobExecutionContext context) throws JobExecutionException {
+        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
             if (trigger.invoker != null) { // could be null from previous runs
                 if (trigger.invoker.isBlocking()) {
                     try {
-                        trigger.invoker.invoke(new QuartzScheduledExecution(trigger, context));
+                        trigger.invoker.invoke(new QuartzScheduledExecution(trigger, jobExecutionContext));
                     } catch (Exception e) {
                         throw new JobExecutionException(e);
                     }
                 } else {
-                    VertxContext.getOrCreateDuplicatedContext(vertx).runOnContext(new Handler<Void>() {
+                    Context context = VertxContext.getOrCreateDuplicatedContext(vertx);
+                    VertxContextSafetyToggle.setContextSafe(context, true);
+                    context.runOnContext(new Handler<Void>() {
                         @Override
                         public void handle(Void event) {
                             try {
-                                trigger.invoker.invoke(new QuartzScheduledExecution(trigger, context));
+                                trigger.invoker.invoke(new QuartzScheduledExecution(trigger, jobExecutionContext));
                             } catch (Exception e) {
                                 // already logged by the StatusEmitterInvoker
                             }
diff --git a/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java b/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java
index 15adc72b52b..40a2bf2af9d 100644
--- a/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java
+++ b/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java
@@ -51,6 +51,7 @@
 import io.quarkus.scheduler.common.runtime.SkipPredicateInvoker;
 import io.quarkus.scheduler.common.runtime.StatusEmitterInvoker;
 import io.quarkus.scheduler.common.runtime.util.SchedulerUtils;
+import io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle;
 import io.smallrye.common.vertx.VertxContext;
 import io.vertx.core.Context;
 import io.vertx.core.Handler;
@@ -319,6 +320,7 @@ public void run() {
                     }
                 } else {
                     Context context = VertxContext.getOrCreateDuplicatedContext(vertx);
+                    VertxContextSafetyToggle.setContextSafe(context, true);
                     context.runOnContext(new Handler<Void>() {
                         @Override
                         public void handle(Void event) {