]> _ Git - psq.git/commitdiff
step 3 ws pusher
authorLouis Jeckel <louis.jeckel@outlook.cm>
Tue, 7 Apr 2020 19:14:43 +0000 (21:14 +0200)
committerLouis Jeckel <louis.jeckel@outlook.cm>
Tue, 7 Apr 2020 19:14:43 +0000 (21:14 +0200)
app/Events/ProcessBatch.php
app/Jobs/ProcessEmailBatch.php
composer.json
public/js/app.js
resources/js/components/LiveStats.vue
resources/js/components/Step3Send.vue

index 12f73478ac5323689f73dfac6cf58768ff00de30..c0870a4068d0722e3d04726ccb17ac5b7c772354 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App\Events;
 
+use http\Exception\InvalidArgumentException;
 use Illuminate\Broadcasting\Channel;
 use Illuminate\Broadcasting\InteractsWithSockets;
 use Illuminate\Broadcasting\PresenceChannel;
@@ -9,27 +10,26 @@ use Illuminate\Broadcasting\PrivateChannel;
 use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
 use Illuminate\Foundation\Events\Dispatchable;
 use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Arr;
 
-class ProcessBatch
+class ProcessBatch implements ShouldBroadcast
 {
     use Dispatchable, InteractsWithSockets, SerializesModels;
+
+
     public $data;
 
     /**
      * Create a new event instance.
      *
-     * @param $status
-     * @param $totalBatches
-     * @param $currentBatches
+     * @param $data
      */
-    public function __construct($status, $totalBatches, $currentBatches)
+    public function __construct($data)
     {
-        //
-        $this->data = [
-            'status' => $status,
-            'totalBatches' => $totalBatches,
-            'currentBatches' => $currentBatches,
-        ];
+        if(! Arr::has($data, ['batch_id', 'status'])) {
+            throw new InvalidArgumentException('Missing args');
+        }
+        $this->data = $data;
     }
 
     /**
@@ -39,7 +39,7 @@ class ProcessBatch
      */
     public function broadcastOn()
     {
-        return new PrivateChannel('emailBatch.'.$this->batch_id);
+        return new PrivateChannel('emailBatch.'.$this->data['batch_id']);
     }
 
 
index bed26bd722c2a8076a14061e1b36421615392b2c..49b2fc93d9bb5d1932e0cd1388ff9118c53a2f9e 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Jobs;
 
 use App\EmailBatch;
+use App\Events\ProcessBatch;
 use App\User;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
@@ -10,6 +11,7 @@ use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Arr;
 use Mailgun\Mailgun;
 
 class ProcessEmailBatch implements ShouldQueue
@@ -27,22 +29,41 @@ class ProcessEmailBatch implements ShouldQueue
      */
     public function __construct(EmailBatch $batch)
     {
-        //
         $this->batch = $batch;
     }
 
+    protected function processUpdate($data)
+    {
+        event(new ProcessBatch(array_merge($data, [
+            'batch_id' => $this->batch->id
+        ])));
+
+    }
+
     /**
      * Execute the job.
      *
      * @return void
+     * @throws \Throwable
      */
     public function handle()
     {
         /** @var Collection $users */
         $users = User::subscribed()->get();
 
-        $users->chunk(200)->each(function(Collection $chunk) {
-            $mailgun = resolve(Mailgun::class);
+        $size = 2;
+        $chunks = $users->chunk($size);
+
+        $this->processUpdate([
+            'status' => 'processing',
+            'total' => $count = $users->count(),
+            'current' => 0
+        ]);
+        $i = 0;
+        $mailgun = resolve(Mailgun::class);
+
+        foreach($chunks as $chunk) {
+
             $variables = json_encode($chunk->mapWithKeys(function($user) {
                 return [$user->email => [
                     'id' => $user->id,
@@ -62,12 +83,22 @@ class ProcessEmailBatch implements ShouldQueue
                 'recipient-variables' => $variables,
                 'html' => $view,
                 'o:tag' => ['batch_id_'.$this->batch->id, 'test_psq'],
-                'o:tracking' => true,
             ];
 
             $mailgun->messages()->send(env('MAILGUN_DOMAIN'), $params);
+            $this->processUpdate([
+                'status' => 'processing',
+                'total' => $count,
+                'current' => ($i++)*$size + $chunk->count()
+            ]);
+
+        }
 
-        });
+        $this->processUpdate([
+            'status' => 'done',
+            'total' => $count,
+            'current' => $count
+        ]);
 
 
 
index cfdb1c0b36cdf642436b2e0a9161481f0b37d67c..2a42c173883c273cace5c277b30efd754878bf72 100644 (file)
@@ -22,7 +22,8 @@
         "mailgun/mailgun-php": "^3.0",
         "nyholm/psr7": "^1.2",
         "pusher/pusher-php-server": "~4.0",
-        "vaites/php-apache-tika": "^0.9.1"
+        "vaites/php-apache-tika": "^0.9.1",
+        "ext-http": "*"
     },
     "require-dev": {
         "facade/ignition": "^2.0",
index 031ae3bdfcb6e018a28fc3ebf0e366dc8691f309..d043a111694aa34fc848f73d33d34c425518c015 100644 (file)
@@ -4446,6 +4446,8 @@ __webpack_require__.r(__webpack_exports__);
   mounted: function mounted() {
     Echo["private"]('emailBatch.1').listen('.mailgun.event', function (e) {
       console.log(e);
+    }).listen('.batch.status', function (e) {
+      console.log(e);
     });
   },
   methods: {}
@@ -4942,18 +4944,50 @@ __webpack_require__.r(__webpack_exports__);
 //
 //
 //
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
 /* harmony default export */ __webpack_exports__["default"] = ({
   components: {},
   data: function data() {
     return {
       upload_result: this.$root.publishState.file,
-      recipients_count: 0
+      recipients_count: 0,
+      status: 'En attente',
+      counters: {
+        delivered: 0,
+        opened: 0
+      },
+      publishing: false
     };
   },
   computed: {},
   methods: {
-    processEvent: function processEvent(e) {
-      console.log(e);
+    processMailEvent: function processMailEvent(e) {
+      if (e.data.event === 'delivered') this.counters.delivered++;
+      if (e.data.event === 'opened') this.counters.opened++;
+    },
+    processProgressEvent: function processProgressEvent(e) {
+      switch (e.data.status) {
+        case 'init':
+          this.status = "Préparation en cours";
+          break;
+
+        case 'processing':
+          this.status = "Traitement en cours : (".concat(e.data.current, " / ").concat(e.data.total, ")");
+          break;
+
+        case 'done':
+          this.status = "Termin\xE9, ".concat(e.data.total, " emails envoy\xE9s");
+      }
     },
     publish: function publish() {
       var _this = this;
@@ -4965,7 +4999,8 @@ __webpack_require__.r(__webpack_exports__);
           data: this.$root.publishState
         }
       }).then(function (d) {
-        Echo["private"]("emailBatch.".concat(d.data)).listen('.mailgun.event', _this.processEvent);
+        Echo["private"]("emailBatch.".concat(d.data.data)).listen('.mailgun.event', _this.processMailEvent).listen('.batch.status', _this.processProgressEvent);
+        _this.publishing = true;
       });
     }
   },
@@ -51690,10 +51725,32 @@ var render = function() {
     _c("div", [
       _c(
         "button",
-        { staticClass: "btn btn-danger", on: { click: _vm.publish } },
+        {
+          staticClass: "btn btn-danger d-block m-auto",
+          on: { click: _vm.publish }
+        },
         [_vm._v("Envoyer " + _vm._s(_vm.recipients_count) + " emails !")]
       )
-    ])
+    ]),
+    _vm._v(" "),
+    _vm.publishing
+      ? _c("div", { staticClass: "alert alert-success my-3" }, [
+          _c("strong", [_vm._v("Statut : ")]),
+          _vm._v(_vm._s(_vm.status) + "\n    ")
+        ])
+      : _vm._e(),
+    _vm._v(" "),
+    _vm.publishing
+      ? _c("div", { staticClass: "row", attrs: { id: "counters" } }, [
+          _c("div", { staticClass: "col-6" }, [
+            _vm._v("Reçus : " + _vm._s(_vm.counters.delivered))
+          ]),
+          _vm._v(" "),
+          _c("div", { staticClass: "col-6" }, [
+            _vm._v("Ouverts : " + _vm._s(_vm.counters.opened))
+          ])
+        ])
+      : _vm._e()
   ])
 }
 var staticRenderFns = [
index a28d5da38f4e468c7459fe54e6107997b218c815..eacd2782609b7f708effac0a52d1b23fe8d50ea0 100644 (file)
@@ -21,6 +21,9 @@
             .listen('.mailgun.event', (e) => {
                 console.log(e);
             })
+            .listen('.batch.status', (e) => {
+                console.log(e);
+            })
 
         },
         methods: {
index 3f77b3f299a780c56f04625e6752434253c35e87..d9f82f02c114cc4ccf10b7b94bb1f98eda601f6e 100644 (file)
         </div>
 
         <div>
-            <button class="btn btn-danger" @click="publish">Envoyer {{recipients_count}} emails !</button>
+            <button class="btn btn-danger d-block m-auto" @click="publish">Envoyer {{recipients_count}} emails !</button>
+        </div>
+
+
+        <div class="alert alert-success my-3" v-if="publishing">
+            <strong>Statut : </strong>{{status}}
+        </div>
+
+        <div id="counters" class="row" v-if="publishing">
+            <div class="col-6">Reçus : {{counters.delivered}}</div>
+            <div class="col-6">Ouverts : {{counters.opened}}</div>
+
         </div>
 
 
             return {
                 upload_result: this.$root.publishState.file,
                 recipients_count: 0,
+                status: 'En attente',
+                counters : {
+                    delivered : 0,
+                    opened : 0
+                },
+                publishing: false,
             }
         },
         computed: {
         },
         methods: {
 
-            processEvent(e) {
-                console.log(e);
+            processMailEvent(e) {
+                if(e.data.event === 'delivered')
+                    this.counters.delivered++;
+                if(e.data.event === 'opened')
+                    this.counters.opened++;
+
+            },
+
+            processProgressEvent(e) {
+                switch(e.data.status){
+                    case 'init':
+                        this.status = "Préparation en cours";
+                        break;
+                    case 'processing':
+                        this.status = `Traitement en cours : (${e.data.current} / ${e.data.total})`;
+                        break;
+                    case 'done':
+                        this.status = `Terminé, ${e.data.total} emails envoyés`;
+                }
 
             },
 
                     data: {data: this.$root.publishState}
                 }).then(d => {
 
-                    Echo.private(`emailBatch.${d.data}`)
-                    .listen('.mailgun.event', this.processEvent);
+                    Echo.private(`emailBatch.${d.data.data}`)
+                    .listen('.mailgun.event', this.processMailEvent)
+                    .listen('.batch.status', this.processProgressEvent);
+                    this.publishing = true;
 
                 })