Is it possible to queue a queueable apex job from a scheduled apex job?
I can confirm that this works - I have just created these classes and executed them and they successfully ran.
For reference, here is the exact code:
Schedulable:
global class X_DeleteMeSchedulable implements Schedulable {
global void execute(SchedulableContext sc) {
System.enqueueJob(new X_DeleteMeQueuable());
}
}
Queueable:
global without sharing class X_DeleteMeQueuable implements Queueable {
Boolean empty;
global void execute(QueueableContext SC) {
empty = false;
System.debug(SC);
if (empty){
System.enqueueJob(new X_DeleteMeQueuable());
}
}
}
Output:
DEBUG|QueueableContextImpl:[jobId=7074B000005GroGQAS]
You can do essentially what you're asking, but there's a couple of precautions you'll want to take.
First, you'll want to create an "on/off" switch that allows you to shut down your queueables should they happen run amuck. Should that occur, you'll need some way of preventing them from continually spawning new jobs. You can easily do this by creating a class that checks the value of a custom setting. Your schedulable and queuables then checks a boolean that's been set on a utility class before it queues up another instance of the queueable.
You'll also want to check limits so you don't try to fill up your flex queue with too many pending jobs. Your method would look something like this:
@future
private static void tryToQueue()
{
if(!MyAppConfigSupport.appEnabled) return;
// This is your On/off switch
try
{
if((Limits.getLimitQueueableJobs() - Limits.getQueueableJobs() > 0) && (!empty))
// I suspect your criteria is !empty
System.enqueueJob(new QueueableApexClassName();
}
catch(Exception ex)
{
// The exception would be because of hitting limits, so...
// you could wait for next request...
// or maybe try using another instance of scheduled Apex for 2 min later?
}
}