Typo3 scheduler class not found - php

I am trying to get a scheduler class running on typo3.
ext_autoload.php:
$extensionPath = t3lib_extMgm::extPath('mh_compass');
$loaderClass = array(
'tx_monitorcompassdailyreset_sched' => $extensionPath.'scheduler/class.tx_monitorcompassdailyreset_sched.php',
);
return $loaderClass;
ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['tx_monitorcompassdailyreset_sched'] = array(
'extension' => $_EXTKEY,
'title' => 'Compass Monitor Daily Reset Scheduler',
'description' => 'Reset daily Monitor Jobs',
);
class.tx_monitorcompassdailyreset_sched.php:
class tx_monitorcompassdailyreset_sched extends tx_scheduler_Task {
public function execute() {
error_log( "Start Compass Monitor Daily Reset Scheduled Job" );
//do some stuff
error_log( "Finished Compass Monitor Scheduled Job" );
}
}
When I try to add the task in the scheduler in the backend, I get the following error:
Fatal error: Class 'tx_monitorcompassdailyreset_sched' not found in /var/www/typo3_src-4.5.22/t3lib/class.t3lib_div.php on line 5375
This doesn't make sense as a) I have used debug on that function and it loads all the other classes, and b) I cannot see a discrepancy in my class naming.
The version of php is 5.3.10 (there was a bug like this with 5.3.2, but it disappeared)

Your class naming is obviously wrong. Your extension key is "mh_compass", so your class name needs to start with "tx_mhcompass_". The beginning of the class name always contains "tx_" followed up with the extension key without underscores.
Try renaming your class to "tx_mhcompass_Task_Reset" for example.

Related

Adding a cronjob with moodle-cron-api is not working properly

I am trying to schedule a task in moodle-cron-api following the instructions here at: https://docs.moodle.org/dev/Task_API.
I have the root folder at /local and the name of the root folder is mod_hygene.
I have a cut_my_toe_nails.php at /local/mod_hygene/classes/task which is:
namespace mod_hygene\task;
/**
* An example of a scheduled task.
*/
class cut_my_toe_nails extends \core\task\scheduled_task {
/**
* Return the task's name as shown in admin screens.
*
* #return string
*/
public function get_name() {
return get_string('cutmytoenails', 'mod_hygene');
}
/**
* Execute the task.
*/
public function execute() {
// Apply fungus cream.
// Apply chainsaw.
// Apply olive oil.
echo 'do';
}
}
And there is /mod_hygene/db/tasks.php:
$tasks = [
[
'classname' => 'mod_hygene\task\cut_my_toe_nails',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'month' => '1,7',
'dayofweek' => '*',
],
];
As you see, the task should run every minute. I run my moodle container via terminal with
docker-compose up -d --build moodle
In the terminal I should see 'do' printed every minute. But, I am not seeing anything. I hovered to Site Administration/Server/Scheduled Tasks. There I am not seeing this task. But I checked in Site Administration/Plugins/Plugins Overview and could find local_mod_hygene in the Local Plugins.
Can anyone help me with it? Do I need to make some changes to the Dockerfile as well?
Just a tip, don't use the prefix mod_ for a local plugin, it could be confused for an activity plugin in the mod folder.
So assuming your code is in /local/hygene/
The task file should be /local/hygene/classes/task/cut_my_toe_nails.php
The class namespace should be
namespace local_hygene\task;
The tasks file should be /local/hygene/db/tasks.php with
'classname' => 'local_hygene\task\cut_my_toe_nails',
Once its installed, check if the task is enabled via
Site admin > Server > Tasks
Or direct to
yourwebsite/admin/tool/task/scheduledtasks.php
If its installed and enabled, then run it from the command line to test it.
First see if its listed - if its not listed then its not installed correctly
php admin/cli/scheduled_task.php --list
If it it listed, then note the name and run the task manually - note that the \ has to be escaped with \\
php admin/cli/scheduled_task.php --execute=\\local_hygene\\task\\cut_my_toe_nails
If the task is working correctly, then wait for cron
Or, depending on the settings, you can run cron manually via yourmoodlewebsite/admin/cron.php when logged in as an admin
Or from the command line
php admin/cli/cron.php

