Another interpretation of "He set an end to the darkness" (Job 28:3): All the afflictions that come upon a person have an end to them, as it is said (in the same verse), "and He searches out every limit." It speaks about Joseph. At the time he was confined in the prison, he was there ten years, and he was to have gone out in the eleventh year. Thus, "He set an end to the darkness." What is the meaning of "and He searches out every limit"? That at the time when his appointed season arrived to go out, the Holy One, blessed be He, sat and searched out and saw that he was required to remain in the prison two more years, because he had relied upon the chief of the cupbearers, when he said to him two times (in Genesis 40:14), "But remember me when it is well with you," and "and mention me to Pharaoh." The Holy One, blessed be He, said to him: You relied upon him with two rememberings; by your life, do two more years! As it is said (in Genesis 41:1), "And it came to pass at the end of two full years."
Joseph's Two Extra Prison Years and the End Set for Darkness
Midrash Tanchuma Buber, Miketz 1:2
ד"א קץ שם לחושך, כל היסורין הבאין על האדם קץ יש להם, שנאמר ולכל תכלית הוא חוקר, מדבר ביוסף, בשעה שנאסר בבית האסורים היה שם עשר שנים, והיה לו לצאת באחת עשרה, הוי קץ שם לחושך, מהו לכל תכלית הוא חוקר, שבשעה שהגיעה עונתו לצאת, ישב הקב"ה וחקר וראה שמתבקש להיות עוד בבית האסורים שתי שנים, על ידי שבטח בשר המשקים שאמר לו ב'פעמים כי אם זכרתני אתך (בראשית מ יד), והזכרתני אל פרעה (שם), א"ל הקב"ה אתה הבטחת עליו שתי זכירות, חייך עשה עוד שתי שנים, שנאמר ויהי מקץ שנתים ימים.