<?php
namespace Jobs\Controller;

use Mall\Framework\Factory;
use Jobs\Controller\CBase;

class CJobs extends CBase
{
    private $logger=null;

    public function __construct($config)
    {
        $this->config      = $config;
        $this->logger      = Factory::logs($this->config['logPath'] ?:'', $this->config['logSaveFileApp'] ?:'', $this->config['app_name'] ?:'');
    }

    public function start($JobObject)
    {
        $jobClass =$JobObject->jobClass;
        $jobMethod=$JobObject->jobMethod;
        $jobParams=$JobObject->jobParams;
        try {
            $obj      =new $jobClass();
            if (is_object($obj) && method_exists($obj, $jobMethod)) {
                $result = call_user_func([$obj, $jobMethod], $jobParams);
                $this->logger->log('topic run result: ' . $result, 'error');
            } else {
                $this->logger->log('Action obj not find: ' . json_encode($JobObject), 'error');
            }
        } catch (\Throwable $e) {
            catchError($this->logger, $e);
        } catch (\Exception $e) {
            catchError($this->logger, $e);
        }

        $this->logger->log('Action has been done, action content: ' . json_encode($JobObject));
    }
}