Skip to content

Commit 0e52443

Browse files
authored
Added possibility to create one shot job from scheduled job (#4)
1 parent a46d008 commit 0e52443

File tree

7 files changed

+98
-5
lines changed

7 files changed

+98
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Version 4.4.0
2+
* Added possibility to create one shot job from scheduled job
3+
4+
# Version 4.3.0
5+
6+
* Replaced date field with date picker
7+
18
# Version 4.2.0
29

310
* Added error count columns to job tables

src/Controller/JobController.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use CodeRhapsodie\DataflowBundle\Entity\Job;
88
use CodeRhapsodie\EzDataflowBundle\Form\CreateOneshotType;
99
use CodeRhapsodie\EzDataflowBundle\Gateway\JobGateway;
10+
use CodeRhapsodie\EzDataflowBundle\Gateway\ScheduledDataflowGateway;
1011
use Ibexa\Contracts\AdminUi\Controller\Controller;
1112
use Ibexa\Contracts\AdminUi\Notification\NotificationHandlerInterface;
1213
use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1618
use Symfony\Component\Routing\Annotation\Route;
1719
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1820
use Symfony\Contracts\Translation\TranslatorInterface;
@@ -28,15 +30,19 @@ class JobController extends Controller
2830
private $notificationHandler;
2931
/** @var \Symfony\Contracts\Translation\TranslatorInterface */
3032
private $translator;
33+
/** @var \CodeRhapsodie\EzDataflowBundle\Gateway\ScheduledDataflowGateway */
34+
private $scheduledDataflowGateway;
3135

3236
public function __construct(
3337
JobGateway $jobGateway,
3438
NotificationHandlerInterface $notificationHandler,
35-
TranslatorInterface $translator
39+
TranslatorInterface $translator,
40+
ScheduledDataflowGateway $scheduledDataflowGateway
3641
) {
3742
$this->jobGateway = $jobGateway;
3843
$this->notificationHandler = $notificationHandler;
3944
$this->translator = $translator;
45+
$this->scheduledDataflowGateway = $scheduledDataflowGateway;
4046
}
4147

4248
/**
@@ -104,4 +110,37 @@ public function create(Request $request): Response
104110
]),
105111
]);
106112
}
113+
114+
/**
115+
* @Route("/run-oneshot/{id}", name="coderhapsodie.ezdataflow.job.run-oneshot", methods={"GET"})
116+
*/
117+
public function runOneShot(int $id): Response
118+
{
119+
$this->denyAccessUnlessGranted(new Attribute('ezdataflow', 'view'));
120+
121+
$scheduledDataflow = $this->scheduledDataflowGateway->find($id);
122+
123+
if ($scheduledDataflow === null) {
124+
throw new NotFoundHttpException();
125+
}
126+
127+
$newOneshotJob = new Job();
128+
$newOneshotJob->setOptions($scheduledDataflow->getOptions());
129+
$newOneshotJob->setLabel("Manual " . $scheduledDataflow->getLabel());
130+
$newOneshotJob->setScheduledDataflowId($scheduledDataflow->getId());
131+
$newOneshotJob->setRequestedDate((new \DateTime())->add(new \DateInterval('PT1H')));
132+
$newOneshotJob->setDataflowType($scheduledDataflow->getDataflowType());
133+
134+
$form = $this->createForm(CreateOneshotType::class, $newOneshotJob, [
135+
'action' => $this->generateUrl('coderhapsodie.ezdataflow.job.create'),
136+
]);
137+
138+
return new JsonResponse([
139+
'form' => $this->renderView('@ibexadesign/ezdataflow/parts/form_modal.html.twig', [
140+
'form' => $form->createView(),
141+
'id' => 'modal-new-oneshot',
142+
'mode' => 'oneshot',
143+
]),
144+
]);
145+
}
107146
}

src/Resources/config/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ services:
3838
$jobGateway: '@CodeRhapsodie\EzDataflowBundle\Gateway\JobGateway'
3939
$notificationHandler: '@Ibexa\Contracts\AdminUi\Notification\NotificationHandlerInterface'
4040
$translator: '@translator'
41+
$scheduledDataflowGateway: '@CodeRhapsodie\EzDataflowBundle\Gateway\ScheduledDataflowGateway'
4142
calls:
4243
- [ 'setContainer', [ '@service_container' ] ]
4344
- [ 'performAccessCheck', [ ] ]

src/Resources/translations/messages.en.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ coderhapsodie.ezdataflow.workflow.list.history: History
1616
coderhapsodie.ezdataflow.workflow.list.edit: Edit
1717
coderhapsodie.ezdataflow.workflow.list.disable: Disable
1818
coderhapsodie.ezdataflow.workflow.list.enable: Enable
19+
coderhapsodie.ezdataflow.workflow.list.runonce: Run now
1920
coderhapsodie.ezdataflow.history.title: History
2021
coderhapsodie.ezdataflow.history.list.title: 'Executions list'
2122
coderhapsodie.ezdataflow.history.list.name: Name

