Jump to content

Initiate Blesta Core In External Location


Jonathan

Recommended Posts

Is it possible to initialize all of blesta apps/models/etc in a script completely external to Blesta?

 

So far I've found that I need to load the following files:

 

require_once('../lib/init.php');
require_once('../app/app_controller.php');
require_once('../app/app_model.php');

 

Loader appears to be working properly also:

 

Loader::loadModels($this, array("Clients","Accounts","Services","Packages","PackageGroups","PackageOptions","PackageOptionGroups","Companies"));

 

If I then for example do a call to $this->Clients->getAll(); or something similar it works as expected and I can print_r the return array, but as soon as I make a call to anything needing to write data it just fails, we'll say $this->Clients->add() for example.

 

I get no return, no errors, nothing.

Link to comment
Share on other sites

Is it possible to initialize all of blesta apps/models/etc in a script completely external to Blesta?

 

So far I've found that I need to load the following files:

 

require_once('../lib/init.php');

require_once('../app/app_controller.php');

require_once('../app/app_model.php');

 

Loader appears to be working properly also:

 

Loader::loadModels($this, array("Clients","Accounts","Services","Packages","PackageGroups","PackageOptions","PackageOptionGroups","Companies"));

 

If I then for example do a call to $this->Clients->getAll(); or something similar it works as expected and I can print_r the return array, but as soon as I make a call to anything needing to write data it just fails, we'll say $this->Clients->add() for example.

 

I get no return, no errors, nothing.

 

Have you considered using the API?

Link to comment
Share on other sites

Have you considered using the API?

 

Yep, and it works, but I'm trying to speed things up.  The overhead of API style (connect, execute, disconnect) is what I'm trying to work around now.  Initializing the functionlity directly takes the connect and disconnect out of the equation which is what's really slowing me down at this point.  This includes the "local" CLI API method as well.

 

I'm hoping Cody or Tyson can let me know what element(s) I'm missing here as I know I'm close - gets work, posts just don't seem to do anything.

Link to comment
Share on other sites

You can use the CLI API to interact with models.

 

You can also execute controllers via CLI:

% php /path/to/blesta/index.php controller/action param1 param2 param3

This is how cron jobs are executed (e.g. php /path/to/blesta/index.php cron)

 

You could create a plugin which would give you an environment to execute whatever you want via CLI:

% php /path/to/blesta/index.php plugin/plugin_name/controller_name/action

The above assume the following in /path/to/blesta/plugins/plugin_name/controller_name.php:

<?php
class ControllerName extends AppController {
    public function index() {
        echo "the default action";
    }

    public function action() {
        echo "a different action";
    }

}
Link to comment
Share on other sites

 

You can use the CLI API to interact with models.

 

You can also execute controllers via CLI:

% php /path/to/blesta/index.php controller/action param1 param2 param3

This is how cron jobs are executed (e.g. php /path/to/blesta/index.php cron)

 

You could create a plugin which would give you an environment to execute whatever you want via CLI:

% php /path/to/blesta/index.php plugin/plugin_name/controller_name/action

The above assume the following in /path/to/blesta/plugins/plugin_name/controller_name.php:

<?php
class ControllerName extends AppController {
    public function index() {
        echo "the default action";
    }

    public function action() {
        echo "a different action";
    }

}

 

Yeah I've tried the CLI API calls but unfortunately the connect/disconnect overhead that goes along with each call is still my biggest slowdown.  I've got the actualy HTTP overhead to such a minimum by doing things locally with a good web-server configuration that the difference in the two in my benchmarks was negligible which is why I was hoping to just initiate Blesta directly in my code as to avoid this overhead.

 

EDIT: Just noticed that your calls are bypassing the API and going for a direct execution of the methods.  What sort of overhead does calling to the API have via command line versus calling directly like this?  Will this get rid of a noticeable amount when doing tens of thousands of calls?

Link to comment
Share on other sites

This command can only be called locally . So is not workable in remote host, that reason the API EXIST .

 

Right - I'm working locally.

 

Figured I'd try throwing all of this into a plugin so I can make the calls locally to the core Models (the ones at http://source-docs.blesta.com/package-blesta.app.models.html) and it seems I'm in the same boat as trying to initialize Blesta externally.

 

Any calls to get data work great.  Any calls that write data such as lets say PackageGroups->add or Packages->add, etc. fail with absolutely 0 return or errors.  Even nothing useful from strace.  They literally just seem to do nothing.

Link to comment
Share on other sites

Well I got it figured out.

 

The problem is/was that the api expects the input arrays to be in the format of

 

$vars

     'vars'

         var1

         var2

 

Whereas internally the calls are expected to be like this:

 

$vars

    var1

    var2

 

Basically drop the 'vars' array which contains the values for the API and set the values directly on the parent array passed to the method.

 

A bit of clarification in the documentation could certainly save folks some time.  This got me both starting using the API as well as trying to convert from the API to internal for speed.

Link to comment
Share on other sites

Well I got it figured out.

The problem is/was that the api expects the input arrays to be in the format of

$vars

'vars'

var1

var2

Whereas internally the calls are expected to be like this:

$vars

var1

var2

Basically drop the 'vars' array which contains the values for the API and set the values directly on the parent array passed to the method.

A bit of clarification in the documentation could certainly save folks some time. This got me both starting using the API as well as trying to convert from the API to internal for speed.

Now the requests of add/edit work with loading the core files without api ?

Link to comment
Share on other sites

Now the requests of add/edit work with loading the core files without api ?

 

Well presumably it would work loading the core files - at this point I've got it dirtily converted into a plugin function I'm calling directly.  I did that while ruling things out and there's no point in going back now.

 

It's sped up this process I'm doing ten-fold.  Don't have a full idea yet as I'm still converting things in my script from making API calls to now internal calls but I suspect it will take this, previously 2-3 hour long process down to 10-20 mins based on what I've tested thus far.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...