Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restart the scheduler to call configure again to get new tasks #588

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions system/async/tasks/Scheduler.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ component accessors="true" singleton {
variables.tasks = structNew( "ordered" );
// Default TimeZone to UTC for all tasks
variables.timezone = createObject( "java", "java.time.ZoneId" ).systemDefault();
// Build out the executor for this scheduler
variables.executor = arguments.asyncManager.newExecutor(
name: arguments.name & "-scheduler",
type: "scheduled"
);
// Bit that denotes if this scheduler has been started or not
variables.started = false;
variables.started = false;
// Send notice
arguments.asyncManager.out( "√ Scheduler (#arguments.name#) has been registered" );

Expand Down Expand Up @@ -167,6 +162,12 @@ component accessors="true" singleton {
if ( !variables.started ) {
lock name="scheduler-#getName()#-startup" type="exclusive" timeout="45" throwOnTimeout="true" {
if ( !variables.started ) {
// Build out the executor for this scheduler
variables.executor = arguments.asyncManager.newExecutor(
name: arguments.name & "-scheduler",
type: "scheduled"
);

// Iterate over tasks and send them off for scheduling
variables.tasks.each( function( taskName, taskRecord ){
// Verify we can start it up the task or not
Expand Down Expand Up @@ -250,6 +251,28 @@ component accessors="true" singleton {
return this;
}

/**
* Restarts a scheduler by shutting it down, clearing out the tasks, calling `configure`, and starting it again.
* Useful when loading tasks from a database or other dynamic sources.
*
* @force If true, it forces all shutdowns this is usually true when doing reinits
* @timeout The timeout in seconds to wait for the shutdown of all tasks, defaults to 30 or whatever you set using the setShutdownTimeout() method
*/
public Scheduler function restart( boolean force = false, numeric timeout = variables.shutdownTimeout ){
variables.asyncManager.out( "√ Scheduler (#arguments.name#) is being restarted" );
shutdown( argumentCollection = arguments );
clearTasks();
configure();
startup();
variables.asyncManager.out( "√ Scheduler (#arguments.name#) has been restarted" );
return this;
}

public Scheduler function clearTasks(){
variables.tasks = structNew( "ordered" );
return this;
}

/**
* --------------------------------------------------------------------------
* Life - Cycle Callbacks
Expand Down
23 changes: 22 additions & 1 deletion system/web/services/SchedulerService.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ component extends="coldbox.system.web.services.BaseService" accessors="true" {
}

/**
* This method is ran by the laoder service once the ColdBox application is ready to serve requests.
* This method is ran by the loader service once the ColdBox application is ready to serve requests.
* It will startup all the schedulers in the order they where registered.
*/
SchedulerService function startupSchedulers(){
Expand Down Expand Up @@ -195,4 +195,25 @@ component extends="coldbox.system.web.services.BaseService" accessors="true" {
return false;
}

/**
* Restarts a scheduler from this manager, if it exists.
*
* @name The name of the scheduler
* @force If true, it forces all shutdowns this is usually true when doing reinits
* @timeout The timeout in seconds to wait for the shutdown of all tasks, defaults to the scheduler's shutdown timeout
*
* @return True if restarted, false if not found
*/
boolean function restartScheduler(
required name,
boolean force = false,
numeric timeout
){
if ( hasScheduler( arguments.name ) ) {
variables.schedulers[ arguments.name ].restart();
elpete marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}

}
Loading