I’m wanting to create a background worker for a ZF application I’m working on but I’m baffled not as much about the software architecture but more of the filesystem architecture.
The worker would be triggered by a controller to perform some tasks and then the controller would check up on the status of the worker so this bit has been covered.
From the folder structure point of view where should these the code for the worker sit in?
application/
models/
services/
worker/
application/ --> code for the worker (standard ZF structure)
worker.php --> entry-point to the worker
Or
application/
controllers/
WorkerController.php
models/
Worker/
Class.php
Class2.php
services/
worker.php --> entry-point to the worker
Bear in mind the configuration of the main application and the worker are almost identical (especially same db connection credentials, autoloading settings) and the worker would need to access the main application’s models.
Any advice or opinions would be appreciated.
Many thanks,
Angel
If the worker is triggered via cronjob, then you could make the worker component a module, so it has its own controllers, views, etc. Then – as @MonkeyMonkey notes – your commandline script could make MVC requests to that module.
But it seems to me that this worker component might function more naturally as a service, a class containing functionality that gets invoked by your cron-triggered cli script. ZF-based cli scripts – optionally using
Zend_Console_Getopt, which is pretty cool – can use the same config and Bootstrap class, selectively bootstrap resources (some might not be required for the cli-based task), and use the same autoloaders as the standard MVC app.As you note, these workers will update a status table that would be accessible to the web-facing portion of the app, so those pages can read/render the status on request.
As for the filesystem structure of that, you could name these service classes something like
Application_Service_MyWorkerstored in the fileapplication/services/MyWorker.php. Perhaps even push down further using something likeApplication_Service_Worker_MyWorkerstored inapplication/services/Worker/MyWorker.php, though this latter might require adding another resource-type entry into the resource autoloader, similar to the way that mappers and DbTable-based models are defined inZend_Application_Module_Autoloader.