Shopware 6 Production Unable to access plugin services from the container in a unit test

I setup a Unit Test in a Shopware custom (static) Plugin following this guide:
Shopware documentation
Everything runs fine and I'm able to run a unit test
class ProductReturnsTest extends TestCase
{
use IntegrationTestBehaviour;
use StorefrontPageTestBehaviour;
public function testConfirmPageSubscriber(): void
{
$container = $this->getKernel()->getContainer();
$dd = $container->get(CustomDataService::class); <== IT BREAKS HERE ServiceNotFoundException: You have requested a non-existent service
$dd = $container->get('event_dispatcher'); // WORKS WITH SHOPWARE ALIASES NOT WITH PLUGINS
}
}
I can make container->get on any shopware alias but as soon I try to recall and get from the container any service decleared in any xml of any 3th party plugin, i get
ServiceNotFoundException: You have requested a non-existent service "blabla"
What is wrong ?
Take a look at the answer given here: https://stackoverflow.com/a/70171394/10064036.
Probably your plugin is not marked as active in the DB your tests run against.
The test environment has a mostly unpopulated database to allow tests to to run unaffected with their own fixtures only. Therefore after each test there should be a rollback to all transactions made within the test. This principle also includes plugin installations and database transactions they may execute in their lifecycle events.
You may want to install your plugin properly before your tests, so you get a representative state of the environment with the plugins lifecycle events getting dispatched and thereby caused possible changes.
public function setUp(): void
{
$this->installPlugin();
}
private function installPlugin(): void
{
$application = new Application($this->getKernel());
$installCommand = $application->find('plugin:install');
$args = [
'--activate' => true,
'--reinstall' => false,
'plugins' => ['YourPluginName'],
];
$installCommand->run(new ArrayInput($args, $installCommand->getDefinition()), new NullOutput());
}

ReflectionException - Class name does not exist in Laravel 5.0

I have a little problem trying to seed my comments table.
I'm 100% sure that I have the Class CommentTableSeeder.php in my /database/seeds directory.
CommentTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class CommentTableSeeder extends Seeder {
public function run()
{
DB::table('comments')->delete();
Comment::create(array(
'author' => 'Chris Sevilleja',
'text' => 'Look I am a test comment.'
));
Comment::create(array(
'author' => 'Nick Cerminara',
'text' => 'This is going to be super crazy.'
));
Comment::create(array(
'author' => 'Holly Lloyd',
'text' => 'I am a master of Laravel and Angular.'
));
}
}
Then when I run : php artisan db:seed
I kept getting
I've also try running composer update and run : php artisan db:seed - still get the same result.
Any hints / help will be much appreciated !
You need to run
composer dump-autoload
to fix this error. What this does, among other things, is refreshes the list of classes available to your application.
In this case, while the class did exist in the right location, it was unavailable in your autoloaded class list, and thus returning a Not Found error.

Symfony 2: Access updated configuration from inside a command

