/** * @param \Venne\Queue\Job $job * @param integer $priority */ public function run(Job $job, $priority) { /** @var Notification $notificationEntity */ $notificationEntity = $this->notificationRepository->find($job->arguments[0]); if ($notificationEntity === null) { return; } $qb = $this->notificationSettingRepository->createQueryBuilder('a')->andWhere('a.type IS NULL OR a.type = :type')->setParameter('type', $notificationEntity->type->id); if ($notificationEntity->user) { $qb = $qb->andWhere('a.targetUser IS NULL OR a.targetUser = :targetUser')->setParameter('targetUser', $notificationEntity->user->id); } if ($notificationEntity->target) { $qb = $qb->andWhere('a.target IS NULL OR a.target = :target')->setParameter('target', $notificationEntity->target); } if ($notificationEntity->targetKey) { $qb = $qb->andWhere('a.targetKey IS NULL OR a.targetKey = :targetKey')->setParameter('targetKey', $notificationEntity->targetKey); } $users = array(); foreach ($qb->getQuery()->getResult() as $setting) { $user = $setting->user !== null ? $setting->user : $notificationEntity->user; if (isset($users[$user->id]) && !$users[$user->id]['email']) { $users[$user->id]['email'] = $setting->email; } else { $users[$user->id] = array('user' => $user, 'email' => $setting->user !== null ? $setting->email : $notificationEntity->user->getEmail()); } } foreach ($users as $user) { $notificationUser = new NotificationUser($notificationEntity, $user['user']); $this->entityManager->persist($notificationUser); $this->entityManager->flush($notificationUser); if ($user['email']) { $this->jobManager->scheduleJob(new Job(EmailJob::getName(), null, array('user' => $user['user'] instanceof User ? $user['user']->id : $user['user'], 'notification' => $notificationUser->id)), $priority); } } }
/** * @return \Venne\System\Components\AdminGrid\AdminGrid */ protected function createComponentTable() { $admin = $this->adminGridFactory->create($this->jobRepository); $table = $admin->getTable(); $table->addColumnText('type', 'Type')->getCellPrototype()->width = '35%'; $table->addColumnText('state', 'State')->getCellPrototype()->width = '15%'; $table->addColumnText('priority', 'Priority')->getCellPrototype()->width = '10%'; $table->addColumnDate('date', 'Date')->getCellPrototype()->width = '15%'; $table->addColumnDate('dateInterval', 'Interval')->getCellPrototype()->width = '15%'; $table->addColumnDate('round', 'Round')->getCellPrototype()->width = '10%'; // actions $table->addActionEvent('run', 'Run')->onClick[] = function ($id) { $job = $this->jobRepository->find($id); try { $this->jobManager->scheduleJob($job, JobManager::PRIORITY_REALTIME); $this->flashMessage($this->getTranslator()->translate('Job has been done.'), 'success'); } catch (JobFailedException $e) { $this->flashMessage($this->getTranslator()->translate('Job failed.'), 'warning'); } $this->redirect('this'); }; $table->addActionEvent('edit', 'Edit')->getElementPrototype()->class[] = 'ajax'; $form = $admin->addForm('job', 'Job', function (Job $job = null) { return $this->jobFormService->getFormFactory($job !== null ? $job->getId() : null); }); $admin->connectFormWithAction($form, $table->getAction('edit')); $table->addActionEvent('delete', 'Delete')->getElementPrototype()->class[] = 'ajax'; $admin->connectActionAsDelete($table->getAction('delete')); return $admin; }
/** * @return \Nette\Application\UI\Form */ public function create() { $form = $this->formFactory->create(); $form->addGroup('Job'); $form->addSelect('type', 'Type')->setItems(array_keys($this->jobManager->getJobs()), false)->addRule($form::FILLED); $form->addSelect('state', 'State')->setItems(array(Job::STATE_SCHEDULED, Job::STATE_IN_PROGRESS, Job::STATE_FAILED), false)->addRule($form::FILLED); $form->addSelect('priority', 'priority')->setItems(array(Job::PRIORITY_LOW => 'low', Job::PRIORITY_NORMAL => 'normal', Job::PRIORITY_HIGH => 'high'))->addRule($form::FILLED); // $form->addDateTime('date', 'Date') // ->addRule($form::FILLED); // // $form->addDateTime('interval', 'Interval') // ->addRule($form::FILLED); $form->addText('round', 'Round')->addCondition($form::FILLED)->addRule($form::INTEGER); return $form; }
/** * @param string $type * @param string|null $target * @param string|null $action * @param string|null $message * @param \Venne\Security\User|null $user * @param integer $priority */ public function notify($type, $target = null, $action = null, $message = null, User $user = null, $priority = NotificationManager::PRIORITY_DEFAULT) { if (!isset($this->types[$type])) { throw new InvalidArgumentException(sprintf('Type \'%s\' does not exist.', $type)); } if ($target && !is_object($target)) { throw new InvalidArgumentException('Target must be object'); } $targetKey = $this->detectPrimaryKey($target); if ($target instanceof BaseEntity) { $metadata = $this->entityManager->getClassMetadata(get_class($target)); $target = $metadata->getName(); } else { $target = get_class($target); } $typeEntity = $this->getTypeEntity($type, $action, $message); $user = $user !== null ? $user : $this->getUser(); $notification = new Notification($typeEntity, $user, $target, $targetKey); $this->entityManager->persist($notification); $this->entityManager->flush(); $job = new Job(NotificationJob::getName(), null, array($notification->id)); $job->user = $user; $this->jobManager->scheduleJob($job, $priority); }
/** * @return bool */ public function run() { $this->log('check jobs!'); return $this->jobManager->doNextWork($this); }