What should a priest who commits a mortal sin just before Holy Mass do, if confession is not available? Imagine following situation. There is a parish in a small village. There is only one priest in that parish (let's call him Fr. Nomen Nescio). It is Easter Sunday (or some important day on which laity expect mass and surely many of the faithful will come to the mass). Mass that will be celebrated on that day is the only one in that village and it will be celebrated in the evening (say 7:30 pm or 8:00 pm). Fr. N.N. prepares for the mass, there is 15 more minutes till mass begins and Church is already almost full. Now, God forbid this awful and monstrous situation; 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.

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. 

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?
What should a priest who commits a mortal sin just before Holy Mass do, if confession is not available?

The priest in question must make a perfect act of contrition as best he can and must go to confession as soon as possible. He may under some circumstances still say Mass. The Church has very clear guidelines on this subject:


  When receiving or celebrating the sacraments, the priest is subject to the same requirements of sanctity and state of grace as every other Catholic; that is, the state of grace is required for fruitful reception of all sacraments except those that actually forgive sins. 
  
  Therefore a priest who is in a state of mortal sin should seek to confess as soon as possible and refrain from celebrating the sacraments until he has done so.  
  
  Normally, to celebrate Mass or receive Communion while in a state of mortal sin would be to commit a sacrilege. Yet, the sacrament would be valid; that is, there would be a true consecration and a true sacrifice. 
  
  The reason is: Christ is the principal actor of the sacraments, so they are efficacious even when performed by an unworthy minister. As St. Thomas Aquinas says: Christ may act even through a minister who is spiritually dead. 
  
  However, a priest who has fallen into mortal sin, but who is unable to make his confession despite his desire to do so, may celebrate Mass for the benefit of the faithful without adding a further sin of sacrilege. 
  
  Thus, as Canon 916 of the Code of Canon Law states: "A person who is conscious of grave 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 (see also Canon 1335)." 
  
  Note that the code requires a grave reason in order to avail of this exception. 
  
  One such grave reason is based on the principle of the good of souls. If a priest is required to celebrate Mass or a soul requests the sacrament of reconciliation, the anointing of the sick, or indeed any other sacrament from this priest that would have to be performed before he can avail of confession, then he may, and usually must, administer the sacrament. 
  
  A second grave reason stems from the danger of infamy by publicly revealing the state of one's soul.
  
  This can occur in the case of a priest in isolated circumstances when there is no one else to perform the usual celebrations. There is no need for him to do anything that might lead people to suspect his lack of a state of grace. 
  
  Even in the case that the priest, or any other person, has secretly committed a grave crime, which would normally lead to his or her being automatically forbidden to receive the sacraments, Church law (Canon 1352) foresees the possibility of the penalty being suspended to avoid infamy or scandal, to wit: 
  
  "§1. If a penalty prohibits the reception of the sacraments or sacramentals, the prohibition is suspended as long as the offender is in danger of death. 
  
  "§2. The obligation to observe an undeclared 'latae sententiae' penalty which is not notorious in the place where the offender is present, is suspended totally or partially whenever the offender cannot observe it without danger of grave scandal or infamy." 
  
  While the possibilities of a layperson or a religious in a state of mortal sin being placed in a similar dilemma as the priest are far rarer, the same basic principles would apply should they occur. 
  
  Furthermore, while it is nobody else's business why somebody does not approach Communion, pastors should do all that they can to avoid creating public pressures that might induce a person in a state of mortal sin — or otherwise unable to receive Communion — to receive out of an objective fear of infamy or even out of human respect. 
  
  For example, when parish ushers move down the aisles during Communion to assure an orderly procession, it becomes very difficult for someone, especially if well known to the other parishioners and who for some hidden reason cannot receive Communion, not to go forward with the others because staying in the pew is often the equivalent of making a public self-denunciation. 
  
  In such cases, a less organized procession at Communion allows such people to pass unnoticed. - When a Priest Is in Mortal Sin


As a side note: I have known of cases in the Extraordinary Rite of the Mass (as we now call it) where a priest had forgotten to fast in the morning before Mass and did not say Mass that day because the fast was not observed.