We are creating a command that relies on other commands to generate a new database and build out its schema. So far we have successfully gotten it to read the config.yml file, add our new connection information, and to write the file back. In the same command we are then trying to run the symfony commands to create the database and schema:update. This is where we are running into problems. We get the following error:
[InvalidArgumentException] Doctrine ORM Manager named "mynewdatabase"
does not exist.
If we run the command a second time there is no error because the updated configuration file is loaded fresh into the application. If we manually run the doctrine commands after writing to the config.yml file it also works without error.
We are thinking that at the point in our command where we're running the database create and update commands, it's still using the current kernel's version of the config.yml/database.yml that are stored in memory. We have tried a number of different ways to reinitialize the application/kernel configuration (calling shutdown(), boot(), etc) without luck. Here's the code:
namespace Test\MyBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
class GeneratorCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('generate')
->setDescription('Create a new database.')
->addArgument('dbname', InputArgument::REQUIRED, 'The db name')
;
}
/*
example: php app/console generate mynewdatabase
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
//Without this, the doctrine commands will prematurely end execution
$this->getApplication()->setAutoExit(false);
//Open up app/config/config.yml
$yaml = Yaml::parse(file_get_contents($this->getContainer()->get('kernel')->getRootDir() .'/config/config.yml'));
//Take input dbname and use it to name the database
$db_name = $input->getArgument('dbname');
//Add that connection to app/config/config.yml
$yaml['doctrine']['dbal']['connections'][$site_name] = Array('driver' => '%database_driver%', 'host' => '%database_host%', 'port' => '%database_port%', 'dbname' => $site_name, 'user' => '%database_user%', 'password' => '%database_password%', 'charset' => 'UTF8');
$yaml['doctrine']['orm']['entity_managers'][$site_name] = Array('connection' => $site_name, 'mappings' => Array('MyCustomerBundle' => null));
//Now put it back
$new_yaml = Yaml::dump($yaml, 5);
file_put_contents($this->getContainer()->get('kernel')->getRootDir() .'/config/config.yml', $new_yaml);
/* http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command */
//Set up our db create script arguments
$args = array(
'command' => 'doctrine:database:create',
'--connection' => $site_name,
);
$db_create_input = new ArrayInput($args);
//Run the symfony database create arguments
$this->getApplication()->run($db_create_input, $output);
//Set up our schema update script arguments
$args = array(
'command' => 'doctrine:schema:update',
'--em' => $site_name,
'--force' => true
);
$update_schema_input = new ArrayInput($args);
//Run the symfony database create command
$this->getApplication()->run($update_schema_input, $output);
}
}
The reason this doesn't work is because the DIC goes through a compilation process and is then written to a PHP file which is then included into the current running process. Which you can see here:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Kernel.php#L562
If you change the service definitions and then try to "reboot" the kernel to compile these changes it won't include the compiled file a second time (require_once) and it will just create another instance of the already included DIC class with the old compiled service definitions.
The simplest way I can think of to get around this is to create an empty Kernel class that simply extends your AppKernel. Like so:
<?php
namespace Test\MyBundle\Command;
class FakeKernel extends \AppKernel
{
}
Then in your command, you can boot up this kernel after you've saved the new service definitions and it will re-compile a new DIC class using the "FakeKernel" name as part of the file name which means it will be included. like so:
$kernel = new \Test\MyBundle\Command\FakeKernel($input->getOption('env'), true);
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
Then you run your sub-commands against this new application which will be running with the new DIC:
$application->run($db_create_input, $output);
disclaimer: this feels very hacky. I'm open to hearing other solutions/workarounds.

How can I define a job in php resque for redis?

I am new for redis and currently I am using PHP resque for redis. How can I define a job in php resque?
This has changed in the newest version of PHP-resque, released 13 October 2012. According to the changelog, "Wrap job arguments in an array to improve compatibility with ruby resque."
which means if you've upgraded to PHP-Resque 1.2 you will access jobs from $args[0].
Queueing Jobs
Jobs are queued as follows:
require_once 'lib/Resque.php';
// Required if redis is located elsewhere
Resque::setBackend('localhost:6379');
$args = array(
'name' => 'Chris'
);
Resque::enqueue('default', 'My_Job', $args);
Defining Jobs
Each job should be in it's own class, and include a perform method.
class My_Job
{
public function perform()
{
// Work work work
echo $this->args['name'];
}
}
When the job is run, the class will be instantiated and any arguments will be set as an array on the instantiated object, and are accessible via $this->args.
Any exception thrown by a job will result in the job failing - be careful here and make sure you handle the exceptions that shouldn't result in a job failing.

Categories