AppServiceProvider.php 2.76 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1 2 3 4
<?php

namespace App\Providers;

5 6
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
7
use Illuminate\Support\Facades\Redis;
8 9
use Illuminate\Support\ServiceProvider;
use Queue;
Dominik Hebeler's avatar
Dominik Hebeler committed
10
use Request;
Dominik Hebeler's avatar
Dominik Hebeler committed
11 12 13 14 15 16 17 18 19 20

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
21 22 23 24 25 26

        /**
         * metager.org is our english Domain
         * We will change the Locale to en
         */
        $host = Request::header("X_Forwarded_Host", "");
27 28 29
        if (empty($host)) {
            $host = Request::header("Host", "");
        }
30

31 32 33
        if (stripos($host, "metager.org") !== false) {
            \App::setLocale('en');
        }
34

35 36 37
        # Wir loggen im Redis-System für jede Sekunde des Tages, wie viele Worker aktiv am Laufen waren.
        # Dies ist notwendig, damit wir mitbekommen können, ab welchem Zeitpunkt wir zu wenig Worker zur Verfügung haben.
        Queue::before(function (JobProcessing $event) {
38
            $this->begin = strtotime(date(DATE_RFC822, mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"))));
39 40
        });
        Queue::after(function (JobProcessed $event) {
41 42
            $today = strtotime(date(DATE_RFC822, mktime(0, 0, 0, date("m"), date("d"), date("Y"))));
            $end = strtotime(date(DATE_RFC822, mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")))) - $today;
43 44
            $expireAt = strtotime(date(DATE_RFC822, mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"))));
            try {
45
                $redis = Redis::connection('redisLogs');
46
                if (!$redis) {
47
                    return;
48 49
                }

50 51
                $p = getmypid();
                $host = gethostname();
52
                $begin = $this->begin - $today;
53 54
                $redis->pipeline(function ($pipe) use ($p, $expireAt, $host, $begin, $end) {
                    for ($i = $begin; $i <= $end; $i++) {
55 56 57 58
                        $pipe->sadd("logs.worker.$host.$i", strval($p));
                        $pipe->expire("logs.worker.$host.$i", 10);
                        $pipe->eval("redis.call('hset', 'logs.worker.$host', '$i', redis.call('scard', 'logs.worker.$host.$i'))", 0);
                        $pipe->sadd("logs.worker", $host);
59
                        if (date("H") !== 0) {
60 61 62
                            $pipe->expire("logs.worker.$host", $expireAt);
                            $pipe->expire("logs.worker", $expireAt);
                        }
63
                    }
64
                });
65
            } catch (\Exception $e) {
66 67
                return;
            }
68
        });
Dominik Hebeler's avatar
Dominik Hebeler committed
69 70 71 72 73 74 75 76 77 78 79 80
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}