يأتي Rails 6 مع وضعين للتحميل التلقائي: zeitwerk و classic. في طلب السحب هذا https://github.com/discourse/discourse/pull/8083، قمت بترقية Rails إلى الإصدار 6.0.0 باستخدام محمل الكود الكلاسيكي كمرحلة انتقالية. سيكون من المثير للاهتمام محاولة التبديل إلى Zeitwerk.
يعتبر Zeitwerk محمل كود فعال وآمن للمواضيع (thread-safe) لـ Ruby. طالما أن المشروع يتبع اتفاقيات التسمية، يمكن لـ Zeitwerk العثور على الملفات الصحيحة وتحميلها عند الطلب أو مسبقًا دون الحاجة إلى أي require أو require_dependency. علاوة على ذلك، قد يمنح ذلك دفعة أداء صغيرة للتطبيق وفقًا لهذا المقال https://weblog.rubyonrails.org/2019/2/22/zeitwerk-integration-in-rails-6-beta-2/
هناك عدة خطوات أحتاج إلى القيام بها لجعلها تعمل:
تغيير اسم بعض الفئات لتتوافق مع اتفاقية تسمية Rails. على سبيل المثال، يجب أن يحدد الملف canonical_url.rb فئة CanonicalUrl بدلاً من CanonicalURL. وبالمثل، يجب أن يحدد الملف ondiff.rb فئة Onpdiff بدلاً من ONPDiff. ستكون هناك نهج بديل وهو ربط مُعرِّف مخصص (custom inflector) بالمشروع، إلا أنني أعتقد أن اتباع الاتفاقية قد يكون خيارًا أفضل - https://github.com/fxn/zeitwerk#custom-inflector
وبالمثل، وفقًا للاتفاقية، يجب تغليف عمليات التحقق المخصصة (custom validations) الموجودة في مجلد validations بوحدة Validations. بالإضافة إلى ذلك، ترث بعض عمليات التحقق من EachValidator ويجب أن تكون قابلة للوصول بدون مساحة أسماء (namespace). أخطط لنقلها إلى مجلد منفصل وإضافتها إلى مسارات التحميل التلقائي.
إزالة جميع require_dependency والتأكد من عمل المشروع.
إزالة جميع require والتأكد من عمل Discourse.
التأكد من أن جميع الإضافات (plugins) يمكنها الوصول إلى التبعيات المطلوبة. لا أعرف كيفية تحقيق ذلك بعد. أريد أولاً جعل Discourse يعمل دون أي إضافات.
I made some progress around Zeitwerk, however, I changed my approach. My original plan was to change every place where Discourse is not following the Zeitwerk filename convention. After a few fixes, I realised that this is just the tip of the iceberg and noticed that if I am going to follow that path, then pull request will be difficult to read and confidently merge to master. For example, all job classes under regular directory should have Regular namespace, same with Onceoff and Scheduled.
I decided to step back a little bit and think about a more evolutionary approach than revolutionary.
I decided that it would be better to introduce custom Inflector, which will cover all files which are not following Zeitwerk convention. The biggest benefit will be that we would be able to deploy that small change and once we are happy with Zeitwerk and we don’t have any performance downgrades, we can start fixing convention file by file in reasonable small pull requests.
I found some problems which could not be sorted by custom Inflector, so I made additional fixes to make it work.
Pull request is still in progress, however, at this stage, I can run Discourse with Zeitwerk and default plugins, run all specs and run benchmark without a problem.
I wanted first to be at that stable state when all specs are passing. Now I can confidently start removing all require_dependency one by one and also test official plugins. Once everything is ready, I will share with you benchmark results in this post.
We can try another way to benchmark. What would you say about more iterations, something which would run for an hour? In addition, instead of taking the best result, compare the average from each experiment. That may give more consistent numbers. What do you think?
@sam I think we are good to go, I rebased with the latest master and made a small adjustment to Webauthn.
I checked 3 things:
run the server on local and click a little bit to ensure it works as expected
run specs
downloaded all official plugins and ensured that specs for plugins are passing (we need to merge adjustments for plugins first)