تبديل الشيفرة الفوري والترقيات الحية
تعلّم كيف تحدّث Erlang الشيفرة قيد التشغيل دون إيقاف النظام، وكيف تدعم سلوكيات OTP الترقيات الحية التي تحافظ على الحالة.
تبديل الشيفرة الفوري والترقيات الحية درس مجاني في Erlang OTP: Distributed & Fault-Tolerant Systems Programming على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Erlang OTP: Distributed & Fault-Tolerant Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Erlang OTP: Distributed & Fault-Tolerant Systems Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Zero-Downtime Upgrades
One of Erlang's signature features is hot code swapping: replacing a module's code while the system keeps running, with no restart and no dropped connections.
Two Code Versions
The VM keeps up to two versions of each module: current and old. Loading a third version purges the oldest.
Local vs External Calls
A local call (foo()) stays in the running version. An external/qualified call (?MODULE:foo()) jumps to the newest loaded version. This distinction drives upgrades.
loop(State) ->
receive
Msg ->
NewState = handle(Msg, State),
?MODULE:loop(NewState) % picks up new code
end.Loading New Code
You compile and load the new version with the code module or via the shell.
c(my_module).
code:load_file(my_module).The code_change Callback
Stateful OTP behaviors like GenServer support a code_change/3 callback. OTP calls it during an upgrade so you can transform old state into the new format.
code_change(_OldVsn, State, _Extra) ->
{ok, migrate_state(State)}.Why State Migration Matters
If your new version stores state differently (say a tuple becomes a map), code_change bridges the gap so live processes do not crash on the new code.
migrate_state({old, A, B}) ->
#{a => A, b => B}.Release Upgrades
For whole-system upgrades, OTP uses appup and relup files that describe the steps to move from one release version to the next.
{"1.1.0",
[{"1.0.0", [{update, my_server, {advanced, []}}]}],
[{"1.0.0", [{update, my_server, {advanced, []}}]}]}.Suspending Processes
During an advanced upgrade, OTP suspends the affected processes, applies code_change, then resumes them, ensuring no message is handled with mismatched state.
Purging Old Code
After an upgrade, processes still running old code can be checked and the old version purged once safe.
code:soft_purge(my_module).
% returns false if any process still runs old codeWhen to Use It
Hot upgrades shine for telecom-grade always-on systems. For many apps, a rolling restart of nodes is simpler. Choose based on your uptime requirements.
Best Practices
Key rules:
- Always make the recursive loop call qualified (
?MODULE:loop) - Implement
code_changefor any stateful behavior - Test upgrades in staging before production
Quick Check
Test your hot-swap knowledge.
Recap
You learned Erlang's live upgrade model.
- The VM keeps two code versions
- Qualified calls adopt new code
code_change/3migrates state during OTP upgrades- appup/relup drive full release upgrades
تعلم Erlang مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «تبديل الشيفرة الفوري والترقيات الحية» مجاني؟
نعم — نص درس «تبديل الشيفرة الفوري والترقيات الحية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Erlang OTP: Distributed & Fault-Tolerant Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Erlang OTP: Distributed & Fault-Tolerant Systems Programming 4 دروس في المجموع.
ماذا ستتعلم في «تبديل الشيفرة الفوري والترقيات الحية»؟
تعلّم كيف تحدّث Erlang الشيفرة قيد التشغيل دون إيقاف النظام، وكيف تدعم سلوكيات OTP الترقيات الحية التي تحافظ على الحالة. تتمرن على Erlang OTP: Distributed & Fault-Tolerant Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Erlang OTP: Distributed & Fault-Tolerant Systems Programming؟
لا تُشترط خبرة سابقة. Erlang OTP: Distributed & Fault-Tolerant Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تبديل الشيفرة الفوري والترقيات الحية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Erlang OTP: Distributed & Fault-Tolerant Systems Programming هذا؟
نعم. كل درس في Erlang OTP: Distributed & Fault-Tolerant Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- GenStatem لإدارة الحالة
- GenEvent لمعالجة الأحداث
- سلوكيات OTP مخصصة
- تبديل الشيفرة الفوري والترقيات الحية