Modify the default provisioning of the APS PHP framework by your own definition of the synchronous and asynchronous operations.
In this document:
The updates concern the vpses
service that together with the APS controller and APS PHP framework
must function as follows:
vpses
service for creating a VPS in the sync
mode,
specifies if the VPS
will be a container (sharing the OS kernel with other containers) or a VM (virtual machine with its own OS kernel).sync
POST request to the provision
operation defined in the vpses
service.provision
method confirms the creation of the new VPS
immediately by sending the 200 OK response to the APS controller. This will complete the operation
in the sync
mode.provision
method returns the 202 Accepted code.
It also specifies the time-out and the description for the task that the APS controller must schedule.async
mode until it receives a return code
different from 202 Accepted.async
POST request to the provisionAsync
operation
defined in the vpses
service.vpses
service will require the APS controller to repeat
a POST request in the async
mode 5 times with the time-out 5 seconds.This section continues the demo project started in the previous step.
Modify the vpses
service and respective APS type in the vpses.php
file as follows.
To influence the provisioning process, add the VM
property in the Hardware
class:
/**
* @type("boolean")
* @title("VM with separate OS kernel or Container using shared OS kernel")
* @description("VM requires more time for provisioning - async POST")
*/
public $VM;
This property will allow subscribers to select the VPS type, either VM (VM=true
) or container (VM=false
).
Depending on this, the application provisions the required VPS using the async
or sync
method respectively.
To count the number of calls on the async
provisioning, add the retry
property to the main vps
class.
/**
* @type("integer")
* @title("Counter for async operation")
* @description("Counts the number of retries for async operation")
*/
public $retry;
Define the sync provision
operation in the main vps
class:
// Provision method is called when a sync provisioning is requested
public function provision() {
// VM requires more time, thus the *async* operation is required
if ($this->hardware->VM) {
$this->state = "Creating"; // Intermediate VPS state
throw new \Rest\Accepted($this, "Creating VPS", 5); // Return "202 Accepted"
} else {
// Simulate the end of async provisioning to show correct status in UI
$this->retry = 5;
}
}
When a container is requested ($this->hardware->VM==false
), the method does nothing, and the APS PHP framework
returns 200 OK, thus completing the requested operation successfully in the sync
mode.
When a long running provisioning is requested (creating a VM),
the provision()
method will throw a predefined exception
processed properly by the APS PHP framework. This will cause sending an HTTP 202 Accepted response.
After getting such a response, the APS controller must schedule a task for sending a request for the async
provisioning. The exception contains the task description (“Creating VPS” in the example above) and
time-out (5 s) for the task launch.
Add the async provisioning operation in the main vps
class:
public function provisionAsync() {
$this->retry +=1; // Increment the retry counter
if ($this->retry >= 5) {
$this->state = "Stopped"; // Finish the Async operation - return "200 OK"
}
else {
throw new \Rest\Accepted($this, "Creating VPS", 5); // Return "202 Accepted"
}
}
The provisionAsync()
method returns 202 Accepted
with proper task description and time-out until the application completes the requested provisioning.
Once, the request is completed (simulated by reaching the 5th retry), it returns 200 OK.
This completes modification of the vpses.php
file, which is a part of the provisioning logic.
You can compare the added code with the contents in the respective file
inside the sample package
.