/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Cronjob::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'recurrence_week' => $this->recurrence_week, 'recurrence_month' => $this->recurrence_month, 'recurrence_year' => $this->recurrence_year, 'job' => $this->job, 'job_id' => $this->job_id, 'task_id' => $this->task_id, 'rule_id' => $this->rule_id, 'start_at' => $this->start_at, 'end_at' => $this->end_at, 'run_at' => $this->run_at, 'weight' => $this->weight, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'recurrence_minute', $this->recurrence_minute])->andFilterWhere(['like', 'recurrence_hour', $this->recurrence_hour])->andFilterWhere(['like', 'recurrence_day', $this->recurrence_day]);
     //->andFilterWhere(['like', 'job', $this->job]);
     return $dataProvider;
 }
 /**
  * This function is call by app\commands\CronController, 
  * the CronController is call by the server cron
  */
 public function cron()
 {
     $models = Cronjob::find()->orderBy('weight')->all();
     // define date, and floor to 5 minutes
     $now = date('Y-m-d H:i:00', floor(time() / (5 * 60)) * (5 * 60));
     foreach ($models as $model) {
         // check start_at and end_at is between date now
         if ($model->start_at <= $now and ($model->end_at >= $now or empty($model->end_at))) {
             // add the cronjob time to the run_at, and check if it has to run (has to be lower or equal than now)
             $run_at = $model->run_at;
             $run_at = date('Y-m-d H:i:s', strtotime('+' . $model->recurrence_minute, strtotime($run_at)));
             $run_at = date('Y-m-d H:i:s', strtotime('+' . $model->recurrence_hour, strtotime($run_at)));
             $run_at = date('Y-m-d H:i:s', strtotime('+' . $model->recurrence_day, strtotime($run_at)));
             $run_at = date('Y-m-d H:i:s', strtotime('+' . $model->recurrence_week, strtotime($run_at)));
             $run_at = date('Y-m-d H:i:s', strtotime('+' . $model->recurrence_month, strtotime($run_at)));
             $run_at = date('Y-m-d H:i:s', strtotime('+' . $model->recurrence_year, strtotime($run_at)));
             // check if it has to run
             if ($run_at <= $now) {
                 // check if the job is correct executed
                 $executed = call_user_func(array('app\\models\\' . ucfirst($model->job), 'cronjob'), $model->job_id);
                 // only update the cronjob if the job is executed correctly
                 //if($executed){
                 // update run_at
                 //$model = Cronjob::findOne($model->id);
                 $model->run_at = $now;
                 if (!$model->save()) {
                     print_r($model->errors);
                 }
                 //}
             }
         }
     }
 }