src/Resources/translations/messages.fr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ coderhapsodie.ezdataflow.workflow.list.history: Historique
1616
coderhapsodie.ezdataflow.workflow.list.edit: Éditer
1717
coderhapsodie.ezdataflow.workflow.list.disable: Désactiver
1818
coderhapsodie.ezdataflow.workflow.list.enable: Activer
19+
coderhapsodie.ezdataflow.workflow.list.runonce: Lancer maintenant
1920
coderhapsodie.ezdataflow.history.title: Historique
2021
coderhapsodie.ezdataflow.history.list.title: 'Liste des exécutions'
2122
coderhapsodie.ezdataflow.history.list.name: Nom

src/Resources/views/themes/admin/ezdataflow/Dashboard/oneshot.html.twig

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{%- block content -%}
22
{% set actions %}
33
<button
4-
type="button"
5-
class="btn ibexa-btn ibexa-btn--tertiary ibexa-btn--small"
6-
data-bs-toggle="modal"
7-
data-bs-target="#modal-new-oneshot"
4+
id="create-oneshot-button"
5+
type="button"
6+
class="btn ibexa-btn ibexa-btn--tertiary ibexa-btn--small"
7+
data-bs-toggle="modal"
8+
data-bs-target="#modal-new-oneshot"
89
>
910
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--create">
1011
<use xlink:href="{{ ibexa_icon_path('create') }}"></use>
@@ -53,6 +54,16 @@
5354
})
5455
;
5556
});
57+
const createButton = document.getElementById('create-oneshot-button')
58+
59+
if (createButton) {
60+
createButton.addEventListener('click', () => {
61+
const oneShotModal = document.getElementById('modal-new-oneshot');
62+
oneShotModal.querySelector('#create_oneshot_label').value = '';
63+
oneShotModal.querySelector('#create_oneshot_options').value = '';
64+
oneShotModal.querySelector('.flatpickr.flatpickr-input').parentNode.parentNode.ibexaInstance.flatpickrInstance.setDate(new Date(), true);
65+
});
66+
}
5667
});
5768
</script>
5869
{%- endblock -%}

src/Resources/views/themes/admin/ezdataflow/parts/tab/schedule_list.html.twig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
<use xlink:href="{{ ibexa_icon_path('edit') }}"></use>
4141
</svg>
4242
</a>
43+
<button
44+
class="btn ibexa-btn ibexa-btn--ghost run-oneshot ibexa-btn--no-text"
45+
data-url="{{ path('coderhapsodie.ezdataflow.job.run-oneshot', {id: item.id}) }}"
46+
title="{{ 'coderhapsodie.ezdataflow.workflow.list.runonce'|trans }}"
47+
>
48+
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--create">
49+
<use xlink:href="{{ ibexa_icon_path('create') }}"></use>
50+
</svg>
51+
</button>
4352
{% if item.enabled %}
4453
<a href="{{ path('coderhapsodie.ezdataflow.workflow.disable', {id: item.id}) }}"
4554
class="btn ibexa-btn ibexa-btn--ghost ibexa-btn--no-text"
@@ -125,3 +134,27 @@
125134
{% endif %}
126135
</div>
127136
</div>
137+
138+
<script>
139+
document.querySelectorAll(".run-oneshot").forEach((el) => {
140+
el.addEventListener('click', () => {
141+
const oneShotModal = document.getElementById('modal-new-oneshot');
142+
143+
fetch(el.getAttribute('data-url'))
144+
.then(response => response.json())
145+
.then(result => {
146+
const node = document.createElement('div');
147+
node.innerHTML = result.form;
148+
oneShotModal.querySelector('#create_oneshot_label').value = node.querySelector('#create_oneshot_label').value;
149+
oneShotModal.querySelector('#create_oneshot_options').value = node.querySelector('#create_oneshot_options').value;
150+
oneShotModal.querySelector('.ibexa-dropdown').ibexaInstance.selectOption(node.querySelector('#create_oneshot_dataflowType').value)
151+
oneShotModal.querySelector('.flatpickr.flatpickr-input').parentNode.parentNode.ibexaInstance.flatpickrInstance.setDate(new Date(), true);
152+
})
153+
.then(() => {
154+
bootstrap.Tab.getOrCreateInstance(document.querySelector('#ibexa-tab-label-coderhapsodie-ezdataflow-code-rhapsodie-ezdataflow-oneshot')).show()
155+
bootstrap.Modal.getOrCreateInstance(oneShotModal).show()
156+
})
157+
158+
});
159+
});
160+
</script>

0 commit comments

Comments
 (0)