public function test_do_not_reset_failed_action()
 {
     $store = new ActionScheduler_wpPostStore();
     $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);
     }
     $claim = $store->stake_claim(10);
     foreach ($claim->get_actions() as $action_id) {
         // simulate the first action interrupted by an uncatchable fatal error
         $store->log_execution($action_id);
         break;
     }
     add_filter('action_scheduler_timeout_period', '__return_zero');
     // delete any finished job
     $cleaner = new ActionScheduler_QueueCleaner($store);
     $cleaner->reset_timeouts();
     remove_filter('action_scheduler_timeout_period', '__return_zero');
     $new_claim = $store->stake_claim(10);
     $this->assertCount(4, $new_claim->get_actions());
     add_filter('action_scheduler_failure_period', '__return_zero');
     $cleaner->mark_failures();
     remove_filter('action_scheduler_failure_period', '__return_zero');
     $failed = $store->query_actions(array('status' => ActionScheduler_Store::STATUS_FAILED));
     $this->assertEquals($created_actions[0], $failed[0]);
     $this->assertCount(1, $failed);
 }
 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()));
 }
 /**
  * Utility method to check the action scheduler for dates
  *
  * @param  string $type             the type of scheduled action
  * @param  string $subscription_key key of subscription in the format of order_id_item_id
  * @return string                   either 0 or mysql date
  */
 private static function maybe_get_date_from_action_scheduler($type, $subscription)
 {
     $action_scheduler = new ActionScheduler_wpPostStore();
     $action_id = $action_scheduler->find_action($type, array('subscription_key' => $subscription['subscription_key']));
     if (is_numeric($action_id)) {
         try {
             $date = $action_scheduler->get_date($action_id);
             $formatted_date = $date->format('Y-m-d H:i:s');
         } catch (Exception $e) {
             WCS_Upgrade_Logger::add(sprintf('-- For order %d: while fetching date from action scheduler, an error occurred. No such action id.', $subscription['order_id']));
             $formatted_date = 0;
         }
     } else {
         $formatted_date = 0;
     }
     return $formatted_date;
 }
 public function test_get_run_date()
 {
     $time = new DateTime('-10 minutes');
     $schedule = new ActionScheduler_IntervalSchedule($time, HOUR_IN_SECONDS);
     $action = new ActionScheduler_Action('my_hook', array(), $schedule);
     $store = new ActionScheduler_wpPostStore();
     $action_id = $store->save_action($action);
     $this->assertEquals($store->get_date($action_id)->format('U'), $time->format('U'));
     $action = $store->fetch_action($action_id);
     $action->execute();
     $now = new DateTime();
     $store->mark_complete($action_id);
     $this->assertEquals($store->get_date($action_id)->format('U'), $now->format('U'));
     $next = $action->get_schedule()->next($now);
     $new_action_id = $store->save_action($action, $next);
     $this->assertEquals((int) $now->format('U') + HOUR_IN_SECONDS, $store->get_date($new_action_id)->format('U'));
 }
 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'));
 }