What should a priest who commits a mortal sin just before Holy Mass do, if confession is not available?
"A person who is conscious of grave sin (mortal sin) is not to celebrate Mass or receive the body of the Lord without previous sacramental confession unless there is a grave reason and there is no opportunity to confess; in this case the person is to remember the obligation to make an act of perfect contrition which includes the resolution of confessing as soon as possible." (Code of Canon Law, canon 916 Archived June 28, 2011, at the Wayback Machine)
There are three conditions in order for a person even a Priest to commit mortal sin.
Three conditions are necessary for mortal sin to exist:
Grave Matter: The act itself is intrinsically evil and immoral.
Full Knowledge: The person must know that what they're doing or planning to do is evil and immoral.
Deliberate Consent: The person must freely choose to commit the act or plan to do it.
Mortal and Venial Sins in the Catholic Church
Example situation in which the priest fall into;
Fr. N.N. commits mortal sin just 5 minutes before mass. He is only priest in that parish (there is no other priest where he could go to confession). Because he is in a small village, the closest priest which could confess him is at least 1 hour of driving away. He is very sorry for his sin that he just committed, however, he has only imperfect contrition; he is unable to move himself to perfect contrition.
What should Fr. N.N. do in that situation?
If he goes to celebrate mass then he is obliged to partake of Body and Blood of Christ, which would for Fr. N.N. be one of the worst mortal sins.
On the other hand, if he does not go to celebrate mass then there is a great evil, every faithful in that village will not get to go on mass on Easter Sunday and everyone already came to mass. If he does not go to celebrate mass, faithful will expect some good reason from Fr. N.N. why there will be no mass.
The priest in your narration had already admitted his fault and attempted to seek the pardon of God, only then he was not able to reach a state of perfect contrition.
If the fallen priest continue to celebrate the Holy Mass taking into consideration the people who are already present to celebrate the Holy Mass and also knowing that there's no time for him to seek the help of another priest, then by the definition of the three elements of mortal sins the fallen priest does not satisfies the conditions of committing mortal sin. Therefore your assumptions of the situation is in error, it is not a mortal sin anymore in the part of the fallen priest.
Now, let's take a look on the issue of the sacrilege, the question assumed that the priest was unable to reach the perfect state of contrition. In this scenario only God can judge the action of the priest as no man has the faculties to see the heart of a man.It's not for us to make a judgement that he committed a sacrilege eventhough the Church clearly defined that those who partake the Holy Communion in the state of mortal sin committed a sacrilege but the issue was negated by the fact that the fallen priest does not deliberately intend to partake but was only forced by the urgency of the situation.
What course of action should Fr. N.N. take?
I am not sure (that is why I am asking this question) what should Fr. N.N. do, however it seems to me that there is no situation in which one should commit mortal sin; therefore it seems to me that Fr. N.N. should not celebrate mass.
What course of action should the fallen priest do? Asking for forgiveness and seeking the mercy of God is the first step as you narrated was already initiated by the fallen priest. So, the first step was good. The next step since the fallen priest already acknowledged his fault then he should expect the merciful graces of God to come as it is the Will of God primarily for Him to fulfill his obligation or priestly vows. Our loving and merciful God does not count how many times we fall but encourage all of us to rise up by seeking His merciful graces.
Has the Church foreseen such awful situations and when these situations occur, is the priest somehow not obliged to partake of Body and Blood of Christ during Mass?
The answer is absolutely yes, that's why the Church thru the Wisdom of the Holy Spirit had instituted the Canon Law that oversee the governance and discipline of the Catholic Church Ministers of the Holy Eucharist not just for the Clergy but also the issues towards the lay faithfuls.
Code of Canon Law
THE MINISTER OF THE MOST HOLY EUCHARIST
Other consideration; you described the situation as awful but let us not forget that your described situation is very light compare to actual situations that happened in the Catholic Church. What do I mean, God Permissive Will allowed ordained priest to celebrate the Holy Mass that was identified as belonging to the Communist group that infiltrated the Church as testified by Bella Dodd. This ordained priest has no vocation nor God's calling to serve the priesthood but God allows them to infiltrate the Church and celebrated Holy Mass eventhough the heart of this ordained priest was corrupted by the malice of Satan.
Full text of "Investigation of Communist activities in the Columbus, Ohio, area. Hearings"
Also, one good example that God allows the celebration of the Holy Mass even if a priest celebrant is sinful or perhaps not in the state of grace is the personal testimony of Fr.Steven Scheier. For twelve years, as Fr.Scheier testified that he was not a good priest and as you said it is a sacrilege on the part of the priest to partake the Holy Communion in the state of mortal sin. So, the testimony affirmed that Jesus judgment for him is eternal damnation. But there's a beautiful story behind the beautiful life of Fr.Scheier as he rise up from his lukewarm or sinful ways as a Priest.
It did not take long before everything seemed to come back to me. The following seemed to happen immediately after the accident. I was before the Throne of Judgment! Jesus Christ was the Judge. I didn’t see Him, I merely heard Him. What took place was instantaneous as far as “our time” is concerned. He went through my entire life on earth and accused me of sins of commission and omission that were unconfessed and therefore unforgiven and unrepented sins. To each offense, I said, “Yes, Lord!” I had planned that when this would happen I would have all kinds of excuses to say to the Lord. For example, “Well, Lord, you know, she was a pretty feisty woman, and one lost his patience very easily with her all the time!” Well, when you are talking to Truth personified, you don’t have any excuses; so all you say is “Yes, Lord!”
Mother – He’s Yours
He reached the end of my judgement and said to me, “Your sentence is hell!” Again, I said, “Yes Lord,
I know!” It was the only logical conclusion that He could have come up with. It was not a shock to my system! It was as if He were honoring my choice, my decision. I had chosen my sentence; He was merely honoring my choice. It was then, after He had said this that I heard a woman’s voice, “Son, would You spare his life and his immortal soul?” The Lord said, “Mother, he has been a priest twelve years not for himself and not for me; let him reap the punishment he deserves!” She, in reply said, “But Son, what if we give to him special graces and strengths and then see if he bears fruit. If not, Your will be done!” There was a very short pause and then I heard Him say, “Mother, he’s yours!” And I have been hers both naturally and supernaturally now for the past twelve years. I don’t believe that I could have been without her for the length of time that she was absent from my life and my spirituality.
God’s Merciful Judgement: A Priest’s True Story
by FR. STEVEN SCHEIER | DECEMBER 17, 2015