-
-
Notifications
You must be signed in to change notification settings - Fork 1
Go function scripts
go() scripts are a more application-friendly style of cron task.
Instead of launching a standalone script process and reading argv, we place a PHP file in cron/ that defines a go() function:
<?php
use GT\Input\Input;
function go(Input $input):void {
file_put_contents(
"var/cron.log",
"Mode: " . $input->getString("mode") . PHP_EOL,
FILE_APPEND
);
}The runner loads that function and invokes it directly.
This style is useful when we want:
- a tidy task entry point in
cron/ - request-like named input instead of shell arguments
- dependency injection for services
- nested files inside the
cron/directory
If cron/cache.php contains function go(...), all of these command forms are accepted:
* * * * * cache?type=db&mode=daily
* * * * * /cache?type=db&mode=daily
* * * * * cache.php?type=db&mode=daily
* * * * * cron/cache?type=db&mode=daily
* * * * * /cron/cache?type=db&mode=daily
The part after ? is parsed as a query string.
Query-string values are made available through an Input object:
<?php
use GT\Input\Input;
function go(Input $input):void {
$type = $input->getString("type");
$mode = $input->getString("mode");
}This keeps the task code readable when there are several named options.
go() functions may also receive services as parameters when they can be resolved from the service container.
Example:
<?php
use GT\Input\Input;
use App\Service\Recorder;
function go(Input $input, Recorder $recorder):void {
$recorder->write($input->getString("type"));
}The runner creates a container, puts the Input object into it, and then uses the service loaders that are available for the project.
When configuration is available, the runner reads:
app.namespaceapp.class_dirapp.service_loader
From there it can:
- autoload project classes from the configured class directory
- register WebEngine's default service loader when it is installed
- register the project's own service loader class
Note
In WebEngine projects this usually means go() scripts can use the same service-loading conventions as page logic, which makes cron tasks feel like part of the application rather than a separate subsystem.
Unlike ordinary script aliases, go() scripts may live in subdirectories under cron/.
For example, this may point at cron/report/daily.php:
0 6 * * * report/daily?audience=internal
go() script commands do not accept shell-style trailing arguments after the script name. The special handling is only used when the command is just the script reference, optionally followed by a query string.
Use query-string parameters when the task needs named input.
Choose an ordinary cron/*.php script when:
-
argvis enough - we want a separate process
- the task is already written as a standalone script
Choose a go() script when:
- we are in a WebEngine context
- we want named input through
Input - we want service injection
- we want to organise tasks into nested directories
If the built-in cron syntax is not enough, the next page shows how to extend it with Custom expression factories.
PHP.GT/Cron is a separately maintained component of PHP.GT/WebEngine.