public function test_failed_execution_comments()
 {
     $hook = md5(rand());
     add_action($hook, array($this, '_a_hook_callback_that_throws_an_exception'));
     $action_id = wc_schedule_single_action(time(), $hook);
     $logger = ActionScheduler::logger();
     $started = new ActionScheduler_LogEntry($action_id, 'action started');
     $finished = new ActionScheduler_LogEntry($action_id, 'action complete');
     $failed = new ActionScheduler_LogEntry($action_id, 'action failed: Execution failed');
     $runner = new ActionScheduler_QueueRunner();
     $runner->run();
     $logs = $logger->get_logs($action_id);
     $this->assertTrue(in_array($started, $logs));
     $this->assertFalse(in_array($finished, $logs));
     $this->assertTrue(in_array($failed, $logs));
 }
 /**
  * @return ActionScheduler_QueueRunner
  * @codeCoverageIgnore
  */
 public static function instance()
 {
     if (empty(self::$runner)) {
         $class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner');
         self::$runner = new $class();
     }
     return self::$runner;
 }
 public function test_next_instance_of_action()
 {
     $store = new ActionScheduler_wpPostStore();
     $runner = new ActionScheduler_QueueRunner($store);
     $random = md5(rand());
     $schedule = new ActionScheduler_IntervalSchedule(new DateTime('12 hours ago'), DAY_IN_SECONDS);
     $action = new ActionScheduler_Action($random, array(), $schedule);
     $store->save_action($action);
     $runner->run();
     $claim = $store->stake_claim(10, new DateTime(DAY_IN_SECONDS - 60 . ' seconds'));
     $this->assertCount(0, $claim->get_actions());
     $claim = $store->stake_claim(10, new DateTime(DAY_IN_SECONDS . ' seconds'));
     $actions = $claim->get_actions();
     $this->assertCount(1, $actions);
     $action_id = reset($actions);
     $new_action = $store->fetch_action($action_id);
     $this->assertEquals($random, $new_action->get_hook());
     $this->assertEquals($schedule->next(new DateTime()), $new_action->get_schedule()->next(new DateTime()));
 }
 public function test_do_not_delete_recent_actions()
 {
     $store = new ActionScheduler_wpPostStore();
     $runner = new ActionScheduler_QueueRunner($store);
     $random = md5(rand());
     $schedule = new ActionScheduler_SimpleSchedule(new DateTime('1 day ago'));
     $created_actions = array();
     for ($i = 0; $i < 5; $i++) {
         $action = new ActionScheduler_Action($random, array($random), $schedule);
         $created_actions[] = $store->save_action($action);
     }
     $runner->run();
     $cleaner = new ActionScheduler_QueueCleaner($store);
     $cleaner->delete_old_actions();
     foreach ($created_actions as $action_id) {
         $action = $store->fetch_action($action_id);
         $this->assertTrue($action->is_finished());
         // It's a FinishedAction
     }
 }
 public static function runner()
 {
     return ActionScheduler_QueueRunner::instance();
 }
/**
 * Handle requests initiated by ashp_request_additional_runners() and start a queue runner if the request is valid.
 */
function ashp_create_additional_runners()
{
    if (isset($_POST['ashp_nonce']) && isset($_POST['instance']) && wp_verify_nonce($_POST['ashp_nonce'], 'ashp_additional_runner_' . $_POST['instance'])) {
        ActionScheduler_QueueRunner::instance()->run();
    }
    wp_die();
}
 public function test_changing_batch_count_limit()
 {
     $store = new ActionScheduler_wpPostStore();
     $runner = new ActionScheduler_QueueRunner($store);
     $random = md5(rand());
     $schedule = new ActionScheduler_SimpleSchedule(new DateTime('1 day ago'));
     for ($i = 0; $i < 30; $i++) {
         $action = new ActionScheduler_Action($random, array($random), $schedule);
         $store->save_action($action);
     }
     $claims = array();
     for ($i = 0; $i < 5; $i++) {
         $claims[] = $store->stake_claim(5);
     }
     $mock1 = new MockAction();
     add_action($random, array($mock1, 'action'));
     $actions_run = $runner->run();
     remove_action($random, array($mock1, 'action'));
     $this->assertEquals(0, $mock1->get_call_count());
     $this->assertEquals(0, $actions_run);
     add_filter('action_scheduler_max_claims', array($this, 'return_6'));
     $mock2 = new MockAction();
     add_action($random, array($mock2, 'action'));
     $actions_run = $runner->run();
     remove_action($random, array($mock2, 'action'));
     $this->assertEquals(5, $mock2->get_call_count());
     $this->assertEquals(5, $actions_run);
     remove_filter('action_scheduler_max_claims', array($this, 'return_6'));
     for ($i = 0; $i < 5; $i++) {
         // to make up for the actions we just processed
         $action = new ActionScheduler_Action($random, array($random), $schedule);
         $store->save_action($action);
     }
     $mock3 = new MockAction();
     add_action($random, array($mock3, 'action'));
     $actions_run = $runner->run();
     remove_action($random, array($mock3, 'action'));
     $this->assertEquals(0, $mock3->get_call_count());
     $this->assertEquals(0, $actions_run);
     remove_filter('action_scheduler_max_claims', array($this, 'return_6'));